Skip to content

Commit 5cd18bc

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

File tree

23 files changed

+314
-23
lines changed

23 files changed

+314
-23
lines changed
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);

ManagedCode.Storage.Tests/FileHelper.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.IO;
3-
using ManagedCode.Storage.AspNetExtensions.Helpers;
43
using ManagedCode.Storage.Core;
4+
using ManagedCode.Storage.Core.Helpers;
55
using Microsoft.AspNetCore.Http;
66
using Microsoft.AspNetCore.Http.Internal;
77

ManagedCode.Storage.sln

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ManagedCode.Storage.FileSys
1717
EndProject
1818
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ManagedCode.Storage.AspNetExtensions", "ManagedCode.Storage.AspNetExtensions\ManagedCode.Storage.AspNetExtensions.csproj", "{852B0DBD-37F0-4DC0-B966-C284AE03C2F5}"
1919
EndProject
20+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{C812D2D8-1854-4043-94A9-4AA32D606D68}"
21+
EndProject
22+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApiSample", "Samples\WebApiSample\WebApiSample.csproj", "{39E5D805-04B4-442B-9B64-116A96041A93}"
23+
EndProject
24+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApiSample.Tests", "Samples\WebApiSample.Tests\WebApiSample.Tests.csproj", "{52B06D10-AB2C-4230-B90D-9ADBE73BC546}"
25+
EndProject
2026
Global
2127
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2228
Debug|Any CPU = Debug|Any CPU
@@ -51,11 +57,23 @@ Global
5157
{852B0DBD-37F0-4DC0-B966-C284AE03C2F5}.Debug|Any CPU.Build.0 = Debug|Any CPU
5258
{852B0DBD-37F0-4DC0-B966-C284AE03C2F5}.Release|Any CPU.ActiveCfg = Release|Any CPU
5359
{852B0DBD-37F0-4DC0-B966-C284AE03C2F5}.Release|Any CPU.Build.0 = Release|Any CPU
60+
{39E5D805-04B4-442B-9B64-116A96041A93}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
61+
{39E5D805-04B4-442B-9B64-116A96041A93}.Debug|Any CPU.Build.0 = Debug|Any CPU
62+
{39E5D805-04B4-442B-9B64-116A96041A93}.Release|Any CPU.ActiveCfg = Release|Any CPU
63+
{39E5D805-04B4-442B-9B64-116A96041A93}.Release|Any CPU.Build.0 = Release|Any CPU
64+
{52B06D10-AB2C-4230-B90D-9ADBE73BC546}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
65+
{52B06D10-AB2C-4230-B90D-9ADBE73BC546}.Debug|Any CPU.Build.0 = Debug|Any CPU
66+
{52B06D10-AB2C-4230-B90D-9ADBE73BC546}.Release|Any CPU.ActiveCfg = Release|Any CPU
67+
{52B06D10-AB2C-4230-B90D-9ADBE73BC546}.Release|Any CPU.Build.0 = Release|Any CPU
5468
EndGlobalSection
5569
GlobalSection(SolutionProperties) = preSolution
5670
HideSolutionNode = FALSE
5771
EndGlobalSection
5872
GlobalSection(ExtensibilityGlobals) = postSolution
5973
SolutionGuid = {A594F814-80A8-49D2-B751-B3A58869B30D}
6074
EndGlobalSection
75+
GlobalSection(NestedProjects) = preSolution
76+
{39E5D805-04B4-442B-9B64-116A96041A93} = {C812D2D8-1854-4043-94A9-4AA32D606D68}
77+
{52B06D10-AB2C-4230-B90D-9ADBE73BC546} = {C812D2D8-1854-4043-94A9-4AA32D606D68}
78+
EndGlobalSection
6179
EndGlobal
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
7+
<IsPackable>false</IsPackable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
12+
<PackageReference Include="xunit" Version="2.4.1" />
13+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
14+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
15+
<PrivateAssets>all</PrivateAssets>
16+
</PackageReference>
17+
<PackageReference Include="coverlet.collector" Version="3.1.0">
18+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
19+
<PrivateAssets>all</PrivateAssets>
20+
</PackageReference>
21+
</ItemGroup>
22+
23+
<ItemGroup>
24+
<ProjectReference Include="..\WebApiSample\WebApiSample.csproj" />
25+
</ItemGroup>
26+
27+
</Project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using ManagedCode.Storage.Aws;
2+
using Microsoft.AspNetCore.Mvc;
3+
4+
namespace WebApiSample.Controllers;
5+
6+
[ApiController]
7+
[Route("api/[controller]")]
8+
public class AWSStorageController : Controller
9+
{
10+
private readonly IAWSStorage _awsStorage;
11+
12+
public AWSStorageController(IAWSStorage awsStorage)
13+
{
14+
_awsStorage = awsStorage;
15+
}
16+
17+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using ManagedCode.Storage.Azure;
2+
using Microsoft.AspNetCore.Mvc;
3+
4+
namespace WebApiSample.Controllers;
5+
6+
[ApiController]
7+
[Route("api/[controller]")]
8+
public class AzureStorageController : Controller
9+
{
10+
private readonly IAzureStorage _azureStorage;
11+
12+
public AzureStorageController(IAzureStorage azureStorage)
13+
{
14+
_azureStorage = azureStorage;
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using ManagedCode.Storage.FileSystem;
2+
using Microsoft.AspNetCore.Mvc;
3+
4+
namespace WebApiSample.Controllers;
5+
6+
[ApiController]
7+
[Route("api/[controller]")]
8+
public class FileSystemStorageController : Controller
9+
{
10+
private readonly IFileSystemStorage _fileSystemStorage;
11+
12+
public FileSystemStorageController(IFileSystemStorage fileSystemStorage)
13+
{
14+
_fileSystemStorage = fileSystemStorage;
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using ManagedCode.Storage.Gcp;
2+
using Microsoft.AspNetCore.Mvc;
3+
4+
namespace WebApiSample.Controllers;
5+
6+
[ApiController]
7+
[Route("api/[controller]")]
8+
public class GCPStorageController : Controller
9+
{
10+
private readonly IGCPStorage _gcpStorage;
11+
12+
public GCPStorageController(IGCPStorage gcpStorage)
13+
{
14+
_gcpStorage = gcpStorage;
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using ManagedCode.Storage.Core;
2+
using Microsoft.AspNetCore.Mvc;
3+
4+
namespace WebApiSample.Controllers;
5+
6+
[ApiController]
7+
[Route("api/[controller]")]
8+
public class StorageController : Controller
9+
{
10+
private readonly IStorage _storage;
11+
12+
public StorageController(IStorage storage)
13+
{
14+
_storage = storage;
15+
}
16+
}

0 commit comments

Comments
 (0)