Skip to content

Commit 8ab1dc1

Browse files
committed
feat: improve file download handling and update product image display logic
1 parent a9cd4e5 commit 8ab1dc1

File tree

3 files changed

+6
-7
lines changed

3 files changed

+6
-7
lines changed

code/src/MicroCommerce.ApiService/Features/Products/GetProductImage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public void MapEndpoint(IEndpointRouteBuilder builder)
1313
{
1414
builder.MapGet("/api/products/images/{url}", async (string url, IFileService fileService) =>
1515
{
16-
var stream = await fileService.DownloadFileAsync(url);
16+
await using var stream = await fileService.DownloadFileAsync(url);
1717

1818
return TypedResults.File(stream, MediaTypeNames.Image.Jpeg);
1919
}).Produces<Stream>(contentType: MediaTypeNames.Image.Jpeg);

code/src/MicroCommerce.ApiService/Services/FileService.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Azure.Storage.Blobs;
2+
using Azure.Storage.Blobs.Models;
23

34
namespace MicroCommerce.ApiService.Services;
45

@@ -42,17 +43,15 @@ public async Task<Stream> DownloadFileAsync(string fileName, CancellationToken c
4243
var containerClient = _blobServiceClient.GetBlobContainerClient(ContainerName);
4344
var blobClient = containerClient.GetBlobClient(fileName);
4445

45-
var memoryStream = new MemoryStream();
46-
var response = await blobClient.DownloadToAsync(memoryStream, cancellationToken);
46+
var response = await blobClient.DownloadStreamingAsync(null, cancellationToken);
4747

48-
if (response.IsError)
48+
if (response.GetRawResponse().IsError)
4949
{
5050
_logger.LogError("Failed to download file {FileName} from blob storage {Info}", fileName, response.ToString());
5151
throw new Exception($"Failed to download file {fileName}");
5252
}
5353

54-
memoryStream.Position = 0;
55-
return memoryStream;
54+
return response.Value.Content;
5655
}
5756

5857
public async Task CreateContainerIfNotExistsAsync(CancellationToken cancellationToken = default)

code/src/MicroCommerce.Web/Components/Shared/ProductCard.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<img src="images/@Product.ImageUrl" class="card-img-top" alt="@Product.Name">
66
<div class="card-body">
77
<h5 class="card-title">@Product.Name</h5>
8-
<p class="card-text">$@Product.Price.ToString("C", new CultureInfo("en-US"))</p>
8+
<p class="card-text">@Product.Price.ToString("C", new CultureInfo("en-US"))</p>
99
<button class="btn btn-primary" @onclick="() => AddToCart(Product)">
1010
<i class="fas fa-cart-plus"></i> Add to Cart
1111
</button>

0 commit comments

Comments
 (0)