|
@@ -0,0 +1,138 @@
|
|
|
+using AutoMapper;
|
|
|
+using Azure.Storage.Blobs;
|
|
|
+using Microsoft.AspNetCore.Http;
|
|
|
+using Microsoft.AspNetCore.Mvc;
|
|
|
+using Microsoft.AspNetCore.StaticFiles;
|
|
|
+using MTWorkHR.Application.Models;
|
|
|
+using MTWorkHR.Application.Services.Interfaces;
|
|
|
+using MTWorkHR.Core.Global;
|
|
|
+using System.Net.Http.Headers;
|
|
|
+
|
|
|
+namespace MTWorkHR.Application.Services
|
|
|
+{
|
|
|
+ public class BlobFileService : IFileService
|
|
|
+ {
|
|
|
+ // private readonly AppSettingsConfiguration settings;
|
|
|
+ private const string ContainerName = "blobcontainer";
|
|
|
+ public const string SuccessMessageKey = "SuccessMessage";
|
|
|
+ public const string ErrorMessageKey = "ErrorMessage";
|
|
|
+ private readonly BlobServiceClient _blobServiceClient;
|
|
|
+ private readonly BlobContainerClient _containerClient;
|
|
|
+
|
|
|
+ public BlobFileService(BlobServiceClient blobServiceClient)
|
|
|
+ {
|
|
|
+ _blobServiceClient = blobServiceClient;
|
|
|
+ _containerClient = _blobServiceClient.GetBlobContainerClient(ContainerName);
|
|
|
+ _containerClient.CreateIfNotExists();
|
|
|
+ }
|
|
|
+ public async Task<string> UploadFile(IFormFile file)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var blobClient = _containerClient.GetBlobClient(file.FileName);
|
|
|
+ var status = await blobClient.UploadAsync(file.OpenReadStream(), true);
|
|
|
+ //if(status.Value == "201")
|
|
|
+ return blobClient.Uri.AbsoluteUri;
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public async Task<List<string>> UploadFiles(List<IFormFile> files)
|
|
|
+ {
|
|
|
+ List<string> msgs = new List<string>();
|
|
|
+ foreach (var formFile in files)
|
|
|
+ {
|
|
|
+ msgs.Add(await UploadFile(formFile));
|
|
|
+ }
|
|
|
+ return msgs;
|
|
|
+ }
|
|
|
+ public async Task<Tuple<bool, string>> UploadFile2(IFormFile file)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var blobClient = _containerClient.GetBlobClient(file.FileName);
|
|
|
+ var status = await blobClient.UploadAsync(file.OpenReadStream(), true);
|
|
|
+ return new Tuple<bool, string>(true, "");// = "File uploaded successfully.";
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ return new Tuple<bool, string>(false, ex.Message);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ public async Task<List<Tuple<bool, string>>> UploadFiles2(List<IFormFile> files)
|
|
|
+ {
|
|
|
+ List<Tuple<bool, string>> msgs = new List<Tuple<bool, string>>();
|
|
|
+ foreach (var formFile in files)
|
|
|
+ {
|
|
|
+ msgs.Add( await UploadFile2(formFile));
|
|
|
+ }
|
|
|
+ return msgs;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public async Task<bool> Delete(string fileName)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var blobClient = _containerClient.GetBlobClient(fileName);
|
|
|
+ await blobClient.DeleteIfExistsAsync();
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void CopyFileToCloud(ref List<AttachmentDto> attachments)
|
|
|
+ {
|
|
|
+ foreach(var attach in attachments)
|
|
|
+ {
|
|
|
+ if (attach.FileData != null)
|
|
|
+ {
|
|
|
+ var resPath = UploadFile(attach.FileData).Result;
|
|
|
+ if (resPath != "")
|
|
|
+ attach.FilePath = resPath;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public bool CopyFileToActualFolder(string FileName)
|
|
|
+ {
|
|
|
+ throw new NotImplementedException();
|
|
|
+ }
|
|
|
+
|
|
|
+ public bool DeleteFileFromTempFolder(string FileName)
|
|
|
+ {
|
|
|
+ throw new NotImplementedException();
|
|
|
+ }
|
|
|
+
|
|
|
+ public string GetTempAttachmentPath()
|
|
|
+ {
|
|
|
+ throw new NotImplementedException();
|
|
|
+ }
|
|
|
+
|
|
|
+ public string GetActualAttachmentPath()
|
|
|
+ {
|
|
|
+ throw new NotImplementedException();
|
|
|
+ }
|
|
|
+
|
|
|
+ public Task<Tuple<MemoryStream, string, string>> GetFileDownloadInfo(string fileUrl)
|
|
|
+ {
|
|
|
+ throw new NotImplementedException();
|
|
|
+ }
|
|
|
+
|
|
|
+ public Task<bool> CopyFileToActualFolder(List<AttachmentDto> attachments)
|
|
|
+ {
|
|
|
+ throw new NotImplementedException();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|