diff --git a/aspnetcore/fundamentals/minimal-apis/resultsStream/7.0-samples/ResultsStreamSample/Program.cs b/aspnetcore/fundamentals/minimal-apis/resultsStream/7.0-samples/ResultsStreamSample/Program.cs new file mode 100644 index 000000000000..2c6efcf0a1d4 --- /dev/null +++ b/aspnetcore/fundamentals/minimal-apis/resultsStream/7.0-samples/ResultsStreamSample/Program.cs @@ -0,0 +1,61 @@ +using Azure.Storage.Blobs; +using Microsoft.Net.Http.Headers; +using SixLabors.ImageSharp; +using SixLabors.ImageSharp.Formats.Jpeg; +using SixLabors.ImageSharp.Processing; + +var builder = WebApplication.CreateBuilder(args); + +var app = builder.Build(); + +app.MapGet("/process-image", (HttpContext http, CancellationToken token) => +{ + http.Response.Headers.CacheControl = $"public,max-age={TimeSpan.FromHours(24).TotalSeconds}"; + return Results.Stream(stream => ProcessImage(stream, token), "image/jpeg"); +}); + +async Task ProcessImage(Stream stream, CancellationToken token) +{ + using var image = await Image.LoadAsync("wwwroot/img/microsoft.jpeg", token); + int width = image.Width / 2; + int height = image.Height / 2; + image.Mutate(x => x.Resize(width, height)); + await image.SaveAsync(stream, JpegFormat.Instance, cancellationToken: token); +} + +// For local development use Azure Storage Emulator and Azure Storage Explorer +// https://docs.microsoft.com/en-us/azure/storage/common/storage-use-emulator +// https://azure.microsoft.com/en-us/features/storage-explorer/ +app.MapGet("/stream-image", async (CancellationToken token) => +{ + BlobContainerClient blobContainerClient = new BlobContainerClient("UseDevelopmentStorage=true", "pictures"); + BlobClient blobClient = blobContainerClient.GetBlobClient("microsoft.jpeg"); + return Results.Stream(await blobClient.OpenReadAsync(cancellationToken: token), "image/jpeg"); +}); + +app.MapGet("/stream-video", async (HttpContext http, CancellationToken token) => +{ + BlobContainerClient blobContainerClient = new BlobContainerClient("UseDevelopmentStorage=true", "videos"); + BlobClient blobClient = blobContainerClient.GetBlobClient("earth.mp4"); + + var properties = await blobClient.GetPropertiesAsync(cancellationToken: token); + + DateTimeOffset lastModified = properties.Value.LastModified; + long length = properties.Value.ContentLength; + + long etagHash = lastModified.ToFileTime() ^ length; + var entityTag = new EntityTagHeaderValue('\"' + Convert.ToString(etagHash, 16) + '\"'); + + http.Response.Headers.CacheControl = "public,max-age=86400"; + + // This is an alias for File(Stream, string, string?, DateTimeOffset?, EntityTagHeaderValue?, bool); + // When fileDownloadName: "rotating-earth.mp4" is added the Content-Disposition header is set to, 'attachment; filename="rotating-earth.mp4"' + // Else it will show the video inline in the browser + return Results.Stream(await blobClient.OpenReadAsync(cancellationToken: token), + contentType: "video/mp4", + lastModified: lastModified, + entityTag: entityTag, + enableRangeProcessing: true); +}); + +app.Run(); diff --git a/aspnetcore/fundamentals/minimal-apis/resultsStream/7.0-samples/ResultsStreamSample/ResultsStreamSample.csproj b/aspnetcore/fundamentals/minimal-apis/resultsStream/7.0-samples/ResultsStreamSample/ResultsStreamSample.csproj new file mode 100644 index 000000000000..f8194fa82a16 --- /dev/null +++ b/aspnetcore/fundamentals/minimal-apis/resultsStream/7.0-samples/ResultsStreamSample/ResultsStreamSample.csproj @@ -0,0 +1,24 @@ + + + + net7.0 + enable + enable + + + + + + + + + + + + + + PreserveNewest + + + + diff --git a/aspnetcore/fundamentals/minimal-apis/resultsStream/7.0-samples/ResultsStreamSample/wwwroot/img/microsoft.jpeg b/aspnetcore/fundamentals/minimal-apis/resultsStream/7.0-samples/ResultsStreamSample/wwwroot/img/microsoft.jpeg new file mode 100644 index 000000000000..4348947d839e Binary files /dev/null and b/aspnetcore/fundamentals/minimal-apis/resultsStream/7.0-samples/ResultsStreamSample/wwwroot/img/microsoft.jpeg differ diff --git a/aspnetcore/fundamentals/minimal-apis/resultsStream/7.0-samples/readme.txt b/aspnetcore/fundamentals/minimal-apis/resultsStream/7.0-samples/readme.txt deleted file mode 100644 index 655bdceab454..000000000000 --- a/aspnetcore/fundamentals/minimal-apis/resultsStream/7.0-samples/readme.txt +++ /dev/null @@ -1 +0,0 @@ -Create sample code here then delete this file. \ No newline at end of file