using Azure; using Azure.Storage.Blobs; using Azure.Storage.Blobs.Models; using Azure.Storage.Blobs.Specialized; using DamageAssesment.Api.DocuLinks.Interfaces; using DamageAssesment.Api.DocuLinks.Models; using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.Extensions.Configuration; using Microsoft.VisualBasic; using System.ComponentModel; using System.IO; using System.Text; using System.Threading.Tasks; namespace DamageAssesment.Api.DocuLinks.Providers { public class AzureBlobService: IAzureBlobService { BlobServiceClient _blobClient; BlobContainerClient _containerClient; string azureConnectionString; private string uploadpath = ""; private string Deletepath = ""; public AzureBlobService(IConfiguration configuration) { uploadpath = configuration.GetValue("Fileupload:folderpath"); Deletepath = configuration.GetValue("Fileupload:Deletepath"); _blobClient = new BlobServiceClient(configuration.GetValue("Fileupload:BlobConnectionString")); _containerClient = _blobClient.GetBlobContainerClient(configuration.GetValue("Fileupload:BlobContainerName")); } public async Task UploadDocument(int counter, ReqDoculink documentInfo) { Models.Doculink Documents = new Models.Doculink(); List attachments = new List(); try { string path = "", UserfileName = ""; if (documentInfo.Files != null) { int counter1 = 1; foreach (var item in documentInfo.Files) { if (item.IsAttachments) { UserfileName = Path.GetFileName(item.FileName); var fileName = String.Format("Document_{0}_{1}{2}", counter, counter1, item.FileExtension); byte[] byteArray = Convert.FromBase64String(item.FileContent); MemoryStream stream = new MemoryStream(byteArray); BlobClient client = _containerClient.GetBlobClient(uploadpath + "/" + fileName); var result = await client.UploadAsync(stream, true); path = uploadpath + "/" + fileName; counter1++; } else path = item.url; attachments.Add(new Models.DoculinkAttachments { docName = UserfileName, Path = path, IsAttachments = item.IsAttachments, CustomOrder = item.CustomOrder }); } } Documents = new Models.Doculink() { linkTypeId = documentInfo.linkTypeId, documentsTranslations = documentInfo.documentsTranslations, doclinksAttachments = attachments, IsDeleted = false, CustomOrder = documentInfo.CustomOrder, IsActive = true }; return Documents; } catch (Exception ex) { return new Models.Doculink(); } } public async Task UpdateDocuments(int counter, Models.Doculink document, ReqDoculink documentInfo) { try { foreach (var item in document.doclinksAttachments) { Movefile(item.Path); } string path = "", UserfileName = ""; List attachments = new List(); int counter1 = 1; foreach (var item in documentInfo.Files) { if (item.IsAttachments) { UserfileName = Path.GetFileName(item.FileName); var fileName = String.Format("Document_{0}_{1}{2)", document.Id, counter1, item.FileExtension); byte[] byteArray = Encoding.UTF8.GetBytes(item.FileContent); MemoryStream stream = new MemoryStream(byteArray); BlobClient client = _containerClient.GetBlobClient(uploadpath + "/" + fileName); path = uploadpath + "/" + fileName; var result = await client.UploadAsync(stream, true); counter1++; } else path = item.url; attachments.Add(new Models.DoculinkAttachments { docName = UserfileName, Path = path, IsAttachments = item.IsAttachments, CustomOrder = item.CustomOrder }); } Models.Doculink Documents = new Models.Doculink() { Id = documentInfo.Id, linkTypeId = documentInfo.linkTypeId, documentsTranslations = documentInfo.documentsTranslations, IsActive = true, IsDeleted = false, CustomOrder = documentInfo.CustomOrder, doclinksAttachments = attachments }; return Documents; } catch (Exception ex) { return new Models.Doculink(); } } public async Task>> UploadFiles(List files) { var azureResponse = new List>(); foreach (var file in files) { string fileName = file.FileName; using (var memoryStream = new MemoryStream()) { file.CopyTo(memoryStream); memoryStream.Position = 0; var client = await _containerClient.UploadBlobAsync(fileName, memoryStream, default); azureResponse.Add(client); } }; return azureResponse; } public string getMovefilename(string movefilename) { var list = movefilename.Split('.'); if (list.Length > 0) list[list.Length - 1] = DateTime.Now.ToShortDateString().Replace("/", "_") +"_"+ DateTime.Now.ToShortTimeString().Replace("/", "_")+"." + list[list.Length - 1]; return string.Join("_", list); } public void Movefile(string path) { try { if (path != "") { string MovePath = getMovefilename(path.Replace(uploadpath, Deletepath)); // Get references to the source and destination blobs BlobClient sourceBlobClient = _containerClient.GetBlobClient(path); BlobClient destinationBlobClient = _containerClient.GetBlobClient(MovePath); // Start the copy operation from the source to the destination destinationBlobClient.StartCopyFromUri(sourceBlobClient.Uri); // Check if the copy operation completed successfully WaitForCopyToComplete(destinationBlobClient); // Delete the source blob after a successful copy sourceBlobClient.DeleteIfExists(); } } catch(Exception ex) { } } static void WaitForCopyToComplete(BlobClient blobClient) { BlobProperties properties = blobClient.GetProperties(); while (properties.CopyStatus == CopyStatus.Pending) { Task.Delay(TimeSpan.FromSeconds(1)); properties = blobClient.GetProperties(); } } public void DeleteFile(string url) { BlobClient sourceBlobClient = _containerClient.GetBlobClient(url); sourceBlobClient.DeleteIfExists(); } } }