MIMEHelper.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. {".svg", "image/svg+xml"},
  132. {".ics", "text/calendar"},
  133. {".idl", "text/plain"},
  134. {".iii", "application/x-iphone"},
  135. {".inc", "text/plain"},
  136. {".infopathxml", "application/ms-infopath.xml"},
  137. {".inl", "text/plain"},
  138. {".ins", "application/x-internet-signup"},
  139. {".iqy", "text/x-ms-iqy"},
  140. {".isp", "application/x-internet-signup"},
  141. {".java", "text/java"},
  142. {".jfif", "image/jpeg"},
  143. {".jnlp", "application/x-java-jnlp-file"},
  144. {".jpe", "image/jpeg"},
  145. {".jpeg", "image/jpeg"},
  146. {".jpg", "image/jpeg"},
  147. {".jsl", "text/plain"},
  148. {".kci", "text/plain"},
  149. {".la1", "audio/x-liquid-file"},
  150. {".lar", "application/x-laplayer-reg"},
  151. {".latex", "application/x-latex"},
  152. {".lavs", "audio/x-liquid-secure"},
  153. {".lgn", "text/plain"},
  154. {".lmsff", "audio/x-la-lms"},
  155. {".lqt", "audio/x-la-lqt"},
  156. {".lst", "text/plain"},
  157. {".m1v", "video/mpeg"},
  158. {".m3u", "audio/mpegurl"},
  159. {".m4e", "video/mpeg4"},
  160. {".MAC", "image/x-macpaint"},
  161. {".mak", "text/plain"},
  162. {".man", "application/x-troff-man"},
  163. {".map", "text/plain"},
  164. {".mda", "application/msaccess"},
  165. {".mdb", "application/msaccess"},
  166. {".mde", "application/msaccess"},
  167. {".mdi", "image/vnd.ms-modi"},
  168. {".mfp", "application/x-shockwave-flash"},
  169. {".mht", "message/rfc822"},
  170. {".mhtml", "message/rfc822"},
  171. {".mid", "audio/mid"},
  172. {".midi", "audio/mid"},
  173. {".mk", "text/plain"},
  174. {".mnd", "audio/x-musicnet-download"},
  175. {".mns", "audio/x-musicnet-stream"},
  176. {".MP1", "audio/mp1"},
  177. {".mp2", "video/mpeg"},
  178. {".mp2v", "video/mpeg"},
  179. {".mp3", "audio/mpeg"},
  180. {".mp4", "video/mp4"},
  181. {".mpa", "video/mpeg"},
  182. {".mpe", "video/mpeg"},
  183. {".mpeg", "video/mpeg"},
  184. {".mpf", "application/vnd.ms-mediapackage"},
  185. {".mpg", "video/mpeg"},
  186. {".mpg4", "video/mp4"},
  187. {".mpga", "audio/rn-mpeg"},
  188. {".mpv2", "video/mpeg"},
  189. {".NMW", "application/nmwb"},
  190. {".nws", "message/rfc822"},
  191. {".odc", "text/x-ms-odc"},
  192. {".odh", "text/plain"},
  193. {".odl", "text/plain"},
  194. {".p10", "application/pkcs10"},
  195. {".p12", "application/x-pkcs12"},
  196. {".p7b", "application/x-pkcs7-certificates"},
  197. {".p7c", "application/pkcs7-mime"},
  198. {".p7m", "application/pkcs7-mime"},
  199. {".p7r", "application/x-pkcs7-certreqresp"},
  200. {".p7s", "application/pkcs7-signature"},
  201. {".PCT", "image/pict"},
  202. {".pdf", "application/pdf"},
  203. {".pdx", "application/vnd.adobe.pdx"},
  204. {".pfx", "application/x-pkcs12"},
  205. {".pic", "image/pict"},
  206. {".PICT", "image/pict"},
  207. {".pko", "application/vnd.ms-pki.pko"},
  208. {".png", "image/png"},
  209. {".pnt", "image/x-macpaint"},
  210. {".pntg", "image/x-macpaint"},
  211. {".pot", "application/vnd.ms-powerpoint"},
  212. {".ppa", "application/vnd.ms-powerpoint"},
  213. {".pps", "application/vnd.ms-powerpoint"},
  214. {".ppt", "application/vnd.ms-powerpoint"},
  215. {".prc", "text/plain"},
  216. {".prf", "application/pics-rules"},
  217. {".ps", "application/postscript"},
  218. {".pub", "application/vnd.ms-publisher"},
  219. {".pwz", "application/vnd.ms-powerpoint"},
  220. {".qt", "video/quicktime"},
  221. {".qti", "image/x-quicktime"},
  222. {".qtif", "image/x-quicktime"},
  223. {".qtl", "application/x-quicktimeplayer"},
  224. {".qup", "application/x-quicktimeupdater"},
  225. {".r1m", "application/vnd.rn-recording"},
  226. {".r3t", "text/vnd.rn-realtext3d"},
  227. {".RA", "audio/vnd.rn-realaudio"},
  228. {".RAM", "audio/x-pn-realaudio"},
  229. {".rat", "application/rat-file"},
  230. {".rc", "text/plain"},
  231. {".rc2", "text/plain"},
  232. {".rct", "text/plain"},
  233. {".rec", "application/vnd.rn-recording"},
  234. {".rgs", "text/plain"},
  235. {".rjs", "application/vnd.rn-realsystem-rjs"},
  236. {".rjt", "application/vnd.rn-realsystem-rjt"},
  237. {".RM", "application/vnd.rn-realmedia"},
  238. {".rmf", "application/vnd.adobe.rmf"},
  239. {".rmi", "audio/mid"},
  240. {".RMJ", "application/vnd.rn-realsystem-rmj"},
  241. {".RMM", "audio/x-pn-realaudio"},
  242. {".rms", "application/vnd.rn-realmedia-secure"},
  243. {".rmvb", "application/vnd.rn-realmedia-vbr"},
  244. {".RMX", "application/vnd.rn-realsystem-rmx"},
  245. {".RNX", "application/vnd.rn-realplayer"},
  246. {".rp", "image/vnd.rn-realpix"},
  247. {".RPM", "audio/x-pn-realaudio-plugin"},
  248. {".rqy", "text/x-ms-rqy"},
  249. {".rsml", "application/vnd.rn-rsml"},
  250. {".rt", "text/vnd.rn-realtext"},
  251. {".rtf", "application/msword"},
  252. {".rul", "text/plain"},
  253. {".RV", "video/vnd.rn-realvideo"},
  254. {".s", "text/plain"},
  255. {".sc2", "application/schdpl32"},
  256. {".scd", "application/schdpl32"},
  257. {".sch", "application/schdpl32"},
  258. {".sct", "text/scriptlet"},
  259. {".sd2", "audio/x-sd2"},
  260. {".sdp", "application/sdp"},
  261. {".sit", "application/x-stuffit"},
  262. {".slk", "application/vnd.ms-excel"},
  263. {".sln", "application/octet-stream"},
  264. {".SMI", "application/smil"},
  265. {".smil", "application/smil"},
  266. {".snd", "audio/basic"},
  267. {".snp", "application/msaccess"},
  268. {".spc", "application/x-pkcs7-certificates"},
  269. {".spl", "application/futuresplash"},
  270. {".sql", "text/plain"},
  271. {".srf", "text/plain"},
  272. {".ssm", "application/streamingmedia"},
  273. {".sst", "application/vnd.ms-pki.certstore"},
  274. {".stl", "application/vnd.ms-pki.stl"},
  275. {".swf", "application/x-shockwave-flash"},
  276. {".tab", "text/plain"},
  277. {".tar", "application/x-tar"},
  278. {".tdl", "text/xml"},
  279. {".tgz", "application/x-compressed"},
  280. {".tif", "image/tiff"},
  281. {".tiff", "image/tiff"},
  282. {".tlh", "text/plain"},
  283. {".tli", "text/plain"},
  284. {".torrent", "application/x-bittorrent"},
  285. {".trg", "text/plain"},
  286. {".txt", "text/plain"},
  287. {".udf", "text/plain"},
  288. {".udt", "text/plain"},
  289. {".uls", "text/iuls"},
  290. {".user", "text/plain"},
  291. {".usr", "text/plain"},
  292. {".vb", "text/plain"},
  293. {".vcf", "text/x-vcard"},
  294. {".vcproj", "text/plain"},
  295. {".viw", "text/plain"},
  296. {".vpg", "application/x-vpeg005"},
  297. {".vspscc", "text/plain"},
  298. {".vsscc", "text/plain"},
  299. {".vssscc", "text/plain"},
  300. {".wav", "audio/wav"},
  301. {".wax", "audio/x-ms-wax"},
  302. {".wbk", "application/msword"},
  303. {".wiz", "application/msword"},
  304. {".wm", "video/x-ms-wm"},
  305. {".wma", "audio/x-ms-wma"},
  306. {".wmd", "application/x-ms-wmd"},
  307. {".wmv", "video/x-ms-wmv"},
  308. {".wmx", "video/x-ms-wmx"},
  309. {".wmz", "application/x-ms-wmz"},
  310. {".wpl", "application/vnd.ms-wpl"},
  311. {".wprj", "application/webzip"},
  312. {".wsc", "text/scriptlet"},
  313. {".wvx", "video/x-ms-wvx"},
  314. {".XBM", "image/x-xbitmap"},
  315. {".xdp", "application/vnd.adobe.xdp+xml"},
  316. {".xfd", "application/vnd.adobe.xfd+xml"},
  317. {".xfdf", "application/vnd.adobe.xfdf"},
  318. {".xla", "application/vnd.ms-excel"},
  319. {".xlb", "application/vnd.ms-excel"},
  320. {".xlc", "application/vnd.ms-excel"},
  321. {".xld", "application/vnd.ms-excel"},
  322. {".xlk", "application/vnd.ms-excel"},
  323. {".xll", "application/vnd.ms-excel"},
  324. {".xlm", "application/vnd.ms-excel"},
  325. {".xls", "application/vnd.ms-excel"},
  326. {".xlt", "application/vnd.ms-excel"},
  327. {".xlv", "application/vnd.ms-excel"},
  328. {".xlw", "application/vnd.ms-excel"},
  329. {".xml", "text/xml"},
  330. {".xpl", "audio/scpls"},
  331. {".xsl", "text/xml"},
  332. {".z", "application/x-compress"},
  333. {".zip", "application/x-zip-compressed"},
  334. {".js", "application/x-javascript"},
  335. {".apk", "application/vnd.android.package-archive"},
  336. {".ipa", "application/vnd.iphone"}
  337. };
  338. }
  339. /// <summary>Returns the MIME content-type for the supplied file extension</summary>
  340. /// <returns>string MIME type (Example: \"text/plain\")</returns>
  341. public static string GetMimeType(string strFileName)
  342. {
  343. try
  344. {
  345. string strFileExtension=new FileInfo(strFileName).Extension;
  346. string strContentType;
  347. bool MONO = Environment.OSVersion.Platform == PlatformID.Unix;
  348. if(MONO)
  349. {
  350. strContentType=ContentType(strFileExtension);
  351. }
  352. else
  353. {
  354. Microsoft.Win32.RegistryKey extKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(strFileExtension);
  355. strContentType = (string)extKey.GetValue("Content Type");
  356. }
  357. if (strContentType != null)
  358. {
  359. return strContentType;
  360. }
  361. return "application/octet-stream";
  362. }
  363. catch(Exception)
  364. {
  365. return "application/octet-stream";
  366. }
  367. }
  368. }
  369. }