FileHelper.cs 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  1. using System.Globalization;
  2. using System.Security.Cryptography;
  3. using System.Text;
  4. using System.Xml.Serialization;
  5. using WingServerCommon.Config;
  6. using WingServerCommon.Config.Parameters;
  7. namespace WingServerCommon.Interfaces.FileTransfer
  8. {
  9. /// <summary>
  10. /// 文件帮助类
  11. /// </summary>
  12. public class FileHelper
  13. {
  14. #region MimeType
  15. private static readonly IDictionary<string, string> _mappings = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase) {
  16. #region Big freaking list of mime types
  17. // combination of values from Windows 7 Registry and
  18. // from C:\Windows\System32\inetsrv\config\applicationHost.config
  19. // some added, including .7z and .dat
  20. {".323", "text/h323"},
  21. {".3g2", "video/3gpp2"},
  22. {".3gp", "video/3gpp"},
  23. {".3gp2", "video/3gpp2"},
  24. {".3gpp", "video/3gpp"},
  25. {".7z", "application/x-7z-compressed"},
  26. {".aa", "audio/audible"},
  27. {".AAC", "audio/aac"},
  28. {".aaf", "application/octet-stream"},
  29. {".aax", "audio/vnd.audible.aax"},
  30. {".ac3", "audio/ac3"},
  31. {".aca", "application/octet-stream"},
  32. {".accda", "application/msaccess.addin"},
  33. {".accdb", "application/msaccess"},
  34. {".accdc", "application/msaccess.cab"},
  35. {".accde", "application/msaccess"},
  36. {".accdr", "application/msaccess.runtime"},
  37. {".accdt", "application/msaccess"},
  38. {".accdw", "application/msaccess.webapplication"},
  39. {".accft", "application/msaccess.ftemplate"},
  40. {".acx", "application/internet-property-stream"},
  41. {".AddIn", "text/xml"},
  42. {".ade", "application/msaccess"},
  43. {".adobebridge", "application/x-bridge-url"},
  44. {".adp", "application/msaccess"},
  45. {".ADT", "audio/vnd.dlna.adts"},
  46. {".ADTS", "audio/aac"},
  47. {".afm", "application/octet-stream"},
  48. {".ai", "application/postscript"},
  49. {".aif", "audio/x-aiff"},
  50. {".aifc", "audio/aiff"},
  51. {".aiff", "audio/aiff"},
  52. {".air", "application/vnd.adobe.air-application-installer-package+zip"},
  53. {".amc", "application/x-mpeg"},
  54. {".application", "application/x-ms-application"},
  55. {".art", "image/x-jg"},
  56. {".asa", "application/xml"},
  57. {".asax", "application/xml"},
  58. {".ascx", "application/xml"},
  59. {".asd", "application/octet-stream"},
  60. {".asf", "video/x-ms-asf"},
  61. {".ashx", "application/xml"},
  62. {".asi", "application/octet-stream"},
  63. {".asm", "text/plain"},
  64. {".asmx", "application/xml"},
  65. {".aspx", "application/xml"},
  66. {".asr", "video/x-ms-asf"},
  67. {".asx", "video/x-ms-asf"},
  68. {".atom", "application/atom+xml"},
  69. {".au", "audio/basic"},
  70. {".avi", "video/x-msvideo"},
  71. {".axs", "application/olescript"},
  72. {".bas", "text/plain"},
  73. {".bcpio", "application/x-bcpio"},
  74. {".bin", "application/octet-stream"},
  75. {".bmp", "image/bmp"},
  76. {".c", "text/plain"},
  77. {".cab", "application/octet-stream"},
  78. {".caf", "audio/x-caf"},
  79. {".calx", "application/vnd.ms-office.calx"},
  80. {".cat", "application/vnd.ms-pki.seccat"},
  81. {".cc", "text/plain"},
  82. {".cd", "text/plain"},
  83. {".cdda", "audio/aiff"},
  84. {".cdf", "application/x-cdf"},
  85. {".cer", "application/x-x509-ca-cert"},
  86. {".chm", "application/octet-stream"},
  87. {".class", "application/x-java-applet"},
  88. {".clp", "application/x-msclip"},
  89. {".cmx", "image/x-cmx"},
  90. {".cnf", "text/plain"},
  91. {".cod", "image/cis-cod"},
  92. {".config", "application/xml"},
  93. {".contact", "text/x-ms-contact"},
  94. {".coverage", "application/xml"},
  95. {".cpio", "application/x-cpio"},
  96. {".cpp", "text/plain"},
  97. {".crd", "application/x-mscardfile"},
  98. {".crl", "application/pkix-crl"},
  99. {".crt", "application/x-x509-ca-cert"},
  100. {".cs", "text/plain"},
  101. {".csdproj", "text/plain"},
  102. {".csh", "application/x-csh"},
  103. {".csproj", "text/plain"},
  104. {".css", "text/css"},
  105. {".csv", "text/csv"},
  106. {".cur", "application/octet-stream"},
  107. {".cxx", "text/plain"},
  108. {".dat", "application/octet-stream"},
  109. {".datasource", "application/xml"},
  110. {".dbproj", "text/plain"},
  111. {".dcr", "application/x-director"},
  112. {".def", "text/plain"},
  113. {".deploy", "application/octet-stream"},
  114. {".der", "application/x-x509-ca-cert"},
  115. {".dgml", "application/xml"},
  116. {".dib", "image/bmp"},
  117. {".dif", "video/x-dv"},
  118. {".dir", "application/x-director"},
  119. {".disco", "text/xml"},
  120. {".dll", "application/x-msdownload"},
  121. {".dll.config", "text/xml"},
  122. {".dlm", "text/dlm"},
  123. {".doc", "application/msword"},
  124. {".docm", "application/vnd.ms-word.document.macroEnabled.12"},
  125. {".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},
  126. {".dot", "application/msword"},
  127. {".dotm", "application/vnd.ms-word.template.macroEnabled.12"},
  128. {".dotx", "application/vnd.openxmlformats-officedocument.wordprocessingml.template"},
  129. {".dsp", "application/octet-stream"},
  130. {".dsw", "text/plain"},
  131. {".dtd", "text/xml"},
  132. {".dtsConfig", "text/xml"},
  133. {".dv", "video/x-dv"},
  134. {".dvi", "application/x-dvi"},
  135. {".dwf", "drawing/x-dwf"},
  136. {".dwp", "application/octet-stream"},
  137. {".dxr", "application/x-director"},
  138. {".eml", "message/rfc822"},
  139. {".emz", "application/octet-stream"},
  140. {".eot", "application/octet-stream"},
  141. {".eps", "application/postscript"},
  142. {".etl", "application/etl"},
  143. {".etx", "text/x-setext"},
  144. {".evy", "application/envoy"},
  145. {".exe", "application/octet-stream"},
  146. {".exe.config", "text/xml"},
  147. {".fdf", "application/vnd.fdf"},
  148. {".fif", "application/fractals"},
  149. {".filters", "Application/xml"},
  150. {".fla", "application/octet-stream"},
  151. {".flr", "x-world/x-vrml"},
  152. {".flv", "video/x-flv"},
  153. {".fsscript", "application/fsharp-script"},
  154. {".fsx", "application/fsharp-script"},
  155. {".generictest", "application/xml"},
  156. {".gif", "image/gif"},
  157. {".group", "text/x-ms-group"},
  158. {".gsm", "audio/x-gsm"},
  159. {".gtar", "application/x-gtar"},
  160. {".gz", "application/x-gzip"},
  161. {".h", "text/plain"},
  162. {".hdf", "application/x-hdf"},
  163. {".hdml", "text/x-hdml"},
  164. {".hhc", "application/x-oleobject"},
  165. {".hhk", "application/octet-stream"},
  166. {".hhp", "application/octet-stream"},
  167. {".hlp", "application/winhlp"},
  168. {".hpp", "text/plain"},
  169. {".hqx", "application/mac-binhex40"},
  170. {".hta", "application/hta"},
  171. {".htc", "text/x-component"},
  172. {".htm", "text/html"},
  173. {".html", "text/html"},
  174. {".htt", "text/webviewhtml"},
  175. {".hxa", "application/xml"},
  176. {".hxc", "application/xml"},
  177. {".hxd", "application/octet-stream"},
  178. {".hxe", "application/xml"},
  179. {".hxf", "application/xml"},
  180. {".hxh", "application/octet-stream"},
  181. {".hxi", "application/octet-stream"},
  182. {".hxk", "application/xml"},
  183. {".hxq", "application/octet-stream"},
  184. {".hxr", "application/octet-stream"},
  185. {".hxs", "application/octet-stream"},
  186. {".hxt", "text/html"},
  187. {".hxv", "application/xml"},
  188. {".hxw", "application/octet-stream"},
  189. {".hxx", "text/plain"},
  190. {".i", "text/plain"},
  191. {".ico", "image/x-icon"},
  192. {".ics", "application/octet-stream"},
  193. {".idl", "text/plain"},
  194. {".ief", "image/ief"},
  195. {".iii", "application/x-iphone"},
  196. {".inc", "text/plain"},
  197. {".inf", "application/octet-stream"},
  198. {".inl", "text/plain"},
  199. {".ins", "application/x-internet-signup"},
  200. {".ipa", "application/x-itunes-ipa"},
  201. {".ipg", "application/x-itunes-ipg"},
  202. {".ipproj", "text/plain"},
  203. {".ipsw", "application/x-itunes-ipsw"},
  204. {".iqy", "text/x-ms-iqy"},
  205. {".isp", "application/x-internet-signup"},
  206. {".ite", "application/x-itunes-ite"},
  207. {".itlp", "application/x-itunes-itlp"},
  208. {".itms", "application/x-itunes-itms"},
  209. {".itpc", "application/x-itunes-itpc"},
  210. {".IVF", "video/x-ivf"},
  211. {".jar", "application/java-archive"},
  212. {".java", "application/octet-stream"},
  213. {".jck", "application/liquidmotion"},
  214. {".jcz", "application/liquidmotion"},
  215. {".jfif", "image/pjpeg"},
  216. {".jnlp", "application/x-java-jnlp-file"},
  217. {".jpb", "application/octet-stream"},
  218. {".jpe", "image/jpeg"},
  219. {".jpeg", "image/jpeg"},
  220. {".jpg", "image/jpeg"},
  221. {".js", "application/x-javascript"},
  222. {".json", "application/json"},
  223. {".jsx", "text/jscript"},
  224. {".jsxbin", "text/plain"},
  225. {".latex", "application/x-latex"},
  226. {".library-ms", "application/windows-library+xml"},
  227. {".lit", "application/x-ms-reader"},
  228. {".loadtest", "application/xml"},
  229. {".lpk", "application/octet-stream"},
  230. {".lsf", "video/x-la-asf"},
  231. {".lst", "text/plain"},
  232. {".lsx", "video/x-la-asf"},
  233. {".lzh", "application/octet-stream"},
  234. {".m13", "application/x-msmediaview"},
  235. {".m14", "application/x-msmediaview"},
  236. {".m1v", "video/mpeg"},
  237. {".m2t", "video/vnd.dlna.mpeg-tts"},
  238. {".m2ts", "video/vnd.dlna.mpeg-tts"},
  239. {".m2v", "video/mpeg"},
  240. {".m3u", "audio/x-mpegurl"},
  241. {".m3u8", "audio/x-mpegurl"},
  242. {".m4a", "audio/m4a"},
  243. {".m4b", "audio/m4b"},
  244. {".m4p", "audio/m4p"},
  245. {".m4r", "audio/x-m4r"},
  246. {".m4v", "video/x-m4v"},
  247. {".mac", "image/x-macpaint"},
  248. {".mak", "text/plain"},
  249. {".man", "application/x-troff-man"},
  250. {".manifest", "application/x-ms-manifest"},
  251. {".map", "text/plain"},
  252. {".master", "application/xml"},
  253. {".mda", "application/msaccess"},
  254. {".mdb", "application/x-msaccess"},
  255. {".mde", "application/msaccess"},
  256. {".mdp", "application/octet-stream"},
  257. {".me", "application/x-troff-me"},
  258. {".mfp", "application/x-shockwave-flash"},
  259. {".mht", "message/rfc822"},
  260. {".mhtml", "message/rfc822"},
  261. {".mid", "audio/mid"},
  262. {".midi", "audio/mid"},
  263. {".mix", "application/octet-stream"},
  264. {".mk", "text/plain"},
  265. {".mmf", "application/x-smaf"},
  266. {".mno", "text/xml"},
  267. {".mny", "application/x-msmoney"},
  268. {".mod", "video/mpeg"},
  269. {".mov", "video/quicktime"},
  270. {".movie", "video/x-sgi-movie"},
  271. {".mp2", "video/mpeg"},
  272. {".mp2v", "video/mpeg"},
  273. {".mp3", "audio/mpeg"},
  274. {".mp4", "video/mp4"},
  275. {".mp4v", "video/mp4"},
  276. {".mpa", "video/mpeg"},
  277. {".mpe", "video/mpeg"},
  278. {".mpeg", "video/mpeg"},
  279. {".mpf", "application/vnd.ms-mediapackage"},
  280. {".mpg", "video/mpeg"},
  281. {".mpp", "application/vnd.ms-project"},
  282. {".mpv2", "video/mpeg"},
  283. {".mqv", "video/quicktime"},
  284. {".ms", "application/x-troff-ms"},
  285. {".msi", "application/octet-stream"},
  286. {".mso", "application/octet-stream"},
  287. {".mts", "video/vnd.dlna.mpeg-tts"},
  288. {".mtx", "application/xml"},
  289. {".mvb", "application/x-msmediaview"},
  290. {".mvc", "application/x-miva-compiled"},
  291. {".mxp", "application/x-mmxp"},
  292. {".nc", "application/x-netcdf"},
  293. {".nsc", "video/x-ms-asf"},
  294. {".nws", "message/rfc822"},
  295. {".ocx", "application/octet-stream"},
  296. {".oda", "application/oda"},
  297. {".odc", "text/x-ms-odc"},
  298. {".odh", "text/plain"},
  299. {".odl", "text/plain"},
  300. {".odp", "application/vnd.oasis.opendocument.presentation"},
  301. {".ods", "application/oleobject"},
  302. {".odt", "application/vnd.oasis.opendocument.text"},
  303. {".one", "application/onenote"},
  304. {".onea", "application/onenote"},
  305. {".onepkg", "application/onenote"},
  306. {".onetmp", "application/onenote"},
  307. {".onetoc", "application/onenote"},
  308. {".onetoc2", "application/onenote"},
  309. {".orderedtest", "application/xml"},
  310. {".osdx", "application/opensearchdescription+xml"},
  311. {".p10", "application/pkcs10"},
  312. {".p12", "application/x-pkcs12"},
  313. {".p7b", "application/x-pkcs7-certificates"},
  314. {".p7c", "application/pkcs7-mime"},
  315. {".p7m", "application/pkcs7-mime"},
  316. {".p7r", "application/x-pkcs7-certreqresp"},
  317. {".p7s", "application/pkcs7-signature"},
  318. {".pbm", "image/x-portable-bitmap"},
  319. {".pcast", "application/x-podcast"},
  320. {".pct", "image/pict"},
  321. {".pcx", "application/octet-stream"},
  322. {".pcz", "application/octet-stream"},
  323. {".pdf", "application/pdf"},
  324. {".pfb", "application/octet-stream"},
  325. {".pfm", "application/octet-stream"},
  326. {".pfx", "application/x-pkcs12"},
  327. {".pgm", "image/x-portable-graymap"},
  328. {".pic", "image/pict"},
  329. {".pict", "image/pict"},
  330. {".pkgdef", "text/plain"},
  331. {".pkgundef", "text/plain"},
  332. {".pko", "application/vnd.ms-pki.pko"},
  333. {".pls", "audio/scpls"},
  334. {".pma", "application/x-perfmon"},
  335. {".pmc", "application/x-perfmon"},
  336. {".pml", "application/x-perfmon"},
  337. {".pmr", "application/x-perfmon"},
  338. {".pmw", "application/x-perfmon"},
  339. {".png", "image/png"},
  340. {".pnm", "image/x-portable-anymap"},
  341. {".pnt", "image/x-macpaint"},
  342. {".pntg", "image/x-macpaint"},
  343. {".pnz", "image/png"},
  344. {".pot", "application/vnd.ms-powerpoint"},
  345. {".potm", "application/vnd.ms-powerpoint.template.macroEnabled.12"},
  346. {".potx", "application/vnd.openxmlformats-officedocument.presentationml.template"},
  347. {".ppa", "application/vnd.ms-powerpoint"},
  348. {".ppam", "application/vnd.ms-powerpoint.addin.macroEnabled.12"},
  349. {".ppm", "image/x-portable-pixmap"},
  350. {".pps", "application/vnd.ms-powerpoint"},
  351. {".ppsm", "application/vnd.ms-powerpoint.slideshow.macroEnabled.12"},
  352. {".ppsx", "application/vnd.openxmlformats-officedocument.presentationml.slideshow"},
  353. {".ppt", "application/vnd.ms-powerpoint"},
  354. {".pptm", "application/vnd.ms-powerpoint.presentation.macroEnabled.12"},
  355. {".pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"},
  356. {".prf", "application/pics-rules"},
  357. {".prm", "application/octet-stream"},
  358. {".prx", "application/octet-stream"},
  359. {".ps", "application/postscript"},
  360. {".psc1", "application/PowerShell"},
  361. {".psd", "application/octet-stream"},
  362. {".psess", "application/xml"},
  363. {".psm", "application/octet-stream"},
  364. {".psp", "application/octet-stream"},
  365. {".pub", "application/x-mspublisher"},
  366. {".pwz", "application/vnd.ms-powerpoint"},
  367. {".qht", "text/x-html-insertion"},
  368. {".qhtm", "text/x-html-insertion"},
  369. {".qt", "video/quicktime"},
  370. {".qti", "image/x-quicktime"},
  371. {".qtif", "image/x-quicktime"},
  372. {".qtl", "application/x-quicktimeplayer"},
  373. {".qxd", "application/octet-stream"},
  374. {".ra", "audio/x-pn-realaudio"},
  375. {".ram", "audio/x-pn-realaudio"},
  376. {".rar", "application/octet-stream"},
  377. {".ras", "image/x-cmu-raster"},
  378. {".rat", "application/rat-file"},
  379. {".rc", "text/plain"},
  380. {".rc2", "text/plain"},
  381. {".rct", "text/plain"},
  382. {".rdlc", "application/xml"},
  383. {".resx", "application/xml"},
  384. {".rf", "image/vnd.rn-realflash"},
  385. {".rgb", "image/x-rgb"},
  386. {".rgs", "text/plain"},
  387. {".rm", "application/vnd.rn-realmedia"},
  388. {".rmi", "audio/mid"},
  389. {".rmp", "application/vnd.rn-rn_music_package"},
  390. {".roff", "application/x-troff"},
  391. {".rpm", "audio/x-pn-realaudio-plugin"},
  392. {".rqy", "text/x-ms-rqy"},
  393. {".rtf", "application/rtf"},
  394. {".rtx", "text/richtext"},
  395. {".ruleset", "application/xml"},
  396. {".s", "text/plain"},
  397. {".safariextz", "application/x-safari-safariextz"},
  398. {".scd", "application/x-msschedule"},
  399. {".sct", "text/scriptlet"},
  400. {".sd2", "audio/x-sd2"},
  401. {".sdp", "application/sdp"},
  402. {".sea", "application/octet-stream"},
  403. {".searchConnector-ms", "application/windows-search-connector+xml"},
  404. {".setpay", "application/set-payment-initiation"},
  405. {".setreg", "application/set-registration-initiation"},
  406. {".settings", "application/xml"},
  407. {".sgimb", "application/x-sgimb"},
  408. {".sgml", "text/sgml"},
  409. {".sh", "application/x-sh"},
  410. {".shar", "application/x-shar"},
  411. {".shtml", "text/html"},
  412. {".sit", "application/x-stuffit"},
  413. {".sitemap", "application/xml"},
  414. {".skin", "application/xml"},
  415. {".sldm", "application/vnd.ms-powerpoint.slide.macroEnabled.12"},
  416. {".sldx", "application/vnd.openxmlformats-officedocument.presentationml.slide"},
  417. {".slk", "application/vnd.ms-excel"},
  418. {".sln", "text/plain"},
  419. {".slupkg-ms", "application/x-ms-license"},
  420. {".smd", "audio/x-smd"},
  421. {".smi", "application/octet-stream"},
  422. {".smx", "audio/x-smd"},
  423. {".smz", "audio/x-smd"},
  424. {".snd", "audio/basic"},
  425. {".snippet", "application/xml"},
  426. {".snp", "application/octet-stream"},
  427. {".sol", "text/plain"},
  428. {".sor", "text/plain"},
  429. {".spc", "application/x-pkcs7-certificates"},
  430. {".spl", "application/futuresplash"},
  431. {".src", "application/x-wais-source"},
  432. {".srf", "text/plain"},
  433. {".SSISDeploymentManifest", "text/xml"},
  434. {".ssm", "application/streamingmedia"},
  435. {".sst", "application/vnd.ms-pki.certstore"},
  436. {".stl", "application/vnd.ms-pki.stl"},
  437. {".sv4cpio", "application/x-sv4cpio"},
  438. {".sv4crc", "application/x-sv4crc"},
  439. {".svc", "application/xml"},
  440. {".swf", "application/x-shockwave-flash"},
  441. {".t", "application/x-troff"},
  442. {".tar", "application/x-tar"},
  443. {".tcl", "application/x-tcl"},
  444. {".testrunconfig", "application/xml"},
  445. {".testsettings", "application/xml"},
  446. {".tex", "application/x-tex"},
  447. {".texi", "application/x-texinfo"},
  448. {".texinfo", "application/x-texinfo"},
  449. {".tgz", "application/x-compressed"},
  450. {".thmx", "application/vnd.ms-officetheme"},
  451. {".thn", "application/octet-stream"},
  452. {".tif", "image/tiff"},
  453. {".tiff", "image/tiff"},
  454. {".tlh", "text/plain"},
  455. {".tli", "text/plain"},
  456. {".toc", "application/octet-stream"},
  457. {".tr", "application/x-troff"},
  458. {".trm", "application/x-msterminal"},
  459. {".trx", "application/xml"},
  460. {".ts", "video/vnd.dlna.mpeg-tts"},
  461. {".tsv", "text/tab-separated-values"},
  462. {".ttf", "application/octet-stream"},
  463. {".tts", "video/vnd.dlna.mpeg-tts"},
  464. {".txt", "text/plain"},
  465. {".u32", "application/octet-stream"},
  466. {".uls", "text/iuls"},
  467. {".user", "text/plain"},
  468. {".ustar", "application/x-ustar"},
  469. {".vb", "text/plain"},
  470. {".vbdproj", "text/plain"},
  471. {".vbk", "video/mpeg"},
  472. {".vbproj", "text/plain"},
  473. {".vbs", "text/vbscript"},
  474. {".vcf", "text/x-vcard"},
  475. {".vcproj", "Application/xml"},
  476. {".vcs", "text/plain"},
  477. {".vcxproj", "Application/xml"},
  478. {".vddproj", "text/plain"},
  479. {".vdp", "text/plain"},
  480. {".vdproj", "text/plain"},
  481. {".vdx", "application/vnd.ms-visio.viewer"},
  482. {".vml", "text/xml"},
  483. {".vscontent", "application/xml"},
  484. {".vsct", "text/xml"},
  485. {".vsd", "application/vnd.visio"},
  486. {".vsi", "application/ms-vsi"},
  487. {".vsix", "application/vsix"},
  488. {".vsixlangpack", "text/xml"},
  489. {".vsixmanifest", "text/xml"},
  490. {".vsmdi", "application/xml"},
  491. {".vspscc", "text/plain"},
  492. {".vss", "application/vnd.visio"},
  493. {".vsscc", "text/plain"},
  494. {".vssettings", "text/xml"},
  495. {".vssscc", "text/plain"},
  496. {".vst", "application/vnd.visio"},
  497. {".vstemplate", "text/xml"},
  498. {".vsto", "application/x-ms-vsto"},
  499. {".vsw", "application/vnd.visio"},
  500. {".vsx", "application/vnd.visio"},
  501. {".vtx", "application/vnd.visio"},
  502. {".wav", "audio/wav"},
  503. {".wave", "audio/wav"},
  504. {".wax", "audio/x-ms-wax"},
  505. {".wbk", "application/msword"},
  506. {".wbmp", "image/vnd.wap.wbmp"},
  507. {".wcm", "application/vnd.ms-works"},
  508. {".wdb", "application/vnd.ms-works"},
  509. {".wdp", "image/vnd.ms-photo"},
  510. {".webarchive", "application/x-safari-webarchive"},
  511. {".webtest", "application/xml"},
  512. {".wiq", "application/xml"},
  513. {".wiz", "application/msword"},
  514. {".wks", "application/vnd.ms-works"},
  515. {".WLMP", "application/wlmoviemaker"},
  516. {".wlpginstall", "application/x-wlpg-detect"},
  517. {".wlpginstall3", "application/x-wlpg3-detect"},
  518. {".wm", "video/x-ms-wm"},
  519. {".wma", "audio/x-ms-wma"},
  520. {".wmd", "application/x-ms-wmd"},
  521. {".wmf", "application/x-msmetafile"},
  522. {".wml", "text/vnd.wap.wml"},
  523. {".wmlc", "application/vnd.wap.wmlc"},
  524. {".wmls", "text/vnd.wap.wmlscript"},
  525. {".wmlsc", "application/vnd.wap.wmlscriptc"},
  526. {".wmp", "video/x-ms-wmp"},
  527. {".wmv", "video/x-ms-wmv"},
  528. {".wmx", "video/x-ms-wmx"},
  529. {".wmz", "application/x-ms-wmz"},
  530. {".wpl", "application/vnd.ms-wpl"},
  531. {".wps", "application/vnd.ms-works"},
  532. {".wri", "application/x-mswrite"},
  533. {".wrl", "x-world/x-vrml"},
  534. {".wrz", "x-world/x-vrml"},
  535. {".wsc", "text/scriptlet"},
  536. {".wsdl", "text/xml"},
  537. {".wvx", "video/x-ms-wvx"},
  538. {".x", "application/directx"},
  539. {".xaf", "x-world/x-vrml"},
  540. {".xaml", "application/xaml+xml"},
  541. {".xap", "application/x-silverlight-app"},
  542. {".xbap", "application/x-ms-xbap"},
  543. {".xbm", "image/x-xbitmap"},
  544. {".xdr", "text/plain"},
  545. {".xht", "application/xhtml+xml"},
  546. {".xhtml", "application/xhtml+xml"},
  547. {".xla", "application/vnd.ms-excel"},
  548. {".xlam", "application/vnd.ms-excel.addin.macroEnabled.12"},
  549. {".xlc", "application/vnd.ms-excel"},
  550. {".xld", "application/vnd.ms-excel"},
  551. {".xlk", "application/vnd.ms-excel"},
  552. {".xll", "application/vnd.ms-excel"},
  553. {".xlm", "application/vnd.ms-excel"},
  554. {".xls", "application/vnd.ms-excel"},
  555. {".xlsb", "application/vnd.ms-excel.sheet.binary.macroEnabled.12"},
  556. {".xlsm", "application/vnd.ms-excel.sheet.macroEnabled.12"},
  557. {".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
  558. {".xlt", "application/vnd.ms-excel"},
  559. {".xltm", "application/vnd.ms-excel.template.macroEnabled.12"},
  560. {".xltx", "application/vnd.openxmlformats-officedocument.spreadsheetml.template"},
  561. {".xlw", "application/vnd.ms-excel"},
  562. {".xml", "text/xml"},
  563. {".xmta", "application/xml"},
  564. {".xof", "x-world/x-vrml"},
  565. {".XOML", "text/plain"},
  566. {".xpm", "image/x-xpixmap"},
  567. {".xps", "application/vnd.ms-xpsdocument"},
  568. {".xrm-ms", "text/xml"},
  569. {".xsc", "application/xml"},
  570. {".xsd", "text/xml"},
  571. {".xsf", "text/xml"},
  572. {".xsl", "text/xml"},
  573. {".xslt", "text/xml"},
  574. {".xsn", "application/octet-stream"},
  575. {".xss", "application/xml"},
  576. {".xtp", "application/octet-stream"},
  577. {".xwd", "image/x-xwindowdump"},
  578. {".z", "application/x-compress"},
  579. {".zip", "application/x-zip-compressed"},
  580. #endregion
  581. };
  582. #endregion
  583. /// <summary>
  584. /// 获取文件类型
  585. /// </summary>
  586. /// <param name="extension"></param>
  587. /// <returns></returns>
  588. public static string GetMimeType(string extension)
  589. {
  590. if (extension == null)
  591. {
  592. throw new ArgumentNullException("extension");
  593. }
  594. var lowerExt = extension.ToLower();
  595. var mime = "application/octet-stream";
  596. if (_mappings.ContainsKey(lowerExt))
  597. {
  598. mime = _mappings[lowerExt];
  599. }
  600. return mime;
  601. }
  602. }
  603. /// <summary>
  604. /// 签名算法
  605. /// </summary>
  606. public class SignatureHelper
  607. {
  608. /// <summary>
  609. /// 生成签名
  610. /// </summary>
  611. /// <param name="fileName"></param>
  612. /// <param name="isRechristen"></param>
  613. /// <returns>返回第一个是签名,第二个是地址</returns>
  614. public static Tuple<string, string> GetAuthorizationAsync(string fileName, bool isRechristen = true, string method = "put", List<KeyValuePair<string, string>> requestParams = null, List<KeyValuePair<string, string>> headers = null, bool isLast = false, bool isUpgradePackage = false)
  615. {
  616. var storageUrl = "";
  617. var authorization = "";
  618. var storageSettingsParam = ConfigurationManager.StorageSettings;
  619. if (storageSettingsParam == null)
  620. {
  621. return null;
  622. }
  623. var storageServer = ConfigurationManager.GetParammeter<StringParameter>("Storage", "StorageServer").Value;
  624. var serverType = storageServer == "VCS" ? StorageServerEnum.Vinno : StorageServerEnum.Tencent;
  625. //表示自建服务器
  626. var newFileName = string.Empty;
  627. if (!isRechristen)
  628. {
  629. newFileName = fileName;
  630. }
  631. else
  632. {
  633. newFileName = Guid.NewGuid().ToString("N").ToUpper(); ;
  634. if (fileName.Split('.').Length > 0)
  635. {
  636. newFileName += "." + fileName.Split('.')[(fileName.Split('.').Length - 1)];
  637. }
  638. else
  639. {
  640. newFileName += ".dat";
  641. }
  642. }
  643. if (StorageServerEnum.Vinno == serverType)
  644. {
  645. //storageUrl = storageSettingsParam.CFSServerUrl + "/" + DateTime.Now.ToString("yyyyMMdd") + "/" + newFileName;
  646. if (isUpgradePackage)
  647. {
  648. storageUrl = storageSettingsParam.CFSServerUrl + "/SystemUpgradePackage/" + newFileName;
  649. }
  650. else
  651. {
  652. storageUrl = storageSettingsParam.CFSServerUrl + "/" + DateTime.UtcNow.ToString("yyyyMMdd") + "/" + newFileName;
  653. }
  654. }
  655. else
  656. {
  657. storageUrl = storageSettingsParam.CFSServerUrl + "/" + newFileName;
  658. }
  659. authorization = GetStorageAuthorization(newFileName, serverType, method, requestParams, headers, isLast);
  660. return Tuple.Create<string, string>(authorization, storageUrl);
  661. }
  662. /// <summary>
  663. /// 权鉴 签名地址生成
  664. /// </summary>
  665. /// <param name="fileType">上传文件类型</param>
  666. /// <returns>输出文件签名</returns>
  667. public static string GetStorageAuthorization(string fileName, StorageServerEnum serverType, string method = "put", List<KeyValuePair<string, string>> _requestParams = null, List<KeyValuePair<string, string>> _headers = null, bool isLast = false)
  668. {
  669. var storageSettingsParam = ConfigurationManager.StorageSettings;
  670. if (storageSettingsParam == null)
  671. {
  672. return string.Empty;
  673. }
  674. var now = DateTimeOffset.Now;
  675. long startTimeStamp = 0;
  676. long endTimeStamp = 0;
  677. if (isLast)
  678. {
  679. startTimeStamp = now.AddDays(-1).ToUnixTimeSeconds();
  680. endTimeStamp = now.AddDays(2).ToUnixTimeSeconds();
  681. }
  682. else
  683. {
  684. startTimeStamp = now.ToUnixTimeSeconds();
  685. endTimeStamp = now.AddDays(1).ToUnixTimeSeconds();
  686. }
  687. var keyTime = $"{startTimeStamp};{endTimeStamp}";
  688. var signKey = HMACSHA1(storageSettingsParam.Sercret, keyTime);
  689. var urlParamListBuilder = new StringBuilder();
  690. var stringParamBuilder = new StringBuilder();
  691. var urlParamList = string.Empty;
  692. var httpParameters = string.Empty;
  693. if (_requestParams?.Count > 0)
  694. {
  695. _requestParams = _requestParams.OrderBy(c => c.Key).ToList();//TODO check
  696. foreach (var param in _requestParams)
  697. {
  698. var encodeKey = Encode(param.Key).ToLower();
  699. urlParamListBuilder.Append($"{encodeKey};");
  700. var encodeValue = Encode(param.Value);
  701. stringParamBuilder.Append($"{encodeKey}={encodeValue}&");
  702. }
  703. urlParamList = urlParamListBuilder.ToString();
  704. if (!string.IsNullOrEmpty(urlParamList))
  705. {
  706. urlParamList = urlParamList.TrimEnd(';');
  707. }
  708. httpParameters = stringParamBuilder.ToString();
  709. if (!string.IsNullOrEmpty(httpParameters))
  710. {
  711. httpParameters = httpParameters.TrimEnd('&');
  712. }
  713. }
  714. var httpHeaders = string.Empty;
  715. var headerListBuilder = new StringBuilder();
  716. var httpHeadersBuilder = new StringBuilder();
  717. var headerList = string.Empty;
  718. if (_headers?.Count > 0)
  719. {
  720. _headers = _headers.OrderBy(c => c.Key).ToList();//TODO check
  721. foreach (var param in _headers)
  722. {
  723. var encodeKey = Encode(param.Key).ToLower();
  724. headerListBuilder.Append($"{encodeKey};");
  725. var encodeValue = Encode(param.Value);
  726. httpHeadersBuilder.Append($"{encodeKey}={encodeValue}&");
  727. }
  728. headerList = headerListBuilder.ToString();
  729. if (!string.IsNullOrEmpty(headerList))
  730. {
  731. headerList = headerList.TrimEnd(';');
  732. }
  733. httpHeaders = httpHeadersBuilder.ToString();
  734. if (!string.IsNullOrEmpty(httpHeaders))
  735. {
  736. httpHeaders = httpHeaders.TrimEnd('&');
  737. }
  738. }
  739. //Generatet HttpString
  740. string httpString = string.Format($"{method}\n/{fileName}\n{httpParameters}\n{httpHeaders}\n");
  741. //Generate StringToSign
  742. string stringToSign = string.Format("sha1\n{0}\n{1}\n", keyTime, SHA1Format(httpString));
  743. //Generate Signature
  744. var signature = "";
  745. if (serverType == StorageServerEnum.Vinno)
  746. {
  747. var newStringToSign = MD5.GetHashString(stringToSign, Encoding.UTF8).ToLower();
  748. signature = HMACSHA1(signKey, newStringToSign);
  749. }
  750. else
  751. {
  752. signature = HMACSHA1(signKey, stringToSign);
  753. }
  754. var authorization = $"q-sign-algorithm=sha1&q-ak={storageSettingsParam.AppId}&q-sign-time={keyTime}&q-key-time={keyTime}&q-header-list={headerList}&q-url-param-list={urlParamList}&q-signature={signature}";
  755. return authorization;
  756. }
  757. /// <summary>
  758. /// 权鉴 签名地址生成
  759. /// </summary>
  760. /// <param name="fileType">上传文件类型</param>
  761. /// <returns>输出文件签名</returns>
  762. public static string GetStorageCopyAuthorization(string fileName, StorageServerEnum serverType, List<KeyValuePair<string, string>> _headers = null)
  763. {
  764. var storageSettingsParam = ConfigurationManager.StorageSettings;
  765. if (storageSettingsParam == null)
  766. {
  767. return string.Empty;
  768. }
  769. var now = DateTimeOffset.Now;
  770. long startTimeStamp = now.ToUnixTimeSeconds();
  771. long endTimeStamp = now.AddDays(1).ToUnixTimeSeconds();
  772. var keyTime = $"{startTimeStamp};{endTimeStamp}";
  773. var signKey = HMACSHA1(storageSettingsParam.Sercret, keyTime);
  774. var headerListBuilder = new StringBuilder();
  775. var httpHeadersBuilder = new StringBuilder();
  776. var headerList = string.Empty;
  777. var httpHeaders = string.Empty;
  778. if (_headers?.Count > 0)
  779. {
  780. _headers = _headers.OrderBy(c => c.Key).ToList();//TODO check
  781. foreach (var param in _headers)
  782. {
  783. var encodeKey = Encode(param.Key).ToLower();
  784. headerListBuilder.Append($"{encodeKey};");
  785. var encodeValue = Encode(param.Value);
  786. httpHeadersBuilder.Append($"{encodeKey}={encodeValue}&");
  787. }
  788. headerList = headerListBuilder.ToString();
  789. if (!string.IsNullOrEmpty(headerList))
  790. {
  791. headerList = headerList.TrimEnd(';');
  792. }
  793. httpHeaders = httpHeadersBuilder.ToString();
  794. if (!string.IsNullOrEmpty(httpHeaders))
  795. {
  796. httpHeaders = httpHeaders.TrimEnd('&');
  797. }
  798. }
  799. var httpParameters = string.Empty;
  800. //Generatet HttpString
  801. string httpString = string.Format($"put\n/{fileName}\n{httpParameters}\n{httpHeaders}\n");
  802. //Generate StringToSign
  803. string stringToSign = string.Format("sha1\n{0}\n{1}\n", keyTime, SHA1Format(httpString));
  804. //Generate Signature
  805. var signature = "";
  806. if (serverType == StorageServerEnum.Vinno)
  807. {
  808. var newStringToSign = MD5.GetHashString(stringToSign, Encoding.UTF8).ToLower();
  809. signature = HMACSHA1(signKey, newStringToSign);
  810. }
  811. else
  812. {
  813. signature = HMACSHA1(signKey, stringToSign);
  814. }
  815. var authorization = $"q-sign-algorithm=sha1&q-ak={storageSettingsParam.AppId}&q-sign-time={keyTime}&q-key-time={keyTime}&q-header-list={headerList}&q-url-param-list=&q-signature={signature}";
  816. return authorization;
  817. }
  818. /// <summary>
  819. /// 生成其他节点配置列表
  820. /// </summary>
  821. /// <param name="config">请求参数<param>
  822. /// <returns>节点列表</returns>
  823. public static List<string> GetOtherCloudStorageNodeList(string config)
  824. {
  825. var result = new List<string>();
  826. var storageSettingsParam = ConfigurationManager.StorageSettings;
  827. if (storageSettingsParam == null)
  828. {
  829. return result;
  830. }
  831. var storageServer = ConfigurationManager.GetParammeter<StringParameter>("Storage", "StorageServer").Value;
  832. var serverType = storageServer == "VCS" ? StorageServerEnum.Vinno : StorageServerEnum.Tencent;
  833. if (!string.IsNullOrEmpty(config) && config.Split(';').Length > 1)
  834. {
  835. var tempList = config.Split(';');
  836. foreach (var item in tempList)
  837. {
  838. if (!string.IsNullOrEmpty(item) && item.Split('|').Length > 1)
  839. {
  840. var bucket = item.Split('|')[0];
  841. var storageNode = item.Split('|')[1];
  842. var storageUrl = "";
  843. if (StorageServerEnum.Vinno == serverType)
  844. {
  845. var secondDomain = bucket + "." + storageServer + "." + storageNode;
  846. storageUrl = storageSettingsParam.CFSServerUrl + "/" + secondDomain + "/" + DateTime.Now.ToString("yyyyMMdd");
  847. }
  848. else
  849. {
  850. var secondDomain = bucket + "." + storageServer.ToLower() + "." + storageNode;
  851. storageUrl = secondDomain + ".myqcloud.com";
  852. }
  853. result.Add(storageUrl);
  854. }
  855. }
  856. }
  857. return result;
  858. }
  859. /// <summary>
  860. /// 加密算法
  861. /// </summary>
  862. /// <param name="content">请求参数<param>
  863. /// <returns>加密结果</returns>
  864. private static string SHA1Format(string content)
  865. {
  866. var sha1 = SHA1.Create();
  867. byte[] c = Encoding.Default.GetBytes(content);
  868. byte[] sc = sha1.ComputeHash(c);
  869. StringBuilder sb = new StringBuilder("");
  870. foreach (byte b in sc)
  871. {
  872. sb.AppendFormat("{0:x2}", b);
  873. }
  874. return sb.ToString();
  875. }
  876. /// <summary>
  877. /// 加密算法
  878. /// </summary>
  879. /// <param name="secret">秘钥</param>
  880. /// <param name="content"></param>
  881. /// <returns>上传生成结果对象</returns>
  882. private static string HMACSHA1(string secret, string content)
  883. {
  884. byte[] keyByte = Encoding.Default.GetBytes(secret);
  885. HMACSHA1 hmacsha1 = new HMACSHA1(keyByte);
  886. byte[] messageBytes = Encoding.Default.GetBytes(content);
  887. byte[] hashmessage = hmacsha1.ComputeHash(messageBytes);
  888. StringBuilder sb = new StringBuilder("");
  889. foreach (byte b in hashmessage)
  890. {
  891. sb.AppendFormat("{0:x2}", b);
  892. }
  893. return sb.ToString();
  894. }
  895. /// <summary>
  896. /// 编码
  897. /// </summary>
  898. /// <param name="value"></param>
  899. /// <returns>编码结果</returns>
  900. public static string Encode(string value)
  901. {
  902. return Encode(value, Encoding.UTF8);
  903. }
  904. /// <summary>
  905. /// 针对单个value,则只需满足 URLAllowChars 不需要编码即可
  906. /// urlEncode: 转为一个byte -> 转为两个16进制 -> 前面加上 %
  907. /// </summary>
  908. /// <param name="value"></param>
  909. /// <param name="encoding"></param>
  910. /// <returns></returns>
  911. public static string Encode(string value, Encoding encoding)
  912. {
  913. //只有字母和数字[0-9a-zA-Z]、一些特殊符串_.!*~',以及某些保留字,才可以不经过编码直接用于URL地址
  914. string URLAllowChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~";
  915. if (String.IsNullOrEmpty(value))
  916. {
  917. return String.Empty;
  918. }
  919. // %xy%xy
  920. // %xy%xy
  921. StringBuilder result = new StringBuilder(value.Length * 2);
  922. byte[] strToBytes = encoding.GetBytes(value);
  923. foreach (byte b in strToBytes)
  924. {
  925. char ch = (char)b;
  926. if (URLAllowChars.IndexOf(ch) != -1)
  927. {
  928. result.Append(ch);
  929. }
  930. else
  931. {
  932. result.Append('%').Append(String.Format(CultureInfo.InvariantCulture, "{0:X2}", (int)b));
  933. }
  934. }
  935. return result.ToString();
  936. }
  937. /// <summary>
  938. /// 对象转XML字符串
  939. /// </summary>
  940. public static string XMLSerialize<T>(T t)
  941. {
  942. System.Text.StringBuilder sb = new System.Text.StringBuilder();
  943. XmlSerializer ser = new XmlSerializer(typeof(T));
  944. using (TextWriter write = new StringWriter(sb))
  945. {
  946. ser.Serialize(write, t);
  947. }
  948. return sb.ToString();
  949. }
  950. /// <summary>
  951. /// 反序列化
  952. /// </summary>
  953. /// <typeparam name="T"></typeparam>
  954. /// <param name="str"></param>
  955. /// <param name="encode"></param>
  956. /// <returns></returns>
  957. public static T DeserializeXML<T>(byte[] byteData) where T : class
  958. {
  959. T result = null;
  960. try
  961. {
  962. using (MemoryStream input = new MemoryStream(byteData))
  963. {
  964. XmlSerializer serializer = new XmlSerializer(typeof(T));
  965. result = serializer.Deserialize(input) as T;
  966. }
  967. }
  968. catch (Exception ex)
  969. {
  970. Console.WriteLine(ex.Message);
  971. }
  972. return result;
  973. }
  974. /// <summary>
  975. /// 反序列化
  976. /// </summary>
  977. /// <typeparam name="T"></typeparam>
  978. /// <param name="str"></param>
  979. /// <param name="encode"></param>
  980. /// <returns></returns>
  981. public static T DeserializeXML<T>(string xmlString) where T : class
  982. {
  983. T result = null;
  984. try
  985. {
  986. XmlSerializer serializer = new XmlSerializer(typeof(T));
  987. using (StringReader reader = new StringReader(xmlString))
  988. {
  989. result = (T)serializer.Deserialize(reader);
  990. }
  991. }
  992. catch (Exception ex)
  993. {
  994. Console.WriteLine(ex.Message);
  995. }
  996. return result;
  997. }
  998. }
  999. }