BlobFileService.cs 6.6 KB

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