View Source Majic.Extension (Majic v1.1.1)
Helper module to fix extensions. Uses MIME.
Summary
Types
If an extension is defined for a given MIME type, append it to the previous extension.
If no extension is defined for a given MIME type, use the subtype as its extension.
Functions
Fix name
's extension according to result_or_mime_type
.
Types
@type option_append() :: {:append, false | true}
If an extension is defined for a given MIME type, append it to the previous extension.
If no extension could be found for the MIME type, and subtype_as_extension: false
, the returned filename will have no extension.
@type option_subtype_as_extension() :: {:subtype_as_extension, false | true}
If no extension is defined for a given MIME type, use the subtype as its extension.
Functions
@spec fix(Path.t(), Majic.Result.t() | String.t(), [ option_append() | option_subtype_as_extension() ]) :: Path.t()
Fix name
's extension according to result_or_mime_type
.
iex(1)> {:ok, result} = Majic.perform("cat.jpeg", once: true)
{:ok, %Majic.Result{mime_type: "image/webp", ...}}
iex(1)> Majic.Extension.fix("cat.jpeg", result)
"cat.webp"
The append: true
option will append the correct extension to the user-provided one, if there's an extension for the
type:
iex(1)> Majic.Extension.fix("cat.jpeg", result, append: true)
"cat.jpeg.webp"
iex(2)> Majic.Extension.fix("Makefile.txt", "text/x-makefile", append: true)
"Makefile"
The subtype_as_extension: true
option will use the subtype part of the MIME type as an extension for the ones that
don't have any:
iex(1)> Majic.Extension.fix("Makefile.txt", "text/x-makefile", subtype_as_extension: true)
"Makefile.x-makefile"
iex(1)> Majic.Extension.fix("Makefile.txt", "text/x-makefile", subtype_as_extension: true, append: true)
"Makefile.txt.x-makefile"