MIMEHelper.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. using System;
  2. using System.Collections;
  3. using System.IO;
  4. namespace LJLib.Tools.Utils
  5. {
  6. public static class MIMEHelper
  7. {
  8. public static string GetExType(byte[] filedata)
  9. {
  10. var fileHead = BitConverter.ToString(filedata, 0, 4).Replace("-", string.Empty).ToLower();
  11. if (fileHead.StartsWith("ffd8"))
  12. {
  13. return "jpg";
  14. }
  15. else if (fileHead.StartsWith("8950"))
  16. {
  17. return "png";
  18. }
  19. else if (fileHead.StartsWith("47494638"))
  20. {
  21. return "gif";
  22. }
  23. else if (fileHead.StartsWith("424d"))
  24. {
  25. return "bmp";
  26. }
  27. else
  28. {
  29. return string.Empty;
  30. }
  31. }
  32. private const string MIMEType_MSTNEF="application/ms-tnef";
  33. private const string Content_Transfer_Encoding_Tag="Content-Transfer-Encoding";
  34. public static string GetContentTransferEncoding(string strBuffer, int pos)
  35. {
  36. int begin = strBuffer.ToLower().IndexOf(Content_Transfer_Encoding_Tag.ToLower(), pos);
  37. if(begin != -1)
  38. {
  39. int end = strBuffer.ToLower().IndexOf("\r\n".ToLower(), begin + 1);
  40. return strBuffer.Substring(begin + Content_Transfer_Encoding_Tag.Length + 1, end - begin - Content_Transfer_Encoding_Tag.Length).Trim();
  41. }
  42. return "";
  43. }
  44. public static bool IsMSTNEF(string strContentType)
  45. {
  46. if(!string.IsNullOrEmpty(strContentType))
  47. if(strContentType.ToLower() == MIMEType_MSTNEF.ToLower())
  48. return true;
  49. else
  50. return false;
  51. return false;
  52. }
  53. public static string ContentType(string strExtension)
  54. {
  55. if(MIMETypeList.ContainsKey(strExtension))
  56. return MIMETypeList[strExtension].ToString();
  57. return "application/octet-stream";
  58. }
  59. private static Hashtable MIMETypeList { get; set; }
  60. static MIMEHelper()
  61. {
  62. MIMETypeList = new Hashtable
  63. {
  64. {".323", "text/h323"},
  65. {".3gp", "video/3gpp"},
  66. {".3gpp", "video/3gpp"},
  67. {".acp", "audio/x-mei-aac"},
  68. {".act", "text/xml"},
  69. {".actproj", "text/plain"},
  70. {".ade", "application/msaccess"},
  71. {".adp", "application/msaccess"},
  72. {".ai", "application/postscript"},
  73. {".aif", "audio/aiff"},
  74. {".aifc", "audio/aiff"},
  75. {".aiff", "audio/aiff"},
  76. {".asf", "video/x-ms-asf"},
  77. {".asm", "text/plain"},
  78. {".asx", "video/x-ms-asf"},
  79. {".au", "audio/basic"},
  80. {".avi", "video/avi"},
  81. {".bmp", "image/bmp"},
  82. {".bwp", "application/x-bwpreview"},
  83. {".c", "text/plain"},
  84. {".cat", "application/vnd.ms-pki.seccat"},
  85. {".cc", "text/plain"},
  86. {".cdf", "application/x-cdf"},
  87. {".cer", "application/x-x509-ca-cert"},
  88. {".cod", "text/plain"},
  89. {".cpp", "text/plain"},
  90. {".crl", "application/pkix-crl"},
  91. {".crt", "application/x-x509-ca-cert"},
  92. {".cs", "text/plain"},
  93. {".css", "text/css"},
  94. {".csv", "application/vnd.ms-excel"},
  95. {".cxx", "text/plain"},
  96. {".dbs", "text/plain"},
  97. {".def", "text/plain"},
  98. {".der", "application/x-x509-ca-cert"},
  99. {".dib", "image/bmp"},
  100. {".dif", "video/x-dv"},
  101. {".dll", "application/x-msdownload"},
  102. {".doc", "application/msword"},
  103. {".dot", "application/msword"},
  104. {".dsp", "text/plain"},
  105. {".dsw", "text/plain"},
  106. {".dv", "video/x-dv"},
  107. {".edn", "application/vnd.adobe.edn"},
  108. {".eml", "message/rfc822"},
  109. {".eps", "application/postscript"},
  110. {".etd", "application/x-ebx"},
  111. {".etp", "text/plain"},
  112. {".exe", "application/x-msdownload"},
  113. {".ext", "text/plain"},
  114. {".fdf", "application/vnd.fdf"},
  115. {".fif", "application/fractals"},
  116. {".fky", "text/plain"},
  117. {".gif", "image/gif"},
  118. {".gz", "application/x-gzip"},
  119. {".h", "text/plain"},
  120. {".hpp", "text/plain"},
  121. {".hqx", "application/mac-binhex40"},
  122. {".hta", "application/hta"},
  123. {".htc", "text/x-component"},
  124. {".htm", "text/html"},
  125. {".html", "text/html"},
  126. {".htt", "text/webviewhtml"},
  127. {".hxx", "text/plain"},
  128. {".i", "text/plain"},
  129. {".iad", "application/x-iad"},
  130. {".ico", "image/x-icon"},
  131. {".ics", "text/calendar"},
  132. {".idl", "text/plain"},
  133. {".iii", "application/x-iphone"},
  134. {".inc", "text/plain"},
  135. {".infopathxml", "application/ms-infopath.xml"},
  136. {".inl", "text/plain"},
  137. {".ins", "application/x-internet-signup"},
  138. {".iqy", "text/x-ms-iqy"},
  139. {".isp", "application/x-internet-signup"},
  140. {".java", "text/java"},
  141. {".jfif", "image/jpeg"},
  142. {".jnlp", "application/x-java-jnlp-file"},
  143. {".jpe", "image/jpeg"},
  144. {".jpeg", "image/jpeg"},
  145. {".jpg", "image/jpeg"},
  146. {".jsl", "text/plain"},
  147. {".kci", "text/plain"},
  148. {".la1", "audio/x-liquid-file"},
  149. {".lar", "application/x-laplayer-reg"},
  150. {".latex", "application/x-latex"},
  151. {".lavs", "audio/x-liquid-secure"},
  152. {".lgn", "text/plain"},
  153. {".lmsff", "audio/x-la-lms"},
  154. {".lqt", "audio/x-la-lqt"},
  155. {".lst", "text/plain"},
  156. {".m1v", "video/mpeg"},
  157. {".m3u", "audio/mpegurl"},
  158. {".m4e", "video/mpeg4"},
  159. {".MAC", "image/x-macpaint"},
  160. {".mak", "text/plain"},
  161. {".man", "application/x-troff-man"},
  162. {".map", "text/plain"},
  163. {".mda", "application/msaccess"},
  164. {".mdb", "application/msaccess"},
  165. {".mde", "application/msaccess"},
  166. {".mdi", "image/vnd.ms-modi"},
  167. {".mfp", "application/x-shockwave-flash"},
  168. {".mht", "message/rfc822"},
  169. {".mhtml", "message/rfc822"},
  170. {".mid", "audio/mid"},
  171. {".midi", "audio/mid"},
  172. {".mk", "text/plain"},
  173. {".mnd", "audio/x-musicnet-download"},
  174. {".mns", "audio/x-musicnet-stream"},
  175. {".MP1", "audio/mp1"},
  176. {".mp2", "video/mpeg"},
  177. {".mp2v", "video/mpeg"},
  178. {".mp3", "audio/mpeg"},
  179. {".mp4", "video/mp4"},
  180. {".mpa", "video/mpeg"},
  181. {".mpe", "video/mpeg"},
  182. {".mpeg", "video/mpeg"},
  183. {".mpf", "application/vnd.ms-mediapackage"},
  184. {".mpg", "video/mpeg"},
  185. {".mpg4", "video/mp4"},
  186. {".mpga", "audio/rn-mpeg"},
  187. {".mpv2", "video/mpeg"},
  188. {".NMW", "application/nmwb"},
  189. {".nws", "message/rfc822"},
  190. {".odc", "text/x-ms-odc"},
  191. {".odh", "text/plain"},
  192. {".odl", "text/plain"},
  193. {".p10", "application/pkcs10"},
  194. {".p12", "application/x-pkcs12"},
  195. {".p7b", "application/x-pkcs7-certificates"},
  196. {".p7c", "application/pkcs7-mime"},
  197. {".p7m", "application/pkcs7-mime"},
  198. {".p7r", "application/x-pkcs7-certreqresp"},
  199. {".p7s", "application/pkcs7-signature"},
  200. {".PCT", "image/pict"},
  201. {".pdf", "application/pdf"},
  202. {".pdx", "application/vnd.adobe.pdx"},
  203. {".pfx", "application/x-pkcs12"},
  204. {".pic", "image/pict"},
  205. {".PICT", "image/pict"},
  206. {".pko", "application/vnd.ms-pki.pko"},
  207. {".png", "image/png"},
  208. {".pnt", "image/x-macpaint"},
  209. {".pntg", "image/x-macpaint"},
  210. {".pot", "application/vnd.ms-powerpoint"},
  211. {".ppa", "application/vnd.ms-powerpoint"},
  212. {".pps", "application/vnd.ms-powerpoint"},
  213. {".ppt", "application/vnd.ms-powerpoint"},
  214. {".prc", "text/plain"},
  215. {".prf", "application/pics-rules"},
  216. {".ps", "application/postscript"},
  217. {".pub", "application/vnd.ms-publisher"},
  218. {".pwz", "application/vnd.ms-powerpoint"},
  219. {".qt", "video/quicktime"},
  220. {".qti", "image/x-quicktime"},
  221. {".qtif", "image/x-quicktime"},
  222. {".qtl", "application/x-quicktimeplayer"},
  223. {".qup", "application/x-quicktimeupdater"},
  224. {".r1m", "application/vnd.rn-recording"},
  225. {".r3t", "text/vnd.rn-realtext3d"},
  226. {".RA", "audio/vnd.rn-realaudio"},
  227. {".RAM", "audio/x-pn-realaudio"},
  228. {".rat", "application/rat-file"},
  229. {".rc", "text/plain"},
  230. {".rc2", "text/plain"},
  231. {".rct", "text/plain"},
  232. {".rec", "application/vnd.rn-recording"},
  233. {".rgs", "text/plain"},
  234. {".rjs", "application/vnd.rn-realsystem-rjs"},
  235. {".rjt", "application/vnd.rn-realsystem-rjt"},
  236. {".RM", "application/vnd.rn-realmedia"},
  237. {".rmf", "application/vnd.adobe.rmf"},
  238. {".rmi", "audio/mid"},
  239. {".RMJ", "application/vnd.rn-realsystem-rmj"},
  240. {".RMM", "audio/x-pn-realaudio"},
  241. {".rms", "application/vnd.rn-realmedia-secure"},
  242. {".rmvb", "application/vnd.rn-realmedia-vbr"},
  243. {".RMX", "application/vnd.rn-realsystem-rmx"},
  244. {".RNX", "application/vnd.rn-realplayer"},
  245. {".rp", "image/vnd.rn-realpix"},
  246. {".RPM", "audio/x-pn-realaudio-plugin"},
  247. {".rqy", "text/x-ms-rqy"},
  248. {".rsml", "application/vnd.rn-rsml"},
  249. {".rt", "text/vnd.rn-realtext"},
  250. {".rtf", "application/msword"},
  251. {".rul", "text/plain"},
  252. {".RV", "video/vnd.rn-realvideo"},
  253. {".s", "text/plain"},
  254. {".sc2", "application/schdpl32"},
  255. {".scd", "application/schdpl32"},
  256. {".sch", "application/schdpl32"},
  257. {".sct", "text/scriptlet"},
  258. {".sd2", "audio/x-sd2"},
  259. {".sdp", "application/sdp"},
  260. {".sit", "application/x-stuffit"},
  261. {".slk", "application/vnd.ms-excel"},
  262. {".sln", "application/octet-stream"},
  263. {".SMI", "application/smil"},
  264. {".smil", "application/smil"},
  265. {".snd", "audio/basic"},
  266. {".snp", "application/msaccess"},
  267. {".spc", "application/x-pkcs7-certificates"},
  268. {".spl", "application/futuresplash"},
  269. {".sql", "text/plain"},
  270. {".srf", "text/plain"},
  271. {".ssm", "application/streamingmedia"},
  272. {".sst", "application/vnd.ms-pki.certstore"},
  273. {".stl", "application/vnd.ms-pki.stl"},
  274. {".swf", "application/x-shockwave-flash"},
  275. {".tab", "text/plain"},
  276. {".tar", "application/x-tar"},
  277. {".tdl", "text/xml"},
  278. {".tgz", "application/x-compressed"},
  279. {".tif", "image/tiff"},
  280. {".tiff", "image/tiff"},
  281. {".tlh", "text/plain"},
  282. {".tli", "text/plain"},
  283. {".torrent", "application/x-bittorrent"},
  284. {".trg", "text/plain"},
  285. {".txt", "text/plain"},
  286. {".udf", "text/plain"},
  287. {".udt", "text/plain"},
  288. {".uls", "text/iuls"},
  289. {".user", "text/plain"},
  290. {".usr", "text/plain"},
  291. {".vb", "text/plain"},
  292. {".vcf", "text/x-vcard"},
  293. {".vcproj", "text/plain"},
  294. {".viw", "text/plain"},
  295. {".vpg", "application/x-vpeg005"},
  296. {".vspscc", "text/plain"},
  297. {".vsscc", "text/plain"},
  298. {".vssscc", "text/plain"},
  299. {".wav", "audio/wav"},
  300. {".wax", "audio/x-ms-wax"},
  301. {".wbk", "application/msword"},
  302. {".wiz", "application/msword"},
  303. {".wm", "video/x-ms-wm"},
  304. {".wma", "audio/x-ms-wma"},
  305. {".wmd", "application/x-ms-wmd"},
  306. {".wmv", "video/x-ms-wmv"},
  307. {".wmx", "video/x-ms-wmx"},
  308. {".wmz", "application/x-ms-wmz"},
  309. {".wpl", "application/vnd.ms-wpl"},
  310. {".wprj", "application/webzip"},
  311. {".wsc", "text/scriptlet"},
  312. {".wvx", "video/x-ms-wvx"},
  313. {".XBM", "image/x-xbitmap"},
  314. {".xdp", "application/vnd.adobe.xdp+xml"},
  315. {".xfd", "application/vnd.adobe.xfd+xml"},
  316. {".xfdf", "application/vnd.adobe.xfdf"},
  317. {".xla", "application/vnd.ms-excel"},
  318. {".xlb", "application/vnd.ms-excel"},
  319. {".xlc", "application/vnd.ms-excel"},
  320. {".xld", "application/vnd.ms-excel"},
  321. {".xlk", "application/vnd.ms-excel"},
  322. {".xll", "application/vnd.ms-excel"},
  323. {".xlm", "application/vnd.ms-excel"},
  324. {".xls", "application/vnd.ms-excel"},
  325. {".xlt", "application/vnd.ms-excel"},
  326. {".xlv", "application/vnd.ms-excel"},
  327. {".xlw", "application/vnd.ms-excel"},
  328. {".xml", "text/xml"},
  329. {".xpl", "audio/scpls"},
  330. {".xsl", "text/xml"},
  331. {".z", "application/x-compress"},
  332. {".zip", "application/x-zip-compressed"},
  333. {".js", "application/x-javascript"},
  334. {".apk", "application/vnd.android.package-archive"},
  335. {".ipa", "application/vnd.iphone"}
  336. };
  337. }
  338. /// <summary>Returns the MIME content-type for the supplied file extension</summary>
  339. /// <returns>string MIME type (Example: \"text/plain\")</returns>
  340. public static string GetMimeType(string strFileName)
  341. {
  342. try
  343. {
  344. string strFileExtension=new FileInfo(strFileName).Extension;
  345. string strContentType;
  346. bool MONO = Environment.OSVersion.Platform == PlatformID.Unix;
  347. if(MONO)
  348. {
  349. strContentType=ContentType(strFileExtension);
  350. }
  351. else
  352. {
  353. Microsoft.Win32.RegistryKey extKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(strFileExtension);
  354. strContentType = (string)extKey.GetValue("Content Type");
  355. }
  356. if (strContentType != null)
  357. {
  358. return strContentType;
  359. }
  360. return "application/octet-stream";
  361. }
  362. catch(Exception)
  363. {
  364. return "application/octet-stream";
  365. }
  366. }
  367. }
  368. }