using AutoMapper; using Azure.Storage.Blobs; using Azure.Storage.Blobs.Models; 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.IO; 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 UploadFile(IFormFile file) { try { var blobClient = _containerClient.GetBlobClient(file.FileName); if (blobClient != null) { var status = await blobClient.UploadAsync(file.OpenReadStream(), true); //if(status.Value == "201") return blobClient.Uri.AbsoluteUri; }else return ""; } catch (Exception ex) { return ""; } } public async Task UploadFileCloud(AttachmentDto file) { try { var blobClient = _containerClient.GetBlobClient(file.FileName); if(blobClient != null) { //var stream = file.FileData.OpenReadStream(); using (Stream fs = file.FileData.OpenReadStream()) { var status = await blobClient.UploadAsync(fs, true); fs.Position = 0; using (BinaryReader br = new BinaryReader(fs)) { byte[] bytes = br.ReadBytes((Int32)fs.Length); var Headers = file.FileData.Headers; file.Content = bytes; file.ContentType = file.FileData.ContentType; file.FilePath = blobClient.Uri.AbsoluteUri; } } //if(status.Value == "201") return blobClient.Uri.AbsoluteUri; }else return ""; } catch (Exception ex) { return ""; } } public async Task Download(string url) { try { var fileName = new Uri(url).Segments.LastOrDefault(); var blobClient = _containerClient.GetBlobClient(fileName); if(await blobClient.ExistsAsync()) { BlobDownloadResult content = await blobClient.DownloadContentAsync(); var downloadedData = content.Content.ToStream(); if (ImageExtensions.Contains(Path.GetExtension(fileName.ToUpperInvariant()))) { var extension = Path.GetExtension(fileName); return new BlobObject { Content = downloadedData, ContentType = "image/"+extension.Remove(0,1) }; } else { return new BlobObject { Content = downloadedData, ContentType = content.Details.ContentType }; } } return null; } catch (Exception ex) { return null; } } public async Task> UploadFiles(List files) { List msgs = new List(); foreach (var formFile in files) { msgs.Add(await UploadFile(formFile)); } return msgs; } public async Task> UploadFile2(IFormFile file) { try { var blobClient = _containerClient.GetBlobClient(file.FileName); var status = await blobClient.UploadAsync(file.OpenReadStream(), true); return new Tuple(true, "");// = "File uploaded successfully."; } catch (Exception ex) { return new Tuple(false, ex.Message); } } public async Task>> UploadFiles2(List files) { List> msgs = new List>(); foreach (var formFile in files) { msgs.Add( await UploadFile2(formFile)); } return msgs; } public async Task Delete(string fileName) { try { var blobClient = _containerClient.GetBlobClient(fileName); await blobClient.DeleteIfExistsAsync(); return true; } catch (Exception ex) { return false; } } public void CopyFileToCloud(ref List attachments) { foreach(var attach in attachments) { if (attach.FileData != null) { var resPath = UploadFileCloud(attach).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> GetFileDownloadInfo(string fileUrl) { throw new NotImplementedException(); } public Task CopyFileToActualFolder(List attachments) { throw new NotImplementedException(); } string[] ImageExtensions = new string[] { ".JPEG" , ".JPG" , ".PNG" }; } }