Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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();
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<None Remove="microsoft.jpeg" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Azure.Storage.Blobs" Version="12.13.0" />
<PackageReference Include="SixLabors.ImageSharp" Version="2.1.3" />
</ItemGroup>

<ItemGroup>
<Content Update="wwwroot\img\microsoft.jpeg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

</Project>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

This file was deleted.