|
| 1 | +using Azure.Storage.Blobs; |
| 2 | +using Azure.Storage.Blobs.Models; |
| 3 | + |
| 4 | +namespace BookStore.ApiService.Services; |
| 5 | + |
| 6 | +public class BlobStorageService(BlobServiceClient blobServiceClient) |
| 7 | +{ |
| 8 | + const string ContainerName = "book-covers"; |
| 9 | + static readonly string[] SupportedExtensions = ["jpg", "png", "webp"]; |
| 10 | + |
| 11 | + public async Task<string> UploadBookCoverAsync( |
| 12 | + Guid bookId, |
| 13 | + Stream imageStream, |
| 14 | + string contentType, |
| 15 | + CancellationToken cancellationToken = default) |
| 16 | + { |
| 17 | + var container = await GetContainerAsync(cancellationToken); |
| 18 | + |
| 19 | + // Determine file extension from content type |
| 20 | + var extension = contentType switch |
| 21 | + { |
| 22 | + "image/jpeg" => "jpg", |
| 23 | + "image/png" => "png", |
| 24 | + "image/webp" => "webp", |
| 25 | + _ => "jpg" // Default to jpg for safety |
| 26 | + }; |
| 27 | + |
| 28 | + var blobName = $"{bookId}.{extension}"; |
| 29 | + var blob = container.GetBlobClient(blobName); |
| 30 | + |
| 31 | + await blob.UploadAsync( |
| 32 | + imageStream, |
| 33 | + new BlobHttpHeaders { ContentType = contentType }, |
| 34 | + cancellationToken: cancellationToken); |
| 35 | + |
| 36 | + return blob.Uri.ToString(); |
| 37 | + } |
| 38 | + |
| 39 | + public async Task<BlobDownloadResult> GetBookCoverAsync( |
| 40 | + Guid bookId, |
| 41 | + CancellationToken cancellationToken = default) |
| 42 | + { |
| 43 | + var container = await GetContainerAsync(cancellationToken); |
| 44 | + |
| 45 | + // Try to find the blob with any supported extension |
| 46 | + foreach (var ext in SupportedExtensions) |
| 47 | + { |
| 48 | + var blob = container.GetBlobClient($"{bookId}.{ext}"); |
| 49 | + if (await blob.ExistsAsync(cancellationToken)) |
| 50 | + { |
| 51 | + return await blob.DownloadContentAsync(cancellationToken); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + throw new FileNotFoundException($"Book cover not found for book {bookId}"); |
| 56 | + } |
| 57 | + |
| 58 | + public async Task DeleteBookCoverAsync( |
| 59 | + Guid bookId, |
| 60 | + CancellationToken cancellationToken = default) |
| 61 | + { |
| 62 | + var container = await GetContainerAsync(cancellationToken); |
| 63 | + |
| 64 | + // Delete blob with any supported extension |
| 65 | + foreach (var ext in SupportedExtensions) |
| 66 | + { |
| 67 | + var blob = container.GetBlobClient($"{bookId}.{ext}"); |
| 68 | + await blob.DeleteIfExistsAsync(cancellationToken: cancellationToken); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + async Task<BlobContainerClient> GetContainerAsync( |
| 73 | + CancellationToken cancellationToken = default) |
| 74 | + { |
| 75 | + var container = blobServiceClient.GetBlobContainerClient(ContainerName); |
| 76 | + await container.CreateIfNotExistsAsync( |
| 77 | + PublicAccessType.Blob, |
| 78 | + cancellationToken: cancellationToken); |
| 79 | + return container; |
| 80 | + } |
| 81 | +} |
0 commit comments