mime-types.scm 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. (library (lib mime-types)
  2. (export guess-mime-type
  3. guess-binary-file?)
  4. (import
  5. (except (rnrs base) let-values)
  6. (only (guile) lambda* λ error when display sleep)
  7. ;; SRFIs
  8. ;; hash tables
  9. (prefix (srfi srfi-69) srfi-69:)
  10. ;; strings
  11. (prefix (srfi srfi-13) srfi-13:)
  12. (prefix (fslib) fslib:)
  13. (prefix (logging) log:)))
  14. (define guess-mime-type
  15. (λ (file-location)
  16. (let ([file-ext (fslib:file-extension file-location)])
  17. (srfi-69:hash-table-ref file-extension-mime-types
  18. file-ext
  19. ;; default guess is plain text
  20. (λ () 'text/plain)))))
  21. (define guess-binary-file?
  22. (λ (file-location)
  23. (let ([file-ext (fslib:file-extension file-location)])
  24. (srfi-69:hash-table-ref file-extension-binary
  25. file-ext
  26. ;; default guess is that the file is a not binary file
  27. (λ () #f)))))
  28. ;; The following hash-table contains MIME types for common file types.
  29. ;; See also:
  30. ;; https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
  31. ;; https://www.iana.org/assignments/media-types/media-types.xhtml
  32. ;; This list of MIME types is not comprehensive. It is only a personal opinion
  33. ;; of what might be needed.
  34. (define file-extension-mime-types
  35. (srfi-69:alist->hash-table
  36. '(("bz" . application/x-bzip)
  37. ("bz2" . application/x-bzip2)
  38. ("css" . text/css)
  39. ("csv" . text/csv)
  40. ("gz" . application/gzip)
  41. ("gif" . image/gif)
  42. ("htm" . text/html)
  43. ("html" . text/html)
  44. ("jpg" . image/jpeg)
  45. ("jpeg" . image/jpeg)
  46. ("js" . text/javascript)
  47. ("json" . application/json)
  48. ("mjs" . text/javascript)
  49. ("odp" . application/vnd.oasis.opendocument.presentation)
  50. ("ods" . application/vnd.oasis.opendocument.spreadsheet)
  51. ("odt" . application/vnd.oasis.opendocument.text)
  52. ("oga" . audio/ogg)
  53. ("ogx" . application/ogg)
  54. ("png" . image/png)
  55. ("pdf" . application/pdf)
  56. ("sh" . application/x-sh)
  57. ("svg" . image/svg+xml)
  58. ("tar" . application/x-tar)
  59. ("tif" . image/tiff)
  60. ("tiff" . image/tiff)
  61. ("ttf" . font/ttf)
  62. ("txt" . text/plain)
  63. ("wav" . audio/wav)
  64. ("weba" . audio/webm)
  65. ("webm" . video/webm)
  66. ("webp" . image/webp)
  67. ("xhtml" . application/xhtml+xml)
  68. ("xul" . application/vnd.mozilla.xul+xml)
  69. ("zip" . application/zip)
  70. ("7z" . application/x-7z-compressed))))
  71. ;; The following hash-table contains MIME types for common file types.
  72. ;; See also:
  73. ;; https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
  74. ;; https://www.iana.org/assignments/media-types/media-types.xhtml
  75. ;; This list of MIME types is not comprehensive. It is only a personal opinion
  76. ;; of what might be needed.
  77. (define file-extension-binary
  78. (srfi-69:alist->hash-table
  79. '(("bz" . #t)
  80. ("bz2" . #t)
  81. ("css" . #f)
  82. ("csv" . #f)
  83. ("gz" . #t)
  84. ("gif" . #t)
  85. ("htm" . #f)
  86. ("html" . #f)
  87. ("jpg" . #t)
  88. ("jpeg" . #t)
  89. ("js" . #f)
  90. ("json" . #f)
  91. ("mjs" . #f)
  92. ("odp" . #t)
  93. ("ods" . #t)
  94. ("odt" . #t)
  95. ("oga" . #t)
  96. ("ogx" . #t)
  97. ("png" . #t)
  98. ("pdf" . #t)
  99. ("sh" . #f)
  100. ("svg" . #f) ; should svg be considered binary?
  101. ("tar" . #t)
  102. ("tif" . #t)
  103. ("tiff" . #t)
  104. ("ttf" . #t)
  105. ("txt" . #f)
  106. ("wav" . #t)
  107. ("weba" . #t)
  108. ("webm" . #t)
  109. ("webp" . #t)
  110. ("xhtml" . #f)
  111. ("xul" . #f)
  112. ("zip" . #t)
  113. ("7z" . #t))))