Skip to content

Commit eadbcb7

Browse files
committed
set Content-Type based on file extension
- Modified WriteAsync to detect MIME type from blob path using _mime.TryGetContentType - Falls back to "application/octet-stream" when no mapping is found - Uses BlobUploadOptions with BlobHttpHeaders to ensure Content-Type is stored - Fixes issue where browsers download images instead of rendering inline
1 parent ca8458d commit eadbcb7

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

FluentStorage.Azure.Blobs/AzureBlobStorage.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,23 @@
1414
using Azure.Storage.Sas;
1515
using Blobs;
1616
using Microsoft.Identity.Client;
17+
using Microsoft.AspNetCore.StaticFiles;
1718
using FluentStorage.Blobs;
1819
using FluentStorage.Azure.Blobs.Gen2.Model;
1920

2021
namespace FluentStorage.Azure.Blobs {
2122
//auth scenarios: https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/storage/Azure.Storage.Blobs/samples/Sample02_Auth.cs
2223

2324

24-
class AzureBlobStorage : IAzureBlobStorage {
25+
class AzureBlobStorage : IAzureBlobStorage {
2526

2627
private readonly BlobServiceClient _client;
2728
private readonly StorageSharedKeyCredential _sasSigningCredentials;
2829
private readonly string _containerName;
2930
private readonly ConcurrentDictionary<string, BlobContainerClient> _containerNameToContainerClient =
3031
new ConcurrentDictionary<string, BlobContainerClient>();
32+
private static readonly FileExtensionContentTypeProvider _mime =
33+
new FileExtensionContentTypeProvider();
3134

3235
public AzureBlobStorage(
3336
BlobServiceClient blobServiceClient,
@@ -129,9 +132,19 @@ public async Task WriteAsync(string fullPath, Stream dataStream,
129132

130133
BlockBlobClient client = container.GetBlockBlobClient(path);
131134

135+
string contentType;
136+
if (!_mime.TryGetContentType(path, out contentType)) {
137+
contentType = "application/octet-stream";
138+
}
132139
try {
140+
var options = new BlobUploadOptions {
141+
HttpHeaders = new BlobHttpHeaders {
142+
ContentType = contentType
143+
}
144+
};
133145
await client.UploadAsync(
134146
new StorageSourceStream(dataStream),
147+
options: options,
135148
cancellationToken: cancellationToken).ConfigureAwait(false);
136149
}
137150
catch (RequestFailedException ex) when (ex.ErrorCode == "OperationNotAllowedInCurrentState") {

FluentStorage.Azure.Blobs/FluentStorage.Azure.Blobs.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<ItemGroup>
3030
<PackageReference Include="Azure.Identity" Version="1.11.4" />
3131
<PackageReference Include="Azure.Storage.Blobs" Version="12.20.0" />
32+
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.3.0" />
3233
</ItemGroup>
3334

3435
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netstandard2.1' ">

0 commit comments

Comments
 (0)