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
Expand Up @@ -17,14 +17,14 @@ public SampleController(IStorageFactory storageFactory)
}

[HttpGet]
public async Task<IEnumerable<string>> Get()
public async ValueTask<IEnumerable<string>> Get()
{
var summaries = await this.sharedAssets.ListAsync("summaries", "*.txt", recursive: true, withMetadata: false);
return summaries.Select(x => x.Path);
}

[HttpGet]
public async Task<string> Get(string path)
public async ValueTask<string> Get(string path)
{
var summary = await this.sharedAssets.GetAsync(path);
return await summary.ReadAllTextAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ public ValuesController(TemplatesStore templates)
}

[HttpGet]
public async Task<IEnumerable<string>> Get()
public async ValueTask<IEnumerable<string>> Get()
{
return new string[] { await templates.Store.ReadAllTextAsync("json.json"), "value2" };
}

[HttpGet("files")]
public async Task<IEnumerable<string>> Get(int id)
public async ValueTask<IEnumerable<string>> Get(int id)
{
var files = await templates.Store.ListAsync("");
return files.Select(x => x.PublicUrl);
Expand Down
29 changes: 14 additions & 15 deletions src/GeekLearning.Storage.Azure/AzureStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public Task InitAsync()
return this.container.Value.CreateIfNotExistsAsync(accessType, null, null);
}

public async Task<IFileReference[]> ListAsync(string path, bool recursive, bool withMetadata)
public async ValueTask<IFileReference[]> ListAsync(string path, bool recursive, bool withMetadata)
{
if (string.IsNullOrWhiteSpace(path))
{
Expand Down Expand Up @@ -75,7 +75,7 @@ public async Task<IFileReference[]> ListAsync(string path, bool recursive, bool
return results.OfType<ICloudBlob>().Select(blob => new Internal.AzureFileReference(blob, withMetadata: withMetadata)).ToArray();
}

public async Task<IFileReference[]> ListAsync(string path, string searchPattern, bool recursive, bool withMetadata)
public async ValueTask<IFileReference[]> ListAsync(string path, string searchPattern, bool recursive, bool withMetadata)
{
if (string.IsNullOrWhiteSpace(path))
{
Expand Down Expand Up @@ -121,12 +121,12 @@ public async Task<IFileReference[]> ListAsync(string path, string searchPattern,
return filteredResults.Files.Select(x => pathMap[path + x.Path]).ToArray();
}

public async Task<IFileReference> GetAsync(IPrivateFileReference file, bool withMetadata)
public async ValueTask<IFileReference> GetAsync(IPrivateFileReference file, bool withMetadata)
{
return await this.InternalGetAsync(file, withMetadata);
}

public async Task<IFileReference> GetAsync(Uri uri, bool withMetadata)
public async ValueTask<IFileReference> GetAsync(Uri uri, bool withMetadata)
{
return await this.InternalGetAsync(uri, withMetadata);
}
Expand All @@ -137,33 +137,33 @@ public async Task DeleteAsync(IPrivateFileReference file)
await fileReference.DeleteAsync();
}

public async Task<Stream> ReadAsync(IPrivateFileReference file)
public async ValueTask<Stream> ReadAsync(IPrivateFileReference file)
{
var fileReference = await this.InternalGetAsync(file);
return await fileReference.ReadInMemoryAsync();
}

public async Task<byte[]> ReadAllBytesAsync(IPrivateFileReference file)
public async ValueTask<byte[]> ReadAllBytesAsync(IPrivateFileReference file)
{
var fileReference = await this.InternalGetAsync(file);
return await fileReference.ReadAllBytesAsync();
}

public async Task<string> ReadAllTextAsync(IPrivateFileReference file)
public async ValueTask<string> ReadAllTextAsync(IPrivateFileReference file)
{
var fileReference = await this.InternalGetAsync(file);
return await fileReference.ReadAllTextAsync();
}

public async Task<IFileReference> SaveAsync(byte[] data, IPrivateFileReference file, string contentType)
public async ValueTask<IFileReference> SaveAsync(byte[] data, IPrivateFileReference file, string contentType)
{
using (var stream = new SyncMemoryStream(data, 0, data.Length))
{
return await this.SaveAsync(stream, file, contentType);
}
}

public async Task<IFileReference> SaveAsync(Stream data, IPrivateFileReference file, string contentType)
public async ValueTask<IFileReference> SaveAsync(Stream data, IPrivateFileReference file, string contentType)
{
var blockBlob = this.container.Value.GetBlockBlobReference(file.Path);

Expand All @@ -182,7 +182,7 @@ public async Task<IFileReference> SaveAsync(Stream data, IPrivateFileReference f
return reference;
}

public Task<string> GetSharedAccessSignatureAsync(ISharedAccessPolicy policy)
public ValueTask<string> GetSharedAccessSignatureAsync(ISharedAccessPolicy policy)
{
var adHocPolicy = new SharedAccessBlobPolicy()
{
Expand All @@ -191,7 +191,7 @@ public Task<string> GetSharedAccessSignatureAsync(ISharedAccessPolicy policy)
Permissions = FromGenericToAzure(policy.Permissions),
};

return Task.FromResult(this.container.Value.GetSharedAccessSignature(adHocPolicy));
return new ValueTask<string>(this.container.Value.GetSharedAccessSignature(adHocPolicy));
}

internal static SharedAccessBlobPermissions FromGenericToAzure(SharedAccessPermissions permissions)
Expand Down Expand Up @@ -231,18 +231,17 @@ internal static SharedAccessBlobPermissions FromGenericToAzure(SharedAccessPermi
return result;
}

private async Task<Internal.AzureFileReference> InternalGetAsync(IPrivateFileReference file, bool withMetadata = false)
private async ValueTask<Internal.AzureFileReference> InternalGetAsync(IPrivateFileReference file, bool withMetadata = false)
{
var azureFile = file as Internal.AzureFileReference;
if (azureFile != null)
if (file is Internal.AzureFileReference azureFile)
{
return azureFile;
}

return await this.InternalGetAsync(new Uri(file.Path, UriKind.Relative), withMetadata);
}

private async Task<Internal.AzureFileReference> InternalGetAsync(Uri uri, bool withMetadata)
private async ValueTask<Internal.AzureFileReference> InternalGetAsync(Uri uri, bool withMetadata)
{
try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Options" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.FileSystemGlobbing" Version="1.1.1" />
<PackageReference Include="WindowsAzure.Storage" Version="8.1.1" />
<PackageReference Include="WindowsAzure.Storage" Version="8.1.3" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net45' ">
Expand Down
12 changes: 6 additions & 6 deletions src/GeekLearning.Storage.Azure/Internal/AzureFileReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ public Task DeleteAsync()
return this.CloudBlob.DeleteAsync();
}

public async Task<Stream> ReadAsync()
public async ValueTask<Stream> ReadAsync()
{
return await this.ReadInMemoryAsync();
}

public async Task<MemoryStream> ReadInMemoryAsync()
public async ValueTask<MemoryStream> ReadInMemoryAsync()
{
var memoryStream = new MemoryStream();
await this.CloudBlob.DownloadRangeToStreamAsync(memoryStream, null, null);
Expand All @@ -66,15 +66,15 @@ public async Task ReadToStreamAsync(Stream targetStream)
await this.CloudBlob.DownloadRangeToStreamAsync(targetStream, null, null);
}

public async Task<string> ReadAllTextAsync()
public async ValueTask<string> ReadAllTextAsync()
{
using (var reader = new StreamReader(await this.CloudBlob.OpenReadAsync(AccessCondition.GenerateEmptyCondition(), new BlobRequestOptions(), new OperationContext())))
{
return await reader.ReadToEndAsync();
}
}

public async Task<byte[]> ReadAllBytesAsync()
public async ValueTask<byte[]> ReadAllBytesAsync()
{
return (await this.ReadInMemoryAsync()).ToArray();
}
Expand All @@ -84,7 +84,7 @@ public Task SavePropertiesAsync()
return this.propertiesLazy.Value.SaveAsync();
}

public Task<string> GetSharedAccessSignature(ISharedAccessPolicy policy)
public ValueTask<string> GetSharedAccessSignature(ISharedAccessPolicy policy)
{
var adHocPolicy = new SharedAccessBlobPolicy()
{
Expand All @@ -93,7 +93,7 @@ public Task<string> GetSharedAccessSignature(ISharedAccessPolicy policy)
Permissions = AzureStore.FromGenericToAzure(policy.Permissions),
};

return Task.FromResult(this.CloudBlob.GetSharedAccessSignature(adHocPolicy));
return new ValueTask<string>(this.CloudBlob.GetSharedAccessSignature(adHocPolicy));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ public ExtendedPropertiesProvider(
this.options = options.Value;
}

public Task<FileExtendedProperties> GetExtendedPropertiesAsync(string storeAbsolutePath, IPrivateFileReference file)
public ValueTask<FileExtendedProperties> GetExtendedPropertiesAsync(string storeAbsolutePath, IPrivateFileReference file)
{
var extendedPropertiesPath = this.GetExtendedPropertiesPath(storeAbsolutePath, file);
if (!File.Exists(extendedPropertiesPath))
{
return Task.FromResult(new FileExtendedProperties());
return new ValueTask<FileExtendedProperties>(new FileExtendedProperties());
}

var content = File.ReadAllText(extendedPropertiesPath);
return Task.FromResult(JsonConvert.DeserializeObject<FileExtendedProperties>(content));
return new ValueTask<FileExtendedProperties>(JsonConvert.DeserializeObject<FileExtendedProperties>(content));
}

public Task SaveExtendedPropertiesAsync(string storeAbsolutePath, IPrivateFileReference file, FileExtendedProperties extendedProperties)
Expand Down
27 changes: 13 additions & 14 deletions src/GeekLearning.Storage.FileSystem/FileSystemStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public Task InitAsync()
return Task.FromResult(0);
}

public async Task<IFileReference[]> ListAsync(string path, bool recursive, bool withMetadata)
public async ValueTask<IFileReference[]> ListAsync(string path, bool recursive, bool withMetadata)
{
var directoryPath = (string.IsNullOrEmpty(path) || path == "/" || path == "\\") ? this.AbsolutePath : Path.Combine(this.AbsolutePath, path);

Expand All @@ -57,7 +57,7 @@ public async Task<IFileReference[]> ListAsync(string path, bool recursive, bool
return result.ToArray();
}

public async Task<IFileReference[]> ListAsync(string path, string searchPattern, bool recursive, bool withMetadata)
public async ValueTask<IFileReference[]> ListAsync(string path, string searchPattern, bool recursive, bool withMetadata)
{
var directoryPath = (string.IsNullOrEmpty(path) || path == "/" || path == "\\") ? this.AbsolutePath : Path.Combine(this.AbsolutePath, path);

Expand All @@ -81,12 +81,12 @@ public async Task<IFileReference[]> ListAsync(string path, string searchPattern,
return result.ToArray();
}

public async Task<IFileReference> GetAsync(IPrivateFileReference file, bool withMetadata)
public async ValueTask<IFileReference> GetAsync(IPrivateFileReference file, bool withMetadata)
{
return await this.InternalGetAsync(file, withMetadata);
}

public async Task<IFileReference> GetAsync(Uri uri, bool withMetadata)
public async ValueTask<IFileReference> GetAsync(Uri uri, bool withMetadata)
{
if (uri.IsAbsoluteUri)
{
Expand All @@ -102,33 +102,33 @@ public async Task DeleteAsync(IPrivateFileReference file)
await fileReference.DeleteAsync();
}

public async Task<Stream> ReadAsync(IPrivateFileReference file)
public async ValueTask<Stream> ReadAsync(IPrivateFileReference file)
{
var fileReference = await this.InternalGetAsync(file);
return await fileReference.ReadAsync();
}

public async Task<byte[]> ReadAllBytesAsync(IPrivateFileReference file)
public async ValueTask<byte[]> ReadAllBytesAsync(IPrivateFileReference file)
{
var fileReference = await this.InternalGetAsync(file);
return await fileReference.ReadAllBytesAsync();
}

public async Task<string> ReadAllTextAsync(IPrivateFileReference file)
public async ValueTask<string> ReadAllTextAsync(IPrivateFileReference file)
{
var fileReference = await this.InternalGetAsync(file);
return await fileReference.ReadAllTextAsync();
}

public async Task<IFileReference> SaveAsync(byte[] data, IPrivateFileReference file, string contentType)
public async ValueTask<IFileReference> SaveAsync(byte[] data, IPrivateFileReference file, string contentType)
{
using (var stream = new MemoryStream(data, 0, data.Length))
{
return await this.SaveAsync(stream, file, contentType);
}
}

public async Task<IFileReference> SaveAsync(Stream data, IPrivateFileReference file, string contentType)
public async ValueTask<IFileReference> SaveAsync(Stream data, IPrivateFileReference file, string contentType)
{
var fileReference = await this.InternalGetAsync(file, withMetadata: true, checkIfExists: false);
this.EnsurePathExists(fileReference.FileSystemPath);
Expand All @@ -148,23 +148,22 @@ public async Task<IFileReference> SaveAsync(Stream data, IPrivateFileReference f
return fileReference;
}

public Task<string> GetSharedAccessSignatureAsync(ISharedAccessPolicy policy)
public ValueTask<string> GetSharedAccessSignatureAsync(ISharedAccessPolicy policy)
{
throw new NotSupportedException();
}

private async Task<Internal.FileSystemFileReference> InternalGetAsync(IPrivateFileReference file, bool withMetadata = false, bool checkIfExists = true)
private async ValueTask<Internal.FileSystemFileReference> InternalGetAsync(IPrivateFileReference file, bool withMetadata = false, bool checkIfExists = true)
{
var fileSystemFile = file as Internal.FileSystemFileReference;
if (fileSystemFile != null)
if (file is Internal.FileSystemFileReference fileSystemFile)
{
return fileSystemFile;
}

return await this.InternalGetAsync(file.Path, withMetadata, checkIfExists);
}

private async Task<Internal.FileSystemFileReference> InternalGetAsync(string path, bool withMetadata, bool checkIfExists = true)
private async ValueTask<Internal.FileSystemFileReference> InternalGetAsync(string path, bool withMetadata, bool checkIfExists = true)
{
var fullPath = Path.Combine(this.AbsolutePath, path);
if (checkIfExists && !File.Exists(fullPath))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public interface IExtendedPropertiesProvider
{
Task<Internal.FileExtendedProperties> GetExtendedPropertiesAsync(string storeAbsolutePath, IPrivateFileReference file);
ValueTask<Internal.FileExtendedProperties> GetExtendedPropertiesAsync(string storeAbsolutePath, IPrivateFileReference file);

Task SaveExtendedPropertiesAsync(string storeAbsolutePath, IPrivateFileReference file, Internal.FileExtendedProperties extendedProperties);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,19 @@ public Task DeleteAsync()
return Task.FromResult(true);
}

public Task<byte[]> ReadAllBytesAsync()
public ValueTask<byte[]> ReadAllBytesAsync()
{
return Task.FromResult(File.ReadAllBytes(this.FileSystemPath));
return new ValueTask<byte[]>(File.ReadAllBytes(this.FileSystemPath));
}

public Task<string> ReadAllTextAsync()
public ValueTask<string> ReadAllTextAsync()
{
return Task.FromResult(File.ReadAllText(this.FileSystemPath));
return new ValueTask<string>(File.ReadAllText(this.FileSystemPath));
}

public Task<Stream> ReadAsync()
public ValueTask<Stream> ReadAsync()
{
Stream stream = File.OpenRead(this.FileSystemPath);
return Task.FromResult(stream);
return new ValueTask<Stream>(File.OpenRead(this.FileSystemPath));
}

public async Task ReadToStreamAsync(Stream targetStream)
Expand Down Expand Up @@ -105,7 +104,7 @@ public Task SavePropertiesAsync()
(this.Properties as FileSystemFileProperties).ExtendedProperties);
}

public Task<string> GetSharedAccessSignature(ISharedAccessPolicy policy)
public ValueTask<string> GetSharedAccessSignature(ISharedAccessPolicy policy)
{
throw new NotSupportedException();
}
Expand Down
1 change: 1 addition & 0 deletions src/GeekLearning.Storage/GeekLearning.Storage.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Options" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="1.1.2" />
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.3.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading