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
27 changes: 26 additions & 1 deletion src/GeekLearning.Storage.Azure/Internal/AzureFileProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;

public class AzureFileProperties : IFileProperties
{
private const string DefaultCacheControl = "max-age=300, must-revalidate";
private readonly ICloudBlob cloudBlob;
private readonly Dictionary<string, string> decodedMetadata;

public AzureFileProperties(ICloudBlob cloudBlob)
{
Expand All @@ -16,6 +20,15 @@ public AzureFileProperties(ICloudBlob cloudBlob)
{
this.cloudBlob.Properties.CacheControl = DefaultCacheControl;
}

if (this.cloudBlob.Metadata != null)
{
decodedMetadata = this.cloudBlob.Metadata.ToDictionary(m => m.Key, m => WebUtility.HtmlDecode(m.Value));
}
else
{
decodedMetadata = new Dictionary<string, string>();
}
}

public DateTimeOffset? LastModified => this.cloudBlob.Properties.LastModified;
Expand All @@ -36,6 +49,18 @@ public string CacheControl
set { this.cloudBlob.Properties.CacheControl = value; }
}

public IDictionary<string, string> Metadata => this.cloudBlob.Metadata;
public IDictionary<string, string> Metadata => this.decodedMetadata;

internal async Task SaveAsync()
{
await this.cloudBlob.SetPropertiesAsync();

foreach (var meta in this.decodedMetadata)
{
this.cloudBlob.Metadata[meta.Key] = WebUtility.HtmlEncode(meta.Value);
}

await this.cloudBlob.SetMetadataAsync();
}
}
}
9 changes: 4 additions & 5 deletions src/GeekLearning.Storage.Azure/Internal/AzureFileReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

public class AzureFileReference : IFileReference
{
private Lazy<IFileProperties> propertiesLazy;
private Lazy<AzureFileProperties> propertiesLazy;

public AzureFileReference(string path, ICloudBlob cloudBlob, bool withMetadata)
{
this.Path = path;
this.CloudBlob = cloudBlob;
this.propertiesLazy = new Lazy<IFileProperties>(() =>
this.propertiesLazy = new Lazy<AzureFileProperties>(() =>
{
if (withMetadata && cloudBlob.Metadata != null && cloudBlob.Properties != null)
{
Expand Down Expand Up @@ -84,10 +84,9 @@ public async Task<byte[]> ReadAllBytesAsync()
return (await this.ReadInMemoryAsync()).ToArray();
}

public async Task SavePropertiesAsync()
public Task SavePropertiesAsync()
{
await this.CloudBlob.SetPropertiesAsync();
await this.CloudBlob.SetMetadataAsync();
return this.propertiesLazy.Value.SaveAsync();
}
}
}
24 changes: 24 additions & 0 deletions tests/GeekLearning.Storage.Integration.Test/UpdateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,30 @@ public async Task SaveMetatadaRoundtrip(string storeName)
Assert.Equal(id, actualId);
}

[Theory(DisplayName = nameof(SaveEncodedMetatadaRoundtrip)), InlineData("Store1"), InlineData("Store2"), InlineData("Store3"), InlineData("Store4"), InlineData("Store5"), InlineData("Store6")]
public async Task SaveEncodedMetatadaRoundtrip(string storeName)
{
var storageFactory = this.storeFixture.Services.GetRequiredService<IStorageFactory>();

var store = storageFactory.GetStore(storeName);

var testFile = "Metadata/TextFile.txt";

var file = await store.GetAsync(testFile, withMetadata: true);

var name = "ï";

file.Properties.Metadata["name"] = name;

await file.SavePropertiesAsync();

file = await store.GetAsync(testFile, withMetadata: true);

var actualName = file.Properties.Metadata["name"];

Assert.Equal(name, actualName);
}

[Theory(DisplayName = nameof(ListMetatadaRoundtrip)), InlineData("Store1"), InlineData("Store2"), InlineData("Store3"), InlineData("Store4"), InlineData("Store5"), InlineData("Store6")]
public async Task ListMetatadaRoundtrip(string storeName)
{
Expand Down