BlobFileService.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using AutoMapper;
  2. using Azure.Storage.Blobs;
  3. using Microsoft.AspNetCore.Http;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Microsoft.AspNetCore.StaticFiles;
  6. using MTWorkHR.Application.Models;
  7. using MTWorkHR.Application.Services.Interfaces;
  8. using MTWorkHR.Core.Global;
  9. using System.Net.Http.Headers;
  10. namespace MTWorkHR.Application.Services
  11. {
  12. public class BlobFileService : IFileService
  13. {
  14. // private readonly AppSettingsConfiguration settings;
  15. private const string ContainerName = "blobcontainer";
  16. public const string SuccessMessageKey = "SuccessMessage";
  17. public const string ErrorMessageKey = "ErrorMessage";
  18. private readonly BlobServiceClient _blobServiceClient;
  19. private readonly BlobContainerClient _containerClient;
  20. public BlobFileService(BlobServiceClient blobServiceClient)
  21. {
  22. _blobServiceClient = blobServiceClient;
  23. _containerClient = _blobServiceClient.GetBlobContainerClient(ContainerName);
  24. _containerClient.CreateIfNotExists();
  25. }
  26. public async Task<string> UploadFile(IFormFile file)
  27. {
  28. try
  29. {
  30. var blobClient = _containerClient.GetBlobClient(file.FileName);
  31. var status = await blobClient.UploadAsync(file.OpenReadStream(), true);
  32. //if(status.Value == "201")
  33. return blobClient.Uri.AbsoluteUri;
  34. }
  35. catch (Exception ex)
  36. {
  37. return "";
  38. }
  39. }
  40. public async Task<List<string>> UploadFiles(List<IFormFile> files)
  41. {
  42. List<string> msgs = new List<string>();
  43. foreach (var formFile in files)
  44. {
  45. msgs.Add(await UploadFile(formFile));
  46. }
  47. return msgs;
  48. }
  49. public async Task<Tuple<bool, string>> UploadFile2(IFormFile file)
  50. {
  51. try
  52. {
  53. var blobClient = _containerClient.GetBlobClient(file.FileName);
  54. var status = await blobClient.UploadAsync(file.OpenReadStream(), true);
  55. return new Tuple<bool, string>(true, "");// = "File uploaded successfully.";
  56. }
  57. catch (Exception ex)
  58. {
  59. return new Tuple<bool, string>(false, ex.Message);
  60. }
  61. }
  62. public async Task<List<Tuple<bool, string>>> UploadFiles2(List<IFormFile> files)
  63. {
  64. List<Tuple<bool, string>> msgs = new List<Tuple<bool, string>>();
  65. foreach (var formFile in files)
  66. {
  67. msgs.Add( await UploadFile2(formFile));
  68. }
  69. return msgs;
  70. }
  71. public async Task<bool> Delete(string fileName)
  72. {
  73. try
  74. {
  75. var blobClient = _containerClient.GetBlobClient(fileName);
  76. await blobClient.DeleteIfExistsAsync();
  77. return true;
  78. }
  79. catch (Exception ex)
  80. {
  81. return false;
  82. }
  83. }
  84. public void CopyFileToCloud(ref List<AttachmentDto> attachments)
  85. {
  86. foreach(var attach in attachments)
  87. {
  88. if (attach.FileData != null)
  89. {
  90. var resPath = UploadFile(attach.FileData).Result;
  91. if (resPath != "")
  92. attach.FilePath = resPath;
  93. }
  94. }
  95. }
  96. public bool CopyFileToActualFolder(string FileName)
  97. {
  98. throw new NotImplementedException();
  99. }
  100. public bool DeleteFileFromTempFolder(string FileName)
  101. {
  102. throw new NotImplementedException();
  103. }
  104. public string GetTempAttachmentPath()
  105. {
  106. throw new NotImplementedException();
  107. }
  108. public string GetActualAttachmentPath()
  109. {
  110. throw new NotImplementedException();
  111. }
  112. public Task<Tuple<MemoryStream, string, string>> GetFileDownloadInfo(string fileUrl)
  113. {
  114. throw new NotImplementedException();
  115. }
  116. public Task<bool> CopyFileToActualFolder(List<AttachmentDto> attachments)
  117. {
  118. throw new NotImplementedException();
  119. }
  120. }
  121. }