xs_mime.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* copyright (c) 2022 - 2025 grunfink et al. / MIT license */
  2. #ifndef _XS_MIME_H
  3. #define _XS_MIME_H
  4. const char *xs_mime_by_ext(const char *file);
  5. extern const char *xs_mime_types[];
  6. #ifdef XS_IMPLEMENTATION
  7. /* intentionally brain-dead simple */
  8. /* CAUTION: sorted by extension */
  9. const char *xs_mime_types[] = {
  10. "3gp", "video/3gpp",
  11. "aac", "audio/aac",
  12. "apng", "image/apng",
  13. "avif", "image/avif",
  14. "css", "text/css",
  15. "flac", "audio/flac",
  16. "flv", "video/flv",
  17. "gif", "image/gif",
  18. "gmi", "text/gemini",
  19. "html", "text/html",
  20. "jpeg", "image/jpeg",
  21. "jpg", "image/jpeg",
  22. "json", "application/json",
  23. "m4a", "audio/aac",
  24. "m4v", "video/mp4",
  25. "md", "text/markdown",
  26. "mov", "video/quicktime",
  27. "mp3", "audio/mp3",
  28. "mp4", "video/mp4",
  29. "mpg4", "video/mp4",
  30. "oga", "audio/ogg",
  31. "ogg", "audio/ogg",
  32. "ogv", "video/ogg",
  33. "opus", "audio/ogg",
  34. "png", "image/png",
  35. "svg", "image/svg+xml",
  36. "svgz", "image/svg+xml",
  37. "txt", "text/plain",
  38. "wav", "audio/wav",
  39. "webm", "video/webm",
  40. "webp", "image/webp",
  41. "wma", "audio/wma",
  42. "xml", "text/xml",
  43. NULL, NULL,
  44. };
  45. const char *xs_mime_by_ext(const char *file)
  46. /* returns the MIME type by file extension */
  47. {
  48. const char *ext = strrchr(file, '.');
  49. if (ext) {
  50. xs *uext = xs_tolower_i(xs_dup(ext + 1));
  51. int b = 0;
  52. int t = xs_countof(xs_mime_types) / 2 - 2;
  53. while (t >= b) {
  54. int n = (b + t) / 2;
  55. const char *p = xs_mime_types[n * 2];
  56. int c = strcmp(uext, p);
  57. if (c < 0)
  58. t = n - 1;
  59. else
  60. if (c > 0)
  61. b = n + 1;
  62. else
  63. return xs_mime_types[(n * 2) + 1];
  64. }
  65. }
  66. return "application/octet-stream";
  67. }
  68. #endif /* XS_IMPLEMENTATION */
  69. #endif /* XS_MIME_H */