Skip to content

Commit 7b18f07

Browse files
committed
Enhance storage functionality with cancellation support and metadata updates
+ Nuget
1 parent 8c53309 commit 7b18f07

34 files changed

+761
-328
lines changed

Directory.Build.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
<RepositoryUrl>https://github.com/managedcode/Storage</RepositoryUrl>
2727
<PackageProjectUrl>https://github.com/managedcode/Storage</PackageProjectUrl>
2828
<Product>Managed Code - Storage</Product>
29-
<Version>9.0.3</Version>
30-
<PackageVersion>9.0.3</PackageVersion>
29+
<Version>9.0.4</Version>
30+
<PackageVersion>9.0.4</PackageVersion>
3131

3232
</PropertyGroup>
3333

Integraions/ManagedCode.Storage.Client.SignalR/ManagedCode.Storage.Client.SignalR.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
<ItemGroup>
2121
<PackageReference Include="ManagedCode.MimeTypes" Version="1.0.2" />
22-
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="9.0.1" />
22+
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="9.0.3" />
2323
</ItemGroup>
2424

2525
</Project>

Integraions/ManagedCode.Storage.Server/Extensions/Storage/StorageFromFileExtensions.cs

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,41 +13,23 @@ namespace ManagedCode.Storage.Server.Extensions.Storage;
1313

