AzureBlobDBRepository.cs 22 KB

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