mime.lua 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. -- Copyright 2008 Freifunk Leipzig / Jo-Philipp Wich <jow@openwrt.org>
  2. -- Licensed to the public under the Apache License 2.0.
  3. -- This class provides functions to guess mime types from file extensions and
  4. -- vice versa.
  5. module("luci.http.protocol.mime", package.seeall)
  6. require("luci.util")
  7. MIME_TYPES = {
  8. ["txt"] = "text/plain";
  9. ["js"] = "text/javascript";
  10. ["css"] = "text/css";
  11. ["htm"] = "text/html";
  12. ["html"] = "text/html";
  13. ["patch"] = "text/x-patch";
  14. ["c"] = "text/x-csrc";
  15. ["h"] = "text/x-chdr";
  16. ["o"] = "text/x-object";
  17. ["ko"] = "text/x-object";
  18. ["bmp"] = "image/bmp";
  19. ["gif"] = "image/gif";
  20. ["png"] = "image/png";
  21. ["jpg"] = "image/jpeg";
  22. ["jpeg"] = "image/jpeg";
  23. ["svg"] = "image/svg+xml";
  24. ["zip"] = "application/zip";
  25. ["pdf"] = "application/pdf";
  26. ["xml"] = "application/xml";
  27. ["xsl"] = "application/xml";
  28. ["doc"] = "application/msword";
  29. ["ppt"] = "application/vnd.ms-powerpoint";
  30. ["xls"] = "application/vnd.ms-excel";
  31. ["odt"] = "application/vnd.oasis.opendocument.text";
  32. ["odp"] = "application/vnd.oasis.opendocument.presentation";
  33. ["pl"] = "application/x-perl";
  34. ["sh"] = "application/x-shellscript";
  35. ["php"] = "application/x-php";
  36. ["deb"] = "application/x-deb";
  37. ["iso"] = "application/x-cd-image";
  38. ["tgz"] = "application/x-compressed-tar";
  39. ["mp3"] = "audio/mpeg";
  40. ["ogg"] = "audio/x-vorbis+ogg";
  41. ["wav"] = "audio/x-wav";
  42. ["mpg"] = "video/mpeg";
  43. ["mpeg"] = "video/mpeg";
  44. ["avi"] = "video/x-msvideo";
  45. }
  46. -- "application/octet-stream" if the extension is unknown.
  47. function to_mime(filename)
  48. if type(filename) == "string" then
  49. local ext = filename:match("[^%.]+$")
  50. if ext and MIME_TYPES[ext:lower()] then
  51. return MIME_TYPES[ext:lower()]
  52. end
  53. end
  54. return "application/octet-stream"
  55. end
  56. -- given mime-type is unknown.
  57. function to_ext(mimetype)
  58. if type(mimetype) == "string" then
  59. for ext, type in luci.util.kspairs( MIME_TYPES ) do
  60. if type == mimetype then
  61. return ext
  62. end
  63. end
  64. end
  65. return nil
  66. end