cow_mimetypes.erl.src 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. %% Copyright (c) 2013-2014, Loïc Hoguin <essen@ninenines.eu>
  2. %%
  3. %% Permission to use, copy, modify, and/or distribute this software for any
  4. %% purpose with or without fee is hereby granted, provided that the above
  5. %% copyright notice and this permission notice appear in all copies.
  6. %%
  7. %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. -module(cow_mimetypes).
  15. -export([all/1]).
  16. -export([web/1]).
  17. %% @doc Return the mimetype for any file by looking at its extension.
  18. -spec all(file:filename_all()) -> {binary(), binary(), []}.
  19. all(Path) ->
  20. case filename:extension(Path) of
  21. <<>> -> {<<"application">>, <<"octet-stream">>, []};
  22. << $., Ext/binary >> -> all_ext(Ext)
  23. end.
  24. %% @doc Return the mimetype for a Web related file by looking at its extension.
  25. -spec web(file:filename_all()) -> {binary(), binary(), []}.
  26. web(Path) ->
  27. case filename:extension(Path) of
  28. <<>> -> {<<"application">>, <<"octet-stream">>, []};
  29. << $., Ext/binary >> -> web_ext(Ext)
  30. end.
  31. %% Internal.
  32. %% GENERATED
  33. all_ext(_) -> {<<"application">>, <<"octet-stream">>, []}.
  34. web_ext(<<"css">>) -> {<<"text">>, <<"css">>, []};
  35. web_ext(<<"gif">>) -> {<<"image">>, <<"gif">>, []};
  36. web_ext(<<"html">>) -> {<<"text">>, <<"html">>, []};
  37. web_ext(<<"htm">>) -> {<<"text">>, <<"html">>, []};
  38. web_ext(<<"ico">>) -> {<<"image">>, <<"x-icon">>, []};
  39. web_ext(<<"jpeg">>) -> {<<"image">>, <<"jpeg">>, []};
  40. web_ext(<<"jpg">>) -> {<<"image">>, <<"jpeg">>, []};
  41. web_ext(<<"js">>) -> {<<"application">>, <<"javascript">>, []};
  42. web_ext(<<"mp3">>) -> {<<"audio">>, <<"mpeg">>, []};
  43. web_ext(<<"mp4">>) -> {<<"video">>, <<"mp4">>, []};
  44. web_ext(<<"ogg">>) -> {<<"audio">>, <<"ogg">>, []};
  45. web_ext(<<"ogv">>) -> {<<"video">>, <<"ogg">>, []};
  46. web_ext(<<"png">>) -> {<<"image">>, <<"png">>, []};
  47. web_ext(<<"svg">>) -> {<<"image">>, <<"svg+xml">>, []};
  48. web_ext(<<"wav">>) -> {<<"audio">>, <<"x-wav">>, []};
  49. web_ext(<<"webm">>) -> {<<"video">>, <<"webm">>, []};
  50. web_ext(_) -> {<<"application">>, <<"octet-stream">>, []}.