Skip to content

Commit 5cd18bc

Browse files
authored
Added cascade for sample project. Some fixes for AspNetExtensions (#18)
1 parent cc8e914 commit 5cd18bc

23 files changed

Lines changed: 314 additions & 23 deletions

File tree

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Generic;
22
using System.IO;
3+
using System.Runtime.CompilerServices;
34
using System.Threading;
45
using System.Threading.Tasks;
56
using ManagedCode.Storage.Core;
@@ -12,23 +13,19 @@ public static class FormFileExtensions
1213
public static async Task<LocalFile> ToLocalFileAsync(this IFormFile formFile, CancellationToken cancellationToken = default)
1314
{
1415
var tempPath = Path.GetTempPath();
15-
LocalFile localFile = new($"{tempPath}/{formFile.Name}");
16+
LocalFile localFile = new($"{tempPath}/{formFile.FileName}");
1617

1718
await formFile.CopyToAsync(localFile.FileStream, cancellationToken);
1819

1920
return localFile;
2021
}
2122

22-
public static async Task<IEnumerable<LocalFile>> ToLocalFilesAsync(this IFormFileCollection formFileCollection,
23-
CancellationToken cancellationToken = default)
23+
public static async IAsyncEnumerable<LocalFile> ToLocalFilesAsync(this IFormFileCollection formFileCollection,
24+
[EnumeratorCancellation] CancellationToken cancellationToken = default)
2425
{
25-
List<LocalFile> localFiles = new();
26-
2726
foreach (var formFile in formFileCollection)
2827
{
29-
localFiles.Add(await formFile.ToLocalFileAsync(cancellationToken));
28+
yield return await formFile.ToLocalFileAsync(cancellationToken);
3029
}
31-
32-
return localFiles;
3330
}
3431
}

ManagedCode.Storage.AspNetExtensions/StorageExtensions.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.IO;
4+
using System.Linq;
5+
using System.Runtime.CompilerServices;
36
using System.Threading;
47
using System.Threading.Tasks;
58
using ManagedCode.Storage.AspNetExtensions.Options;
69
using ManagedCode.Storage.Core;
10+
using ManagedCode.Storage.Core.Helpers;
711
using ManagedCode.Storage.Core.Models;
812
using Microsoft.AspNetCore.Http;
913
using Microsoft.AspNetCore.Mvc;
10-
using ManagedCode.Storage.AspNetExtensions.Helpers;
1114

1215
namespace ManagedCode.Storage.AspNetExtensions;
1316

1417
public static class StorageExtensions
1518
{
1619
private const int MinLengthForLargeFile = 256 * 1024;
1720

18-
public static async Task<string> UploadToStorageAsync(this IStorage storage, IFormFile formFile, UploadToStorageOptions? options = null,
21+
public static async Task<BlobMetadata> UploadToStorageAsync(this IStorage storage, IFormFile formFile, UploadToStorageOptions? options = null,
1922
CancellationToken cancellationToken = default)
2023
{
2124
options ??= new UploadToStorageOptions();
@@ -43,7 +46,17 @@ public static async Task<string> UploadToStorageAsync(this IStorage storage, IFo
4346
}
4447
}
4548

46-
return blobMetadata.Name;
49+
return blobMetadata;
50+
}
51+
52+
public static async IAsyncEnumerable<BlobMetadata> UploadToStorageAsync(this IStorage storage, IFormFileCollection formFiles,
53+
UploadToStorageOptions? options = null,
54+
[EnumeratorCancellation] CancellationToken cancellationToken = default)
55+
{
56+
foreach (var formFile in formFiles)
57+
{
58+
yield return await storage.UploadToStorageAsync(formFile, options, cancellationToken);
59+
}
4760
}
4861

4962
public static async Task<FileResult> DownloadAsFileResult(this IStorage storage, string blobName, CancellationToken cancellationToken = default)

ManagedCode.Storage.Aws/AWSStorage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public async Task<BlobMetadata> GetBlobAsync(string blobName, CancellationToken
161161

162162
return new BlobMetadata
163163
{
164-
Name = blobName
164+
Name = blobName,
165165
//Container = containerName,
166166
//Length = objectMetaResponse.Headers.ContentLength,
167167
//ETag = objectMetaResponse.ETag,

ManagedCode.Storage.Azure/AzureStorage.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ public async Task<BlobMetadata> GetBlobAsync(string blobName, CancellationToken
145145
return new BlobMetadata
146146
{
147147
Name = blobClient.Name,
148-
Uri = blobClient.Uri
148+
Uri = blobClient.Uri,
149+
Container = blobClient.BlobContainerName,
149150
};
150151
}
151152

ManagedCode.Storage.AspNetExtensions/Helpers/MimeHelper.cs renamed to ManagedCode.Storage.Core/Helpers/MimeHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33

4-
namespace ManagedCode.Storage.AspNetExtensions.Helpers;
4+
namespace ManagedCode.Storage.Core.Helpers;
55

66
public class MimeHelper
77
{

ManagedCode.Storage.Core/Models/BlobMetadata.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace ManagedCode.Storage.Core.Models;
55
public class BlobMetadata
66
{
77
public string Name { get; set; } = null!;
8-
public Uri? Uri { get; set; } = null!;
8+
public Uri? Uri { get; set; }
99
public string? Container { get; set; }
1010
public string? ContentType { get; set; }
1111
public bool Rewrite { get; set; }

ManagedCode.Storage.FileSystem/FileSystemStorage.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Threading;
66
using System.Threading.Tasks;
77
using ManagedCode.Storage.Core;
8+
using ManagedCode.Storage.Core.Helpers;
89
using ManagedCode.Storage.Core.Models;
910
using ManagedCode.Storage.FileSystem.Options;
1011

@@ -156,12 +157,13 @@ public Task<BlobMetadata> GetBlobAsync(string blobName, CancellationToken cancel
156157
var result = new BlobMetadata
157158
{
158159
Name = fileInfo.Name,
159-
Uri = new Uri(Path.Combine(_path, blobName))
160+
Uri = new Uri(Path.Combine(_path, blobName)),
161+
ContentType = MimeHelper.GetMimeType(fileInfo.Extension)
160162
};
161163
return Task.FromResult(result);
162164
}
163165

164-
return null;
166+
return Task.FromResult<BlobMetadata>(null);
165167
}
166168

167169
public async IAsyncEnumerable<BlobMetadata> GetBlobListAsync(

ManagedCode.Storage.Tests/AWS/AWSStorageTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ protected override ServiceProvider ConfigureServices()
1616
{
1717
var services = new ServiceCollection();
1818

19-
//aws libarary overwrites property values. you should only create configurations this way.
19+
// AWS library overwrites property values. you should only create configurations this way.
2020
var awsConfig = new AmazonS3Config
2121
{
2222
RegionEndpoint = RegionEndpoint.EUWest1,
2323
ForcePathStyle = true,
2424
UseHttp = true,
25-
ServiceURL = "http://localhost:4566" //this is the default port for the aws s3 emulator, must be last in the list
25+
ServiceURL = "http://localhost:4566" // this is the default port for the aws s3 emulator, must be last in the list
2626
};
2727

2828
services.AddAWSStorageAsDefault(opt =>

ManagedCode.Storage.Tests/AspNetExtensions/FormFileExtensionsTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public async Task ToLocalFilesAsync_SmallFiles()
5858
}
5959

6060
// Act
61-
var localFiles = (await collection.ToLocalFilesAsync()).ToList();
61+
var localFiles = await collection.ToLocalFilesAsync().ToListAsync();
6262

6363
// Assert
6464
localFiles.Count.Should().Be(filesCount);

ManagedCode.Storage.Tests/AspNetExtensions/StorageExtensionsTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
using System.Threading.Tasks;
44
using FluentAssertions;
55
using ManagedCode.Storage.AspNetExtensions;
6-
using ManagedCode.Storage.AspNetExtensions.Helpers;
76
using ManagedCode.Storage.AspNetExtensions.Options;
87
using ManagedCode.Storage.Core;
8+
using ManagedCode.Storage.Core.Helpers;
99
using ManagedCode.Storage.Core.Models;
1010
using ManagedCode.Storage.FileSystem.Extensions;
1111
using Microsoft.Extensions.DependencyInjection;
@@ -89,7 +89,7 @@ public async Task UploadToStorageAsync_WithRandomName()
8989

9090
// Assert
9191
localFile.FileInfo.Length.Should().Be(formFile.Length);
92-
localFile.FileName.Should().Be(newFileName);
92+
localFile.FileName.Should().Be(newFileName.Name);
9393
localFile.FileName.Should().NotBe(formFile.FileName);
9494

9595
await Storage.DeleteAsync(fileName);

0 commit comments

Comments
 (0)