BlobFileService.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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<AttachmentResponseDto> UploadFile(IFormFile file)
  29. {
  30. AttachmentResponseDto result = new AttachmentResponseDto();
  31. try
  32. {
  33. var blobClient = _containerClient.GetBlobClient(file.FileName);
  34. if (blobClient != null)
  35. {
  36. var status = await blobClient.UploadAsync(file.OpenReadStream(), true);
  37. //if(status.Value == "201")
  38. result.FileName = file.FileName;
  39. result.FilePath = blobClient.Uri.AbsoluteUri;
  40. return result;
  41. }else
  42. return result;
  43. }
  44. catch (Exception ex)
  45. {
  46. return result;
  47. }
  48. }
  49. public async Task<string> UploadFileCloud(AttachmentDto file)
  50. {
  51. try
  52. {
  53. var blobClient = _containerClient.GetBlobClient(file.FileName);
  54. if(blobClient != null)
  55. {
  56. //var stream = file.FileData.OpenReadStream();
  57. using (Stream fs = file.FileData.OpenReadStream())
  58. {
  59. var status = await blobClient.UploadAsync(fs, true);
  60. fs.Position = 0;
  61. using (BinaryReader br = new BinaryReader(fs))
  62. {
  63. byte[] bytes = br.ReadBytes((Int32)fs.Length);
  64. var Headers = file.FileData.Headers;
  65. file.Content = bytes;
  66. file.ContentType = file.FileData.ContentType;
  67. file.FilePath = blobClient.Uri.AbsoluteUri;
  68. }
  69. }
  70. //if(status.Value == "201")
  71. return blobClient.Uri.AbsoluteUri;
  72. }else
  73. return "";
  74. }
  75. catch (Exception ex)
  76. {
  77. return "";
  78. }
  79. }
  80. public async Task<BlobObject> Download(string url)
  81. {
  82. try
  83. {
  84. var fileName = new Uri(url).Segments.LastOrDefault();
  85. var blobClient = _containerClient.GetBlobClient(fileName);
  86. if(await blobClient.ExistsAsync())
  87. {
  88. BlobDownloadResult content = await blobClient.DownloadContentAsync();
  89. var downloadedData = content.Content.ToStream();
  90. if (ImageExtensions.Contains(Path.GetExtension(fileName.ToUpperInvariant())))
  91. {
  92. var extension = Path.GetExtension(fileName);
  93. return new BlobObject { Content = downloadedData, ContentType = "image/"+extension.Remove(0,1) };
  94. }
  95. else
  96. {
  97. return new BlobObject { Content = downloadedData, ContentType = content.Details.ContentType };
  98. }
  99. }
  100. return null;
  101. }
  102. catch (Exception ex)
  103. {
  104. return null;
  105. }
  106. }
  107. public async Task<List<AttachmentResponseDto>> UploadFiles(List<IFormFile> files)
  108. {
  109. List<AttachmentResponseDto> msgs = new List<AttachmentResponseDto>();
  110. foreach (var formFile in files)
  111. {
  112. msgs.Add(await UploadFile(formFile));
  113. }
  114. return msgs;
  115. }
  116. public async Task<Tuple<bool, string>> UploadFile2(IFormFile file)
  117. {
  118. try
  119. {
  120. var blobClient = _containerClient.GetBlobClient(file.FileName);
  121. var status = await blobClient.UploadAsync(file.OpenReadStream(), true);
  122. return new Tuple<bool, string>(true, "");// = "File uploaded successfully.";
  123. }
  124. catch (Exception ex)
  125. {
  126. return new Tuple<bool, string>(false, ex.Message);
  127. }
  128. }
  129. public async Task<List<Tuple<bool, string>>> UploadFiles2(List<IFormFile> files)
  130. {
  131. List<Tuple<bool, string>> msgs = new List<Tuple<bool, string>>();
  132. foreach (var formFile in files)
  133. {
  134. msgs.Add( await UploadFile2(formFile));
  135. }
  136. return msgs;
  137. }
  138. public async Task<bool> Delete(string fileName)
  139. {
  140. try
  141. {
  142. var blobClient = _containerClient.GetBlobClient(fileName);
  143. await blobClient.DeleteIfExistsAsync();
  144. return true;
  145. }
  146. catch (Exception ex)
  147. {
  148. return false;
  149. }
  150. }
  151. public void CopyFileToCloud(ref List<AttachmentDto> attachments)
  152. {
  153. foreach(var attach in attachments)
  154. {
  155. if (attach.FileData != null)
  156. {
  157. var resPath = UploadFileCloud(attach).Result;
  158. //if (resPath != "")
  159. // attach.FilePath = resPath;
  160. }
  161. }
  162. }
  163. public bool CopyFileToActualFolder(string FileName)
  164. {
  165. throw new NotImplementedException();
  166. }
  167. public bool DeleteFileFromTempFolder(string FileName)
  168. {
  169. throw new NotImplementedException();
  170. }
  171. public string GetTempAttachmentPath()
  172. {
  173. throw new NotImplementedException();
  174. }
  175. public string GetActualAttachmentPath()
  176. {
  177. throw new NotImplementedException();
  178. }
  179. public Task<Tuple<MemoryStream, string, string>> GetFileDownloadInfo(string fileUrl)
  180. {
  181. throw new NotImplementedException();
  182. }
  183. public Task<bool> CopyFileToActualFolder(List<AttachmentDto> attachments)
  184. {
  185. throw new NotImplementedException();
  186. }
  187. string[] ImageExtensions = new string[]
  188. {
  189. ".JPEG"
  190. , ".JPG"
  191. , ".PNG"
  192. };
  193. }
  194. }