AzureBlobDBRepository.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. using TEAMModelOS.SDK.Module.AzureBlob.Configuration;
  2. using TEAMModelOS.SDK.Module.AzureBlob.Container;
  3. using TEAMModelOS.SDK.Module.AzureBlob.Interfaces;
  4. using Microsoft.WindowsAzure.Storage;
  5. using Microsoft.WindowsAzure.Storage.Blob;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Net.Http.Headers;
  10. using System.Threading.Tasks;
  11. using TEAMModelOS.SDK.Helper.Security.AESCrypt;
  12. using TEAMModelOS.SDK.Context.Exception;
  13. using Microsoft.AspNetCore.Http;
  14. using TEAMModelOS.SDK.Extension.SnowFlake;
  15. using TEAMModelOS.SDK.Context.Constant;
  16. using TEAMModelOS.SDK.Helper.Common.JsonHelper;
  17. using TEAMModelOS.SDK.Helper.Security.ShaHash;
  18. namespace TEAMModelOS.SDK.Module.AzureBlob.Implements
  19. {
  20. public class AzureBlobDBRepository : IAzureBlobDBRepository
  21. {
  22. private readonly string china = "417A7572654368696E612020202020202020202020202020202020202020202097EB27FCC1F03349787DCD35F4DE22BBDFEDC90F24738B1D7FB9167A2C191BE671B512E17D48B73A002FC98867345CD59D3250AF59FD5FDFFC67976108F9E3BC9E9F75EDE605B058C1821D16BD9EB753B8E7D39FF48163430C1B5F3B6150195B880C3FCB87D35BF3540432734B768EC28C77B4CF0D556E794DE57979C1E01C429E66F7B2794D9940CF287F2B22A22E5F266B949D5E523E709FF37229E45D1A8FC8C4341E0A8346BB976CCB3D91802FFE5A4A28577898B4E942B5BA3A4A7B796FA673782D405060E7F2CBA4F67DF59F47";
  23. private readonly string global = "417A757265476C6F62616C2020202020202020202020202020202020202020206956019D195ED330AFA660D369B9464FC5E90AB3A106FDDD7978A2772DB186CDAE21C6CBFDE2B6739F089E853B3171A27841026E61C51666347F63FDF63E4377448D493B05CF6CDB3791946B9145825DD7756392EB8EA36DBF42E5C1C0021CEC2CDB5F4EA57EBCFA98B17D7236FA2CDCA6E7FCBE1DDC45BEAF691A2462A8BC3C429CBC4BCCA3192E554D23758AA8EA5937F988C927534C70A4769ED33878BEC10E2550F121E4AEB5A2DA213F2902D602A758C7D93D5DED368544F8A86D2A0CAA7813D1D950EC81D544EE41A8EDC84173";
  24. public CloudBlobClient blobClient;
  25. public CloudBlobContainer blobContainer;
  26. public AzureBlobOptions _options;
  27. public AzureBlobDBRepository(AzureBlobOptions options)
  28. {
  29. _options = options;
  30. if (!string.IsNullOrEmpty(options.ConnectionString))
  31. {
  32. blobClient = BlobClientSingleton.getInstance(options.ConnectionString).GetBlobClient();
  33. }
  34. else { throw new BizException("请设置正确的AzureBlob文件存储配置信息!"); }
  35. }
  36. public AzureBlobDBRepository()
  37. {
  38. // _connectionString = BaseConfigModel.Configuration["AppSettings:Azure:TableStorageConnection"];
  39. }
  40. //private async Task InitializeBlob(string container)
  41. //{
  42. ////https://teammodelstorage.blob.core.chinacloudapi.cn/wechatfilescontainer
  43. // if (blobContainer == null)
  44. // {
  45. // // Type t = typeof(T);
  46. // //若要将权限设置为仅针对 blob 的公共读取访问,请将 PublicAccess 属性设置为 BlobContainerPublicAccessType.Blob。
  47. // //要删除匿名用户的所有权限,请将该属性设置为 BlobContainerPublicAccessType.Off。
  48. // blobContainer = blobClient.GetContainerReference("wechatfilescontainer");
  49. // // await blobContainer.CreateIfNotExistsAsync();
  50. // // BlobContainerPermissions permissions = await blobContainer.GetPermissionsAsync();
  51. // // permissions.PublicAccess = BlobContainerPublicAccessType.Blob;
  52. // // await blobContainer.SetPermissionsAsync(permissions);
  53. // }
  54. // //await UploadFiles(null, new FileContainer() );
  55. //}
  56. public async Task<List<AzureBlobModel>> UploadFiles(IFormFile[] file,string fileSpace= "common" , bool contentTypeDefault = false)
  57. {
  58. string groupName = fileSpace+"/" +DateTime.Now.ToString("yyyyMMdd");
  59. string newFileName = DateTime.Now.ToString("yyyyMMddHHmmss");
  60. // await InitializeBlob(DateTime.Now.ToString("yyyyMMdd"));
  61. blobContainer = blobClient.GetContainerReference( groupName);
  62. //var serviceProperties = await blobClient.GetServicePropertiesAsync();
  63. //var corsSettings = serviceProperties.Cors;
  64. //var corsRule = corsSettings.CorsRules.FirstOrDefault(
  65. // o => o.AllowedOrigins.Contains("http://localhost:3904"));//设置你自己的服务器地址
  66. //if (corsRule == null)
  67. //{
  68. // //Add a new rule.
  69. // corsRule = new CorsRule()
  70. // {
  71. // AllowedHeaders = new List<string> { "x-ms-*", "content-type", "accept" },
  72. // AllowedMethods = CorsHttpMethods.Put| CorsHttpMethods.Head | CorsHttpMethods.Post | CorsHttpMethods.Merge | CorsHttpMethods.Get,//Since we'll only be calling Put Blob, let's just allow PUT verb
  73. // AllowedOrigins = new List<string> { "http://localhost:3904" },//This is the URL of our application.
  74. // ExposedHeaders = { },
  75. // MaxAgeInSeconds = 1 * 60 * 60,//Let the browswer cache it for an hour
  76. // };
  77. // corsSettings.CorsRules.Add(corsRule);
  78. // //Save the rule
  79. // await blobClient.SetServicePropertiesAsync(serviceProperties);
  80. //}
  81. StorageUri url = blobContainer.StorageUri;
  82. List<AzureBlobModel> list = new List<AzureBlobModel>();
  83. foreach (FormFile f in file)
  84. {
  85. string[] names = f.FileName.Split(".");
  86. string name = "";
  87. for (int i = 0; i < names.Length-1; i++) {
  88. name = name + names[i];
  89. }
  90. if (names.Length <= 1)
  91. {
  92. name = f.FileName + "_" + newFileName;
  93. }
  94. else {
  95. name = name + "_" + newFileName + "." + names[names.Length - 1];
  96. }
  97. string fileext = f.FileName.Substring(f.FileName.LastIndexOf(".")>0? f.FileName.LastIndexOf("."):0);
  98. var parsedContentDisposition = ContentDispositionHeaderValue.Parse(f.ContentDisposition);
  99. var filename = Path.Combine(parsedContentDisposition.FileName.Trim('"'));
  100. var blockBlob = blobContainer.GetBlockBlobReference(name);
  101. if (!contentTypeDefault) {
  102. ContentTypeDict.dict.TryGetValue(fileext, out string content_type);
  103. if (!string.IsNullOrEmpty(content_type))
  104. {
  105. blockBlob.Properties.ContentType = content_type;
  106. }
  107. }
  108. await blockBlob.UploadFromStreamAsync(f.OpenReadStream());
  109. string sha1= ShaHashHelper.GetSHA1(f.OpenReadStream());
  110. AzureBlobModel model = new AzureBlobModel(f, _options.Container, groupName, name)
  111. {
  112. BlobUrl = url.PrimaryUri.ToString().Split("?")[0] + "/" + name,
  113. Sha1Code = sha1
  114. };
  115. list.Add(model);
  116. }
  117. return list;
  118. }
  119. public async Task<AzureBlobModel> UploadPath(string path, string fileSpace = "common" , bool contentTypeDefault = false) {
  120. string groupName = fileSpace + "/" + DateTime.Now.ToString("yyyyMMdd");
  121. string newFileName = DateTime.Now.ToString("HHmmssfffffff");
  122. blobContainer = blobClient.GetContainerReference(groupName);
  123. StorageUri url = blobContainer.StorageUri;
  124. FileInfo file = new FileInfo(path);
  125. string[] names = file.Name.Split(".");
  126. string name = "";
  127. for (int i = 0; i < names.Length - 1; i++)
  128. {
  129. name = name + names[i];
  130. }
  131. if (names.Length <= 1)
  132. {
  133. name = file.Name + "_" + newFileName;
  134. }
  135. else
  136. {
  137. name = name + "_" + newFileName + "." + names[names.Length - 1];
  138. }
  139. string fileext = file.Name.Substring(file.Name.LastIndexOf(".") > 0 ? file.Name.LastIndexOf(".") : 0);
  140. // var parsedContentDisposition = ContentDispositionHeaderValue.Parse("form-data; name=\"files\"; filename=\"" + file.Name + "\"");
  141. var blockBlob = blobContainer.GetBlockBlobReference(name);
  142. string content_type = "application/octet-stream";
  143. if (!contentTypeDefault)
  144. {
  145. ContentTypeDict.dict.TryGetValue(fileext, out string contenttype);
  146. if (!string.IsNullOrEmpty(contenttype))
  147. {
  148. blockBlob.Properties.ContentType = contenttype;
  149. content_type = contenttype;
  150. }
  151. else
  152. {
  153. blockBlob.Properties.ContentType = content_type;
  154. }
  155. }
  156. else
  157. {
  158. blockBlob.Properties.ContentType = content_type;
  159. }
  160. await blockBlob.UploadFromFileAsync(path);
  161. //var provider = new FileExtensionContentTypeProvider();
  162. //var memi = provider.Mappings[fileext];
  163. AzureBlobModel model = new AzureBlobModel(file, _options.Container, groupName, name , content_type)
  164. {
  165. Sha1Code=ShaHashHelper.GetSHA1(file.Create()),
  166. BlobUrl = url.PrimaryUri.ToString().Split("?")[0] + "/" + name
  167. };
  168. return model;
  169. }
  170. public async Task<AzureBlobModel> UploadText(string fileName, string text, string fileSpace = "common", bool contentTypeDefault = true)
  171. {
  172. string groupName = fileSpace + "/" + DateTime.Now.ToString("yyyyMMdd");
  173. string newFileName = DateTime.Now.ToString("HHmmssfffffff");
  174. blobContainer = blobClient.GetContainerReference(groupName);
  175. StorageUri url = blobContainer.StorageUri;
  176. //FileInfo file = new FileInfo(path);
  177. string[] names = fileName.Split(".");
  178. string name = "";
  179. for (int i = 0; i < names.Length - 1; i++)
  180. {
  181. name = name + names[i];
  182. }
  183. if (names.Length <= 1)
  184. {
  185. name = fileName + "_" + newFileName;
  186. }
  187. else
  188. {
  189. name = name + "_" + newFileName + "." + names[names.Length - 1];
  190. }
  191. var blockBlob = blobContainer.GetBlockBlobReference(name);
  192. // var parsedContentDisposition = ContentDispositionHeaderValue.Parse("form-data; name=\"files\"; filename=\"" + file.Name + "\"");
  193. string fileext = fileName.Substring(fileName.LastIndexOf(".") > 0 ? fileName.LastIndexOf(".") : 0);
  194. string content_type = "application/octet-stream";
  195. if (!contentTypeDefault)
  196. {
  197. ContentTypeDict.dict.TryGetValue(fileext, out string contenttype);
  198. if (!string.IsNullOrEmpty(contenttype))
  199. {
  200. blockBlob.Properties.ContentType = contenttype;
  201. content_type = contenttype;
  202. }
  203. else
  204. {
  205. blockBlob.Properties.ContentType = content_type;
  206. }
  207. }
  208. else
  209. {
  210. blockBlob.Properties.ContentType = content_type;
  211. }
  212. await blockBlob.UploadTextAsync(text);
  213. byte[] bytes = System.Text.Encoding.Default.GetBytes(text);
  214. //var provider = new FileExtensionContentTypeProvider();
  215. //var memi = provider.Mappings[fileext];
  216. AzureBlobModel model = new AzureBlobModel(fileName, _options.Container, groupName, name, content_type, bytes.Length)
  217. {
  218. Sha1Code = ShaHashHelper.GetSHA1(bytes),
  219. BlobUrl = url.PrimaryUri.ToString().Split("?")[0] + "/" + name
  220. };
  221. return model;
  222. }
  223. public async Task<AzureBlobModel> UploadObject(string fileName,object obj, string fileSpace = "common", bool contentTypeDefault =true)
  224. {
  225. string groupName = fileSpace + "/" + DateTime.Now.ToString("yyyyMMdd");
  226. string newFileName = DateTime.Now.ToString("HHmmssfffffff");
  227. blobContainer = blobClient.GetContainerReference(groupName);
  228. StorageUri url = blobContainer.StorageUri;
  229. //FileInfo file = new FileInfo(path);
  230. string[] names = fileName.Split(".");
  231. string name = "";
  232. for (int i = 0; i < names.Length - 1; i++)
  233. {
  234. name = name + names[i];
  235. }
  236. if (names.Length <= 1)
  237. {
  238. name = fileName + "_" + newFileName;
  239. }
  240. else
  241. {
  242. name = name + "_" + newFileName + "." + names[names.Length - 1];
  243. }
  244. var blockBlob = blobContainer.GetBlockBlobReference(name);
  245. // var parsedContentDisposition = ContentDispositionHeaderValue.Parse("form-data; name=\"files\"; filename=\"" + file.Name + "\"");
  246. string fileext = fileName.Substring(fileName.LastIndexOf(".") > 0 ? fileName.LastIndexOf(".") : 0);
  247. string content_type = "application/octet-stream";
  248. if (!contentTypeDefault)
  249. {
  250. ContentTypeDict.dict.TryGetValue(fileext, out string contenttype);
  251. if (!string.IsNullOrEmpty(contenttype))
  252. {
  253. blockBlob.Properties.ContentType = contenttype;
  254. content_type = contenttype;
  255. }
  256. else
  257. {
  258. blockBlob.Properties.ContentType = content_type;
  259. }
  260. }
  261. else {
  262. blockBlob.Properties.ContentType = content_type;
  263. }
  264. string objStr = obj.ToJsonAbs();
  265. await blockBlob.UploadTextAsync(objStr);
  266. //var provider = new FileExtensionContentTypeProvider();
  267. //var memi = provider.Mappings[fileext];
  268. byte[] bytes = System.Text.Encoding.Default.GetBytes(objStr);
  269. AzureBlobModel model = new AzureBlobModel(fileName, _options.Container, groupName, name, content_type , bytes.Length)
  270. {
  271. Sha1Code = ShaHashHelper.GetSHA1(bytes),
  272. BlobUrl = url.PrimaryUri.ToString().Split("?")[0] + "/" + name
  273. };
  274. return model;
  275. }
  276. public async Task<AzureBlobModel> UploadFile(IFormFile file, string fileSpace = "wordfiles", bool contentTypeDefault = true)
  277. {
  278. long bizno= IdWorker.getInstance().NextId();
  279. string groupName = fileSpace + "/" + DateTime.Now.ToString("yyyyMMdd")+"/"+ bizno;
  280. string newFileName = DateTime.Now.ToString("yyyyMMddHHmmss");
  281. // await InitializeBlob(DateTime.Now.ToString("yyyyMMdd"));
  282. blobContainer = blobClient.GetContainerReference(groupName);
  283. StorageUri url = blobContainer.StorageUri;
  284. string[] names = file.FileName.Split(".");
  285. string name = "";
  286. for (int i = 0; i < names.Length - 1; i++)
  287. {
  288. name = name + names[i];
  289. }
  290. if (names.Length <= 1)
  291. {
  292. name = name + "_" + newFileName;
  293. }
  294. else
  295. {
  296. name = name + "_" + newFileName + "." + names[names.Length - 1];
  297. }
  298. var parsedContentDisposition = ContentDispositionHeaderValue.Parse(file.ContentDisposition);
  299. var filename = Path.Combine(parsedContentDisposition.FileName.Trim('"'));
  300. var blockBlob = blobContainer.GetBlockBlobReference(name);
  301. string fileext = filename.Substring(filename.LastIndexOf(".") > 0 ? filename.LastIndexOf(".") : 0);
  302. if (!contentTypeDefault)
  303. {
  304. ContentTypeDict.dict.TryGetValue(fileext, out string content_type);
  305. if (!string.IsNullOrEmpty(content_type))
  306. {
  307. blockBlob.Properties.ContentType = content_type;
  308. }
  309. }
  310. await blockBlob.UploadFromStreamAsync(file.OpenReadStream());
  311. string sha1 = ShaHashHelper.GetSHA1(file.OpenReadStream());
  312. AzureBlobModel model = new AzureBlobModel(file, _options.Container, groupName, name)
  313. {
  314. Sha1Code=sha1,
  315. BlobUrl = url.PrimaryUri.ToString().Split("?")[0] + "/" + name
  316. };
  317. return model;
  318. }
  319. public AzureBlobModel UploadFileByFolderNAsyn(Stream fileSteam, string folder, string fileName, string fileSpace = "pptx", bool contentTypeDefault = true)
  320. {
  321. string groupName = fileSpace + "/" + folder;
  322. // string newFileName = sha1Code;
  323. // await InitializeBlob(DateTime.Now.ToString("yyyyMMdd"));
  324. blobContainer = blobClient.GetContainerReference(groupName);
  325. StorageUri url = blobContainer.StorageUri;
  326. string[] names = fileName.Split(".");
  327. // string name ;
  328. //for (int i = 0; i < names.Length - 1; i++)
  329. //{
  330. // name = name + names[i];
  331. //}
  332. //if (names.Length <= 1)
  333. //{
  334. // name = newFileName;
  335. //}
  336. //else
  337. //{
  338. // name = newFileName + "." + names[names.Length - 1];
  339. //}
  340. //var parsedContentDisposition = ContentDispositionHeaderValue.Parse(file.ContentDisposition);
  341. //var filename = Path.Combine(parsedContentDisposition.FileName.Trim('"'));
  342. var blockBlob = blobContainer.GetBlockBlobReference(fileName);
  343. string fileext = fileName.Substring(fileName.LastIndexOf(".") > 0 ? fileName.LastIndexOf(".") : 0);
  344. if (!contentTypeDefault)
  345. {
  346. ContentTypeDict.dict.TryGetValue(fileext, out string content_type);
  347. if (!string.IsNullOrEmpty(content_type))
  348. {
  349. blockBlob.Properties.ContentType = content_type;
  350. }
  351. else
  352. {
  353. blockBlob.Properties.ContentType = "application/octet-stream";
  354. }
  355. }
  356. blockBlob.UploadFromStreamAsync(fileSteam).GetAwaiter().GetResult() ;
  357. AzureBlobModel model = new AzureBlobModel(fileName, _options.Container, groupName, fileName, folder, blockBlob.Properties.ContentType, fileSteam.Length)
  358. {
  359. Sha1Code = ShaHashHelper.GetSHA1(fileSteam),
  360. BlobUrl = url.PrimaryUri.ToString().Split("?")[0] + "/" + fileName
  361. };
  362. return model;
  363. }
  364. public async Task<AzureBlobModel> UploadFileByFolder(Stream fileSteam, string folder, string fileName, string fileSpace = "pptx", bool contentTypeDefault = true)
  365. {
  366. string groupName = fileSpace + "/" + folder;
  367. // string newFileName = sha1Code;
  368. // await InitializeBlob(DateTime.Now.ToString("yyyyMMdd"));
  369. blobContainer = blobClient.GetContainerReference(groupName);
  370. StorageUri url = blobContainer.StorageUri;
  371. string[] names = fileName.Split(".");
  372. // string name ;
  373. //for (int i = 0; i < names.Length - 1; i++)
  374. //{
  375. // name = name + names[i];
  376. //}
  377. //if (names.Length <= 1)
  378. //{
  379. // name = newFileName;
  380. //}
  381. //else
  382. //{
  383. // name = newFileName + "." + names[names.Length - 1];
  384. //}
  385. //var parsedContentDisposition = ContentDispositionHeaderValue.Parse(file.ContentDisposition);
  386. //var filename = Path.Combine(parsedContentDisposition.FileName.Trim('"'));
  387. var blockBlob = blobContainer.GetBlockBlobReference(fileName);
  388. string fileext = fileName.Substring(fileName.LastIndexOf(".") > 0 ? fileName.LastIndexOf(".") : 0);
  389. if (!contentTypeDefault)
  390. {
  391. ContentTypeDict.dict.TryGetValue(fileext, out string content_type);
  392. if (!string.IsNullOrEmpty(content_type))
  393. {
  394. blockBlob.Properties.ContentType = content_type;
  395. }
  396. else {
  397. blockBlob.Properties.ContentType = "application/octet-stream";
  398. }
  399. }
  400. await blockBlob.UploadFromStreamAsync(fileSteam);
  401. string sha1 = ShaHashHelper.GetSHA1(fileSteam);
  402. AzureBlobModel model = new AzureBlobModel(fileName, _options.Container, groupName, fileName, folder, blockBlob.Properties.ContentType, fileSteam.Length)
  403. {
  404. Sha1Code = sha1,
  405. BlobUrl = url.PrimaryUri.ToString().Split("?")[0] + "/" + fileName
  406. };
  407. return model;
  408. }
  409. public async Task<AzureBlobModel> UploadTextByFolder(string text, string folder, string fileName, string fileSpace = "pptx", bool contentTypeDefault = true)
  410. {
  411. string groupName = fileSpace + "/" + folder;
  412. blobContainer = blobClient.GetContainerReference(groupName);
  413. StorageUri url = blobContainer.StorageUri;
  414. var blockBlob = blobContainer.GetBlockBlobReference(fileName);
  415. string fileext = fileName.Substring(fileName.LastIndexOf(".") > 0 ? fileName.LastIndexOf(".") : 0);
  416. string content_type = "application/octet-stream";
  417. if (!contentTypeDefault)
  418. {
  419. ContentTypeDict.dict.TryGetValue(fileext, out string contenttype);
  420. if (!string.IsNullOrEmpty(contenttype))
  421. {
  422. blockBlob.Properties.ContentType = contenttype;
  423. content_type = contenttype;
  424. }
  425. else
  426. {
  427. blockBlob.Properties.ContentType = content_type;
  428. }
  429. }
  430. else
  431. {
  432. blockBlob.Properties.ContentType = content_type;
  433. }
  434. await blockBlob.UploadTextAsync(text);
  435. byte[] bytes = System.Text.Encoding.Default.GetBytes(text);
  436. AzureBlobModel model = new AzureBlobModel(fileName, _options.Container, groupName, fileName, folder, blockBlob.Properties.ContentType, bytes.Length)
  437. {
  438. Sha1Code = ShaHashHelper.GetSHA1(bytes),
  439. BlobUrl = url.PrimaryUri.ToString().Split("?")[0] + "/" + fileName
  440. };
  441. return model;
  442. }
  443. }
  444. }