using System; using Microsoft.Extensions.Options; using Azure.Storage.Blobs; using Landmark.OLA.Application.Configuration; using Landmark.OLA.Models.Loan; namespace Landmark.OLA.Application.DataAccess { public class BlobStorageAccess : IBlobStorageAccess { readonly BlobServiceClient _blobServiceClient; readonly string _containerName; readonly BlobContainerClient _containerClient; readonly BlobStorageConnectionSettings _settings; public BlobStorageAccess(IOptions settings) { _settings = settings.Value; _blobServiceClient = new BlobServiceClient($"AccountName={_settings.AccountName};AccountKey={_settings.AccountKey}"); _containerName = "documents"; _containerClient = _blobServiceClient.GetBlobContainerClient(_containerName); } public byte[] GetBlobAsBytes(LoanApplicationFile file) { if (file == null) { return []; } var blob = _containerClient.GetBlobClient(file.GetBlobPath()); var fileContent = blob.DownloadContent(); var fileBytes = fileContent.Value.Content.ToArray(); return fileBytes; } public string GetBlobAsBase64(LoanApplicationFile file) { var fileBytes = GetBlobAsBytes(file); return Convert.ToBase64String(fileBytes); } public bool SaveBlob(LoanApplicationFile file, byte[] fileBytes) { if(file == null || fileBytes == null || fileBytes.Length == 0) { return false; } BlobClient blobClient = _containerClient.GetBlobClient(file.GetBlobPath()); blobClient.Upload(new BinaryData(fileBytes)); blobClient.SetMetadata(file.GetMetadata()); return true; } public bool DeleteBlob(LoanApplicationFile file) { if (file == null) { return false; } BlobClient blobClient = _containerClient.GetBlobClient(file.GetBlobPath()); return blobClient.DeleteIfExists(); } } }