BlobFileService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 Microsoft.Extensions.Logging;
  8. using MTWorkHR.Application.Models;
  9. using MTWorkHR.Application.Services.Interfaces;
  10. using MTWorkHR.Core.Global;
  11. using System.IO;
  12. using System.Net.Http.Headers;
  13. namespace MTWorkHR.Application.Services
  14. {
  15. public class BlobFileService : IFileService
  16. {
  17. // private readonly AppSettingsConfiguration settings;
  18. private const string ContainerName = "blobcontainer";
  19. public const string SuccessMessageKey = "SuccessMessage";
  20. public const string ErrorMessageKey = "ErrorMessage";
  21. private readonly BlobServiceClient _blobServiceClient;
  22. private readonly BlobContainerClient _containerClient;
  23. private readonly ILogger<BlobFileService> _logger;
  24. public BlobFileService(BlobServiceClient blobServiceClient, ILogger<BlobFileService> logger)
  25. {
  26. _blobServiceClient = blobServiceClient;
  27. _containerClient = _blobServiceClient.GetBlobContainerClient(ContainerName);
  28. _containerClient.CreateIfNotExists();
  29. _logger = logger;
  30. }
  31. public async Task<AttachmentResponseDto> UploadFile2(IFormFile file)
  32. {
  33. AttachmentResponseDto result = new AttachmentResponseDto();
  34. try
  35. {
  36. string uniqueFileName = GenerateUniqueFileName(file.FileName);
  37. var blobClient = _containerClient.GetBlobClient(uniqueFileName);
  38. if (blobClient != null)
  39. {
  40. if (blobClient.ExistsAsync().Result)
  41. {
  42. uniqueFileName = GenerateUniqueFileName(file.FileName);
  43. }
  44. var status = await blobClient.UploadAsync(file.OpenReadStream(), true);
  45. result.ContentType = file.ContentType;
  46. result.FileSize = file.Length;
  47. result.OriginalName = file.FileName;
  48. result.FileName = uniqueFileName;
  49. result.FilePath = blobClient.Uri.AbsoluteUri;
  50. return result;
  51. }
  52. else
  53. return result;
  54. }
  55. catch (Exception ex)
  56. {
  57. _logger.LogError(ex.Message);
  58. return result;
  59. }
  60. }
  61. public async Task<AttachmentResponseDto> UploadFile(IFormFile file)
  62. {
  63. AttachmentResponseDto result = new AttachmentResponseDto();
  64. try
  65. {
  66. if (file == null || file.Length == 0)
  67. {
  68. _logger.LogWarning("No file uploaded or file is empty.");
  69. throw new AppException(ExceptionEnum.InCorrectFileLength);
  70. }
  71. _logger.LogInformation("Received file: Name={FileName}, ContentType={ContentType}, Length={Length}",
  72. file.FileName, file.ContentType, file.Length);
  73. string uniqueFileName = GenerateUniqueFileName(file.FileName);
  74. var blobClient = _containerClient.GetBlobClient(uniqueFileName);
  75. if (await blobClient.ExistsAsync())
  76. {
  77. uniqueFileName = GenerateUniqueFileName(file.FileName);
  78. blobClient = _containerClient.GetBlobClient(uniqueFileName);
  79. }
  80. using var stream = file.OpenReadStream();
  81. if (stream.Length == 0)
  82. {
  83. _logger.LogWarning("File stream is empty for file: {FileName}", file.FileName);
  84. throw new AppException(ExceptionEnum.InCorrectFileLength);
  85. }
  86. var status = await blobClient.UploadAsync(stream, true);
  87. result.ContentType = file.ContentType;
  88. result.FileSize = file.Length; // Should now reflect the correct size
  89. result.OriginalName = file.FileName;
  90. result.FileName = uniqueFileName;
  91. result.FilePath = blobClient.Uri.AbsoluteUri;
  92. return result;
  93. }
  94. catch (Exception ex)
  95. {
  96. _logger.LogError(ex, "Failed to upload file: {Message}", ex.Message);
  97. throw new AppException(ExceptionEnum.CouldNotMoveFiles);
  98. }
  99. }
  100. public async Task<string> UploadFileCloud(AttachmentDto file)
  101. {
  102. try
  103. {
  104. // Generate a unique file name to avoid overwriting
  105. string uniqueFileName = GenerateUniqueFileName(file.FileName);
  106. var blobClient = _containerClient.GetBlobClient(uniqueFileName);
  107. _logger.LogInformation("0########## File upload start uniqueFileName={0}", uniqueFileName);
  108. if (blobClient != null)
  109. {
  110. if (blobClient.ExistsAsync().Result)
  111. {
  112. uniqueFileName = GenerateUniqueFileName(file.FileName);
  113. }
  114. using (Stream fs = file.FileData.OpenReadStream())
  115. {
  116. // Upload the file to blob storage
  117. await blobClient.UploadAsync(fs, true);
  118. fs.Position = 0;
  119. using (BinaryReader br = new BinaryReader(fs))
  120. {
  121. byte[] bytes = br.ReadBytes((Int32)fs.Length);
  122. var headers = file.FileData.Headers;
  123. file.Content = bytes;
  124. file.ContentType = file.FileData.ContentType;
  125. file.FilePath = blobClient.Uri.AbsoluteUri;
  126. file.OriginalName = file.FileName;
  127. file.FileName = uniqueFileName;
  128. }
  129. }
  130. _logger.LogInformation("1########## File upload finish path={0}", blobClient.Uri.AbsoluteUri);
  131. return blobClient.Uri.AbsoluteUri;
  132. }
  133. else
  134. {
  135. return "";
  136. }
  137. }
  138. catch (Exception ex)
  139. {
  140. _logger.LogError(ex.Message);
  141. return "";
  142. }
  143. }
  144. // Helper method to generate a unique file name
  145. private string GenerateUniqueFileName(string originalFileName)
  146. {
  147. // Extract file name and extension
  148. string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(originalFileName);
  149. string extension = Path.GetExtension(originalFileName);
  150. // Append a unique identifier (e.g., timestamp or GUID)
  151. string uniqueIdentifier = DateTime.UtcNow.Ticks.ToString(); // or Guid.NewGuid().ToString()
  152. return $"{fileNameWithoutExtension}_{uniqueIdentifier}{extension}";
  153. }
  154. public async Task<AttachmentResponseDto> UploadFile(byte[] pdfBytes, string fileName)
  155. {
  156. AttachmentResponseDto result = new AttachmentResponseDto();
  157. try
  158. {
  159. // Get a reference to the container
  160. //BlobContainerClient containerClient = _blobServiceClient.GetBlobContainerClient("Contracts");
  161. // Create the container if it doesn’t exist
  162. //containerClient.CreateIfNotExists();
  163. var blobClient = _containerClient.GetBlobClient(fileName);
  164. if (blobClient != null)
  165. {
  166. using (MemoryStream uploadStream = new MemoryStream(pdfBytes))
  167. {
  168. _logger.LogInformation("1--Contract uploadStream length={0}", pdfBytes.Length);
  169. var status = await blobClient.UploadAsync(uploadStream, overwrite: true);
  170. _logger.LogInformation("2--Contract uploadStream Status={0}, uri={1}", status, blobClient.Uri.AbsoluteUri);
  171. }
  172. result.FileName = fileName;
  173. result.FilePath = blobClient.Uri.AbsoluteUri;
  174. _logger.LogInformation("3--Contract upload finish path={0}", blobClient.Uri.AbsoluteUri);
  175. return result;
  176. }
  177. else
  178. {
  179. _logger.LogInformation("BlobClient is null when upload file ={0}", fileName);
  180. return result;
  181. }
  182. }
  183. catch (Exception ex)
  184. {
  185. _logger.LogError(ex.Message);
  186. return result;
  187. }
  188. }
  189. public async Task<BlobObject> Download(string url)
  190. {
  191. try
  192. {
  193. var fileName = new Uri(url).Segments.LastOrDefault();
  194. var blobClient = _containerClient.GetBlobClient(fileName);
  195. if(await blobClient.ExistsAsync())
  196. {
  197. BlobDownloadResult content = await blobClient.DownloadContentAsync();
  198. var downloadedData = content.Content.ToStream();
  199. if (ImageExtensions.Contains(Path.GetExtension(fileName.ToUpperInvariant())))
  200. {
  201. var extension = Path.GetExtension(fileName);
  202. return new BlobObject { Content = downloadedData, ContentType = "image/"+extension.Remove(0,1) };
  203. }
  204. else
  205. {
  206. return new BlobObject { Content = downloadedData, ContentType = content.Details.ContentType };
  207. }
  208. }
  209. return null;
  210. }
  211. catch (Exception ex)
  212. {
  213. return null;
  214. }
  215. }
  216. public async Task<List<AttachmentResponseDto>> UploadFiles(List<IFormFile> files)
  217. {
  218. List<AttachmentResponseDto> msgs = new List<AttachmentResponseDto>();
  219. foreach (var formFile in files)
  220. {
  221. msgs.Add(await UploadFile(formFile));
  222. }
  223. return msgs;
  224. }
  225. public async Task<bool> Delete(string fileName)
  226. {
  227. try
  228. {
  229. var blobClient = _containerClient.GetBlobClient(fileName);
  230. await blobClient.DeleteIfExistsAsync();
  231. return true;
  232. }
  233. catch (Exception ex)
  234. {
  235. return false;
  236. }
  237. }
  238. public void CopyFileToCloud(ref List<AttachmentDto> attachments)
  239. {
  240. foreach(var attach in attachments)
  241. {
  242. if (attach.FileData != null)
  243. {
  244. var resPath = UploadFileCloud(attach).Result;
  245. //if (resPath != "")
  246. // attach.FilePath = resPath;
  247. }
  248. }
  249. }
  250. public bool CopyFileToActualFolder(string FileName)
  251. {
  252. throw new NotImplementedException();
  253. }
  254. public bool DeleteFileFromTempFolder(string FileName)
  255. {
  256. throw new NotImplementedException();
  257. }
  258. public string GetTempAttachmentPath()
  259. {
  260. throw new NotImplementedException();
  261. }
  262. public string GetActualAttachmentPath()
  263. {
  264. throw new NotImplementedException();
  265. }
  266. public Task<Tuple<MemoryStream, string, string>> GetFileDownloadInfo(string fileUrl)
  267. {
  268. throw new NotImplementedException();
  269. }
  270. public Task<bool> CopyFileToActualFolder(List<AttachmentDto> attachments)
  271. {
  272. throw new NotImplementedException();
  273. }
  274. string[] ImageExtensions = new string[]
  275. {
  276. ".JPEG"
  277. , ".JPG"
  278. , ".PNG"
  279. };
  280. }
  281. }