| | 1 | | using Azure.Storage.Blobs.Models; |
| | 2 | | using Azure.Storage.Blobs; |
| | 3 | | using MRA.Infrastructure.Settings; |
| | 4 | | using SixLabors.ImageSharp; |
| | 5 | | using MRA.Infrastructure.Storage.Connection; |
| | 6 | |
|
| | 7 | | namespace MRA.Infrastructure.Storage; |
| | 8 | |
|
| | 9 | | public class AzureStorageProvider : IStorageProvider |
| | 10 | | { |
| | 11 | | private readonly string blobContainer; |
| | 12 | | private readonly string blobPath; |
| | 13 | | private readonly IAzureStorageConnection _connection; |
| | 14 | |
|
| | 15 | |
|
| 8 | 16 | | public AzureStorageProvider(AppSettings config, IAzureStorageConnection connection) |
| | 17 | | { |
| 8 | 18 | | blobContainer = config.AzureStorage.BlobStorageContainer; |
| 8 | 19 | | blobPath = config.AzureStorage.BlobPath; |
| 8 | 20 | | _connection = connection; |
| 8 | 21 | | } |
| | 22 | |
|
| | 23 | | public async Task<List<BlobFileInfo>> ListBlobFilesAsync() |
| | 24 | | { |
| 0 | 25 | | var containerClient = _connection.GetContainer(blobContainer); |
| 0 | 26 | | return await _connection.GetListBlobsAsync(blobContainer).Select(model => ConvertToModel(containerClient, model) |
| 0 | 27 | | } |
| | 28 | |
|
| 1 | 29 | | public string GetBlobURL() => blobPath; |
| | 30 | |
|
| | 31 | | private BlobFileInfo ConvertToModel(BlobContainerClient containerClient, BlobItem blobItem) |
| | 32 | | { |
| 0 | 33 | | return new BlobFileInfo |
| 0 | 34 | | { |
| 0 | 35 | | Name = blobItem.Name, |
| 0 | 36 | | Url = $"{containerClient.Uri}/{blobItem.Name}" |
| 0 | 37 | | }; |
| | 38 | | } |
| | 39 | |
|
| | 40 | | public async Task<bool> ExistsBlob(string rutaBlob) |
| | 41 | | { |
| 2 | 42 | | return await _connection.BlobExists(blobContainer, rutaBlob); |
| 2 | 43 | | } |
| | 44 | |
|
| | 45 | | public async Task<bool> ResizeAndSave(string path, string blobName, int width) |
| | 46 | | { |
| 1 | 47 | | using var imagenOriginal = await path.LoadImageAsync(); |
| 1 | 48 | | imagenOriginal.ResizeImageIfNecessary(width); |
| 1 | 49 | | return await UploadImageToBlob(blobName, imagenOriginal); |
| 1 | 50 | | } |
| | 51 | |
|
| | 52 | | public async Task<bool> ResizeAndSave(MemoryStream inputStream, string blobName, int width) |
| | 53 | | { |
| 1 | 54 | | using var imagenOriginal = await inputStream.LoadImageAsync(); |
| 1 | 55 | | imagenOriginal.ResizeImageIfNecessary(width); |
| 1 | 56 | | return await UploadImageToBlob(blobName, imagenOriginal); |
| 1 | 57 | | } |
| | 58 | |
|
| | 59 | | private async Task<bool> UploadImageToBlob(string blobPath, Image image) |
| | 60 | | { |
| 2 | 61 | | using var memoryStream = new MemoryStream(); |
| 2 | 62 | | await image.SaveAsPngAsync(memoryStream); |
| | 63 | |
|
| 2 | 64 | | memoryStream.Seek(0, SeekOrigin.Begin); |
| | 65 | |
|
| 2 | 66 | | return await _connection.UploadImageAsync(blobContainer, blobPath, memoryStream) is not null; |
| 2 | 67 | | } |
| | 68 | |
|
| | 69 | | public async Task<bool> Save(Stream stream, string blobLocation, string blobName) |
| | 70 | | { |
| 1 | 71 | | return await _connection.UploadAsync( |
| 1 | 72 | | containerName: blobContainer, |
| 1 | 73 | | blobPath: Path.Combine(blobLocation, blobName), |
| 1 | 74 | | stream) is not null; |
| 1 | 75 | | } |
| | 76 | |
|
| | 77 | | public string CrearThumbnailName(string imagePath) |
| | 78 | | { |
| 1 | 79 | | string fileName = Path.GetFileNameWithoutExtension(imagePath); |
| 1 | 80 | | string thumbnailFileName = $"{fileName}_tn.png"; |
| | 81 | |
|
| 1 | 82 | | string folder = Path.GetDirectoryName(imagePath) ?? string.Empty; |
| 1 | 83 | | string newPath = Path.Combine(folder, thumbnailFileName).Replace('\\', '/'); |
| | 84 | |
|
| 1 | 85 | | return newPath; |
| | 86 | | } |
| | 87 | | } |