BlobFileService.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using AutoMapper;
  2. using Azure.Storage.Blobs;
  3. using Azure.Storage.Blobs.Models;
  4. using Microsoft.AspNetCore.Http;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.AspNetCore.StaticFiles;
  7. using MTWorkHR.Application.Models;
  8. using MTWorkHR.Application.Services.Interfaces;
  9. using MTWorkHR.Core.Global;
  10. using System.IO;
  11. using System.Net.Http.Headers;
  12. namespace MTWorkHR.Application.Services
  13. {
  14. public class BlobFileService : IFileService
  15. {
  16. // private readonly AppSettingsConfiguration settings;
  17. private const string ContainerName = "blobcontainer";
  18. public const string SuccessMessageKey = "SuccessMessage";
  19. public const string ErrorMessageKey = "ErrorMessage";
  20. private readonly BlobServiceClient _blobServiceClient;
  21. private readonly BlobContainerClient _containerClient;
  22. public BlobFileService(BlobServiceClient blobServiceClient)
  23. {
  24. _blobServiceClient = blobServiceClient;
  25. _containerClient = _blobServiceClient.GetBlobContainerClient(ContainerName);
  26. _containerClient.CreateIfNotExists();
  27. }
  28. public async Task<string> UploadFile(IFormFile file)
  29. {
  30. try
  31. {
  32. var blobClient = _containerClient.GetBlobClient(file.FileName);
  33. if (blobClient != null)
  34. {
  35. var status = await blobClient.UploadAsync(file.OpenReadStream(), true);
  36. //if(status.Value == "201")
  37. return blobClient.Uri.AbsoluteUri;
  38. }else
  39. return "";
  40. }
  41. catch (Exception ex)
  42. {
  43. return "";
  44. }
  45. }
  46. public async Task<string> UploadFileCloud(AttachmentDto file)
  47. {
  48. try
  49. {
  50. var blobClient = _containerClient.GetBlobClient(file.FileName);
  51. if(blobClient != null)
  52. {
  53. //var stream = file.FileData.OpenReadStream();
  54. using (Stream fs = file.FileData.OpenReadStream())
  55. {
  56. var status = await blobClient.UploadAsync(fs, true);
  57. fs.Position = 0;
  58. using (BinaryReader br = new BinaryReader(fs))
  59. {
  60. byte[] bytes = br.ReadBytes((Int32)fs.Length);
  61. var Headers = file.FileData.Headers;
  62. file.Content = bytes;
  63. file.ContentType = file.FileData.ContentType;
  64. file.FilePath = blobClient.Uri.AbsoluteUri;
  65. }
  66. }
  67. //if(status.Value == "201")
  68. return blobClient.Uri.AbsoluteUri;
  69. }else
  70. return "";
  71. }
  72. catch (Exception ex)
  73. {
  74. return "";
  75. }
  76. }
  77. public async Task<BlobObject> Download(string url)
  78. {
  79. try
  80. {
  81. var fileName = new Uri(url).Segments.LastOrDefault();
  82. var blobClient = _containerClient.GetBlobClient(fileName);
  83. if(await blobClient.ExistsAsync())
  84. {
  85. BlobDownloadResult content = await blobClient.DownloadContentAsync();
  86. var downloadedData = content.Content.ToStream();
  87. if (ImageExtensions.Contains(Path.GetExtension(fileName.ToUpperInvariant())))
  88. {
  89. var extension = Path.GetExtension(fileName);
  90. return new BlobObject { Content = downloadedData, ContentType = "image/"+extension.Remove(0,1) };
  91. }
  92. else
  93. {
  94. return new BlobObject { Content = downloadedData, ContentType = content.Details.ContentType };
  95. }
  96. }
  97. return null;
  98. }
  99. catch (Exception ex)
  100. {
  101. return null;
  102. }
  103. }
  104. public async Task<List<string>> UploadFiles(List<IFormFile> files)
  105. {
  106. List<string> msgs = new List<string>();
  107. foreach (var formFile in files)
  108. {
  109. msgs.Add(await UploadFile(formFile));
  110. }
  111. return msgs;
  112. }
  113. public async Task<Tuple<bool, string>> UploadFile2(IFormFile file)
  114. {
  115. try
  116. {
  117. var blobClient = _containerClient.GetBlobClient(file.FileName);
  118. var status = await blobClient.UploadAsync(file.OpenReadStream(), true);
  119. return new Tuple<bool, string>(true, "");// = "File uploaded successfully.";
  120. }
  121. catch (Exception ex)
  122. {
  123. return new Tuple<bool, string>(false, ex.Message);
  124. }
  125. }
  126. public async Task<List<Tuple<bool, string>>> UploadFiles2(List<IFormFile> files)
  127. {
  128. List<Tuple<bool, string>> msgs = new List<Tuple<bool, string>>();
  129. foreach (var formFile in files)
  130. {
  131. msgs.Add( await UploadFile2(formFile));
  132. }
  133. return msgs;
  134. }
  135. public async Task<bool> Delete(string fileName)
  136. {
  137. try
  138. {
  139. var blobClient = _containerClient.GetBlobClient(fileName);
  140. await blobClient.DeleteIfExistsAsync();
  141. return true;
  142. }
  143. catch (Exception ex)
  144. {
  145. return false;
  146. }
  147. }
  148. public void CopyFileToCloud(ref List<AttachmentDto> attachments)
  149. {
  150. foreach(var attach in attachments)
  151. {
  152. if (attach.FileData != null)
  153. {
  154. var resPath = UploadFileCloud(attach).Result;
  155. //if (resPath != "")
  156. // attach.FilePath = resPath;
  157. }
  158. }
  159. }
  160. public bool CopyFileToActualFolder(string FileName)
  161. {
  162. throw new NotImplementedException();
  163. }
  164. public bool DeleteFileFromTempFolder(string FileName)
  165. {
  166. throw new NotImplementedException();
  167. }
  168. public string GetTempAttachmentPath()
  169. {
  170. throw new NotImplementedException();
  171. }
  172. public string GetActualAttachmentPath()
  173. {
  174. throw new NotImplementedException();
  175. }
  176. public Task<Tuple<MemoryStream, string, string>> GetFileDownloadInfo(string fileUrl)
  177. {
  178. throw new NotImplementedException();
  179. }
  180. public Task<bool> CopyFileToActualFolder(List<AttachmentDto> attachments)
  181. {
  182. throw new NotImplementedException();
  183. }
  184. string[] ImageExtensions = new string[]
  185. {
  186. ".JPEG"
  187. , ".JPG"
  188. , ".PNG"
  189. };
  190. }
  191. }