xs_mime.h 2.0 KB

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