MimeTypes.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System;
  2. using System.IO;
  3. namespace MediaBrowser.Common.Net
  4. {
  5. public static class MimeTypes
  6. {
  7. public static string GetMimeType(string path)
  8. {
  9. string ext = Path.GetExtension(path);
  10. // http://en.wikipedia.org/wiki/Internet_media_type
  11. // Add more as needed
  12. // Type video
  13. if (ext.EndsWith("mpg", StringComparison.OrdinalIgnoreCase) || ext.EndsWith("mpeg", StringComparison.OrdinalIgnoreCase))
  14. {
  15. return "video/mpeg";
  16. }
  17. else if (ext.EndsWith("mp4", StringComparison.OrdinalIgnoreCase))
  18. {
  19. return "video/mp4";
  20. }
  21. else if (ext.EndsWith("ogv", StringComparison.OrdinalIgnoreCase))
  22. {
  23. return "video/ogg";
  24. }
  25. else if (ext.EndsWith("mov", StringComparison.OrdinalIgnoreCase))
  26. {
  27. return "video/quicktime";
  28. }
  29. else if (ext.EndsWith("webm", StringComparison.OrdinalIgnoreCase))
  30. {
  31. return "video/webm";
  32. }
  33. else if (ext.EndsWith("mkv", StringComparison.OrdinalIgnoreCase))
  34. {
  35. return "video/x-matroska";
  36. }
  37. else if (ext.EndsWith("wmv", StringComparison.OrdinalIgnoreCase))
  38. {
  39. return "video/x-ms-wmv";
  40. }
  41. else if (ext.EndsWith("flv", StringComparison.OrdinalIgnoreCase))
  42. {
  43. return "video/x-flv";
  44. }
  45. else if (ext.EndsWith("avi", StringComparison.OrdinalIgnoreCase))
  46. {
  47. return "video/avi";
  48. }
  49. else if (ext.EndsWith("m4v", StringComparison.OrdinalIgnoreCase))
  50. {
  51. return "video/x-m4v";
  52. }
  53. // Type text
  54. else if (ext.EndsWith("css", StringComparison.OrdinalIgnoreCase))
  55. {
  56. return "text/css";
  57. }
  58. else if (ext.EndsWith("csv", StringComparison.OrdinalIgnoreCase))
  59. {
  60. return "text/csv";
  61. }
  62. else if (ext.EndsWith("html", StringComparison.OrdinalIgnoreCase) || ext.EndsWith("html", StringComparison.OrdinalIgnoreCase))
  63. {
  64. return "text/html";
  65. }
  66. else if (ext.EndsWith("txt", StringComparison.OrdinalIgnoreCase))
  67. {
  68. return "text/plain";
  69. }
  70. // Type image
  71. else if (ext.EndsWith("gif", StringComparison.OrdinalIgnoreCase))
  72. {
  73. return "image/gif";
  74. }
  75. else if (ext.EndsWith("jpg", StringComparison.OrdinalIgnoreCase) || ext.EndsWith("jpeg", StringComparison.OrdinalIgnoreCase))
  76. {
  77. return "image/jpeg";
  78. }
  79. else if (ext.EndsWith("png", StringComparison.OrdinalIgnoreCase))
  80. {
  81. return "image/png";
  82. }
  83. else if (ext.EndsWith("ico", StringComparison.OrdinalIgnoreCase))
  84. {
  85. return "image/vnd.microsoft.icon";
  86. }
  87. // Type audio
  88. else if (ext.EndsWith("mp3", StringComparison.OrdinalIgnoreCase))
  89. {
  90. return "audio/mpeg";
  91. }
  92. else if (ext.EndsWith("m4a", StringComparison.OrdinalIgnoreCase) || ext.EndsWith("aac", StringComparison.OrdinalIgnoreCase))
  93. {
  94. return "audio/mp4";
  95. }
  96. else if (ext.EndsWith("webma", StringComparison.OrdinalIgnoreCase))
  97. {
  98. return "audio/webm";
  99. }
  100. else if (ext.EndsWith("wav", StringComparison.OrdinalIgnoreCase))
  101. {
  102. return "audio/wav";
  103. }
  104. else if (ext.EndsWith("ogg", StringComparison.OrdinalIgnoreCase) || ext.EndsWith("oga", StringComparison.OrdinalIgnoreCase))
  105. {
  106. return "audio/ogg";
  107. }
  108. throw new InvalidOperationException("Argument not supported: " + path);
  109. }
  110. }
  111. }