1414
public static class StorageFromFileExtensions
1515
{
16-
private const int MinLengthForLargeFile = 256 * 1024;
17-
1816
public static async Task<Result<BlobMetadata>> UploadToStorageAsync(this IStorage storage, IFormFile formFile, UploadOptions? options = null,
1917
CancellationToken cancellationToken = default)
2018
{
2119
options ??= new UploadOptions(formFile.FileName, mimeType: formFile.ContentType);
2220

23-
if (formFile.Length > MinLengthForLargeFile)
24-
{
25-
var localFile = await formFile.ToLocalFileAsync(cancellationToken);
26-
return await storage.UploadAsync(localFile.FileInfo, options, cancellationToken);
27-
}
28-
29-
await using (var stream = formFile.OpenReadStream())
30-
{
31-
return await storage.UploadAsync(stream, options, cancellationToken);
32-
}
21+
await using var stream = formFile.OpenReadStream();
22+
return await storage.UploadAsync(stream, options, cancellationToken);
3323
}
3424

3525
public static async Task<Result<BlobMetadata>> UploadToStorageAsync(this IStorage storage, IFormFile formFile, Action<UploadOptions> options,
3626
CancellationToken cancellationToken = default)
3727
{
3828
var newOptions = new UploadOptions(formFile.FileName, mimeType: formFile.ContentType);
3929
options.Invoke(newOptions);
40-
41-
if (formFile.Length > MinLengthForLargeFile)
42-
{
43-
var localFile = await formFile.ToLocalFileAsync(cancellationToken);
44-
return await storage.UploadAsync(localFile.FileInfo, newOptions, cancellationToken);
45-
}
46-
47-
await using (var stream = formFile.OpenReadStream())
48-
{
49-
return await storage.UploadAsync(stream, newOptions, cancellationToken);
50-
}
30+
31+
await using var stream = formFile.OpenReadStream();
32+
return await storage.UploadAsync(stream, newOptions, cancellationToken);
5133
}
5234

5335
public static async IAsyncEnumerable<Result<BlobMetadata>> UploadToStorageAsync(this IStorage storage, IFormFileCollection formFiles,

ManagedCode.Storage.Core/BaseStorage.cs

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,36 @@ public abstract class BaseStorage<T, TOptions> : IStorage<T, TOptions> where TOp
1515
{
1616
protected bool IsContainerCreated;
1717
protected TOptions StorageOptions;
18+
private readonly SemaphoreSlim _semaphoreSlim = new(1, 1);
1819

1920
protected BaseStorage(TOptions storageOptions)
2021
{
2122
Contract.Assert(storageOptions is not null);
2223
StorageOptions = storageOptions!;
24+
// ReSharper disable once VirtualMemberCallInConstructor
2325
StorageClient = CreateStorageClient();
2426
}
2527

2628
public async Task<Result> CreateContainerAsync(CancellationToken cancellationToken = default)
2729
{
28-
var result = await CreateContainerInternalAsync(cancellationToken);
29-
cancellationToken.ThrowIfCancellationRequested();
30-
IsContainerCreated = result.IsSuccess;
31-
return result;
30+
try
31+
{
32+
await _semaphoreSlim.WaitAsync(cancellationToken);
33+
cancellationToken.ThrowIfCancellationRequested();
34+
35+
var result = await CreateContainerInternalAsync(cancellationToken);
36+
cancellationToken.ThrowIfCancellationRequested();
37+
IsContainerCreated = result.IsSuccess;
38+
return result;
39+
}
40+
catch (Exception ex)
41+
{
42+
return Result.Fail(ex);
43+
}
44+
finally
45+
{
46+
_semaphoreSlim.Release();
47+
}
3248
}
3349

3450
public abstract Task<Result> RemoveContainerAsync(CancellationToken cancellationToken = default);
@@ -89,15 +105,15 @@ public Task<Result<BlobMetadata>> UploadAsync(FileInfo fileInfo, Action<UploadOp
89105
public Task<Result<BlobMetadata>> UploadAsync(Stream stream, UploadOptions options, CancellationToken cancellationToken = default)
90106
{
91107
if (string.IsNullOrWhiteSpace(options.MimeType))
92-
options.MimeType = MimeHelper.BIN;
108+
options.MimeType = MimeHelper.GetMimeType(options.FileName);
93109

94110
return UploadInternalAsync(stream, SetUploadOptions(options), cancellationToken);
95111
}
96112

97113
public Task<Result<BlobMetadata>> UploadAsync(byte[] data, UploadOptions options, CancellationToken cancellationToken = default)
98114
{
99115
if (string.IsNullOrWhiteSpace(options.MimeType))
100-
options.MimeType = MimeHelper.BIN;
116+
options.MimeType = MimeHelper.GetMimeType(options.FileName);
101117

102118
return UploadInternalAsync(new MemoryStream(data), SetUploadOptions(options), cancellationToken);
103119
}
@@ -269,13 +285,16 @@ public Task<Result> SetStorageOptions(Action<TOptions> options, CancellationToke
269285

270286
protected abstract T CreateStorageClient();
271287

272-
protected Task<Result> EnsureContainerExist()
288+
protected Task<Result> EnsureContainerExist(CancellationToken cancellationToken = default)
273289
{
290+
if (StorageClient is null)
291+
throw new InvalidOperationException("Storage client is not initialized.");
292+
274293
if (IsContainerCreated)
275294
return Result.Succeed()
276295
.AsTask();
277296

278-
return CreateContainerAsync();
297+
return CreateContainerAsync(cancellationToken);
279298
}
280299

281300
protected UploadOptions SetUploadOptions(UploadOptions options)

ManagedCode.Storage.Core/ManagedCode.Storage.Core.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
<ItemGroup>
1616
<PackageReference Include="ManagedCode.Communication" Version="9.0.0" />
1717
<PackageReference Include="ManagedCode.MimeTypes" Version="1.0.2" />
18-
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.1" />
19-
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.1" />
18+
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.3" />
19+
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.3" />
2020
</ItemGroup>
2121

2222
</Project>

ManagedCode.Storage.Core/Models/BaseOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace ManagedCode.Storage.Core.Models;
44

55
public abstract class BaseOptions
66
{
7-
public string FileName { get; set; } = $"{Guid.NewGuid():N}";
7+
public string FileName { get; set; } = string.Empty;
88
public string? Directory { get; set; }
99

1010
// TODO: Check this

ManagedCode.Storage.Core/Models/BlobMetadata.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ public class BlobMetadata
1313
public DateTimeOffset CreatedOn { get; set; }
1414
public string? Container { get; set; }
1515
public string? MimeType { get; set; }
16-
public long Length { get; set; }
16+
public ulong Length { get; set; }
1717
}

ManagedCode.Storage.Core/Models/MetadataOptions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ public static MetadataOptions FromBaseOptions(BaseOptions options)
66
{
77
return new MetadataOptions { FileName = options.FileName, Directory = options.Directory };
88
}
9+
10+
public string ETag { get; set; } = string.Empty;
911
}

0 commit comments

Comments
 (0)