racket-mimetypes
mimetype-for-ext
9.2

racket-mimetypes🔗ℹ

Hans Dijkema <hans@dijkewijk.nl>

 (require racket-mimetypes) package: racket-mimetypes

The racket-mimetypes library maps file extensions to MIME types. It uses a built-in fallback table and keeps a validated copy of Apache’s mime.types in Racket’s user cache directory. The cache file is stored below (find-system-path 'cache-dir) in the racket-mimetypes directory.

The first lookup that finds an expired cache starts a background update. Lookups never wait for the download. After a successful update, the next check is performed after seven days. After a failed update, the next attempt is made after one day. Both outcomes are written as debug messages through the simple-log logging system.

The downloaded file is parsed and validated before it replaces the cache. If downloading, parsing, validation, or replacement fails, the existing cache remains available. If Apache assigns an extension more than once, the last assignment in the file is used.

procedure

(mimetype-for-ext ext [#:default default])  string?

  ext : (or/c path? string? symbol?)
  default : string? = "application/octet-stream"
Returns the MIME type associated with ext, or default when the extension is unknown.

ext can be an extension, a file name, or a path. Strings and symbols can contain an extension with or without a leading dot. For a file name or path, the text after the last dot in the last path component is used. Matching is case-insensitive.

The procedure does not parse URLs and does not inspect file contents.

(mimetype-for-ext 'mp3)
(mimetype-for-ext ".PNG")
(mimetype-for-ext "music/track.opus")
(mimetype-for-ext "unknown" #:default "unknown/type")