Skip to content

Commit

Permalink
feat: improve file download handling and update product image display…
Browse files Browse the repository at this point in the history
… logic
  • Loading branch information
baotoq committed Dec 10, 2024
1 parent a9cd4e5 commit 8ab1dc1
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public void MapEndpoint(IEndpointRouteBuilder builder)
{
builder.MapGet("/api/products/images/{url}", async (string url, IFileService fileService) =>
{
var stream = await fileService.DownloadFileAsync(url);
await using var stream = await fileService.DownloadFileAsync(url);

return TypedResults.File(stream, MediaTypeNames.Image.Jpeg);
}).Produces<Stream>(contentType: MediaTypeNames.Image.Jpeg);
Expand Down
9 changes: 4 additions & 5 deletions code/src/MicroCommerce.ApiService/Services/FileService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;

namespace MicroCommerce.ApiService.Services;

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

var memoryStream = new MemoryStream();
var response = await blobClient.DownloadToAsync(memoryStream, cancellationToken);
var response = await blobClient.DownloadStreamingAsync(null, cancellationToken);

if (response.IsError)
if (response.GetRawResponse().IsError)
{
_logger.LogError("Failed to download file {FileName} from blob storage {Info}", fileName, response.ToString());
throw new Exception($"Failed to download file {fileName}");
}

memoryStream.Position = 0;
return memoryStream;
return response.Value.Content;
}

public async Task CreateContainerIfNotExistsAsync(CancellationToken cancellationToken = default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<img src="images/@Product.ImageUrl" class="card-img-top" alt="@Product.Name">
<div class="card-body">
<h5 class="card-title">@Product.Name</h5>
<p class="card-text">$@Product.Price.ToString("C", new CultureInfo("en-US"))</p>
<p class="card-text">@Product.Price.ToString("C", new CultureInfo("en-US"))</p>
<button class="btn btn-primary" @onclick="() => AddToCart(Product)">
<i class="fas fa-cart-plus"></i> Add to Cart
</button>
Expand Down

0 comments on commit 8ab1dc1

Please sign in to comment.