Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add known_types/0 function that lists all known types #79

Merged
merged 4 commits into from
Aug 20, 2023

Conversation

goncalotomas
Copy link
Contributor

Adds a known_types/0 function that returns a list of all known types. Fixes #75.

iex(1)> MIME.known_types
["image/bmp", "application/json-patch+json",
 "application/vnd.oasis.opendocument.spreadsheet", "application/zip",
 "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
 "application/xhtml+xml", "application/x-bzip2", "application/manifest+json",
 "application/vnd.ms-powerpoint", "application/json", "video/mpeg",
 "application/x-7z-compressed", "video/mp4",
 "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
 "text/css", "application/vnd.etsi.asic-e+zip", "text/xml", "text/plain",
 "audio/aac", "image/svg+xml", "video/x-msvideo", "image/heic", "image/avif",
 "text/html",
 "application/vnd.openxmlformats-officedocument.presentationml.presentation",
 "application/x-abiword", "application/postscript", "video/ogg", "audio/opus",
 "application/x-freearc", "audio/webm", "font/woff", "text/markdown",
 "application/x-httpd-php", "audio/wav", "application/vnd.amazon.ebook",
 "application/x-bzip", "application/rss+xml", "image/png", "application/ogg",
 "application/wasm", "application/x-cdf", "video/x-ms-wmv", "audio/midi",
 "video/webm", "video/quicktime", "image/jpeg", "image/vnd.adobe.photoshop",
 "text/calendar", "video/3gpp2", ...]

Copy link
Member

@whatyouhide whatyouhide left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Can we add a test for this? It's enough to test that the returned term is a list of strings, has some known types in it, and has some user-defined known types in it.

lib/mime.ex Show resolved Hide resolved
lib/mime.ex Outdated
Returns a list of all known types, including custom types compiled into the MIME module.
"""
def known_types do
Map.keys(unquote(Macro.escape(all_types)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be better to compile them with their extensions, so all information becomes available?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made it return just the types because that's what I would expect a known_types function to do but it's also better to have more information. I changed it to return the mapping of types to extensions. :)

@goncalotomas
Copy link
Contributor Author

Added a test and changed the return type to include extensions:

iex(1)> MIME.known_types
%{
  "image/bmp" => ["bmp"],
  "application/json-patch+json" => ["json-patch"],
  "application/vnd.oasis.opendocument.spreadsheet" => ["ods"],
  "application/zip" => ["zip"],
  "application/vnd.openxmlformats-officedocument.wordprocessingml.document" => ["docx"],
  "application/xhtml+xml" => ["xhtml"],
  "application/x-bzip2" => ["bz2"],
  "application/manifest+json" => ["webmanifest"],
  "application/vnd.ms-powerpoint" => ["ppt"],
  "application/json" => ["json"],
  "video/mpeg" => ["mpeg", "mpg"],
  "application/x-7z-compressed" => ["7z"],
  "video/mp4" => ["mp4"],
  "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" => ["xlsx"],
  "text/css" => ["css"],
  "application/vnd.etsi.asic-e+zip" => ["asice", "sce"],
  "text/xml" => ["xml"],
  "text/plain" => ["txt", "text"],
  "audio/aac" => ["aac"],
  ...
}

Copy link
Member

@whatyouhide whatyouhide left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!

lib/mime.ex Show resolved Hide resolved
lib/mime.ex Outdated Show resolved Hide resolved
@josevalim josevalim merged commit 97c70ec into elixir-plug:master Aug 20, 2023
@josevalim
Copy link
Member

💚 💙 💜 💛 ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support listing known MIME types
3 participants