Skip to content

Commit 9a447cd

Browse files
authored
Updated methods and fixed tests (#28)
1 parent f0ce4d5 commit 9a447cd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1518
-1996
lines changed

Directory.Build.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1414
<PackageReadmeFile>README.md</PackageReadmeFile>
1515
<Product>Managed Code - Storage</Product>
16-
<Version>1.2.1</Version>
17-
<PackageVersion>1.2.1</PackageVersion>
16+
<Version>2.0.0</Version>
17+
<PackageVersion>2.0.0</PackageVersion>
1818
</PropertyGroup>
1919
<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">
2020
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>

ManagedCode.Storage.AspNetExtensions/FormFileExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using System.Runtime.CompilerServices;
44
using System.Threading;
55
using System.Threading.Tasks;
6-
using ManagedCode.Storage.Core;
6+
using ManagedCode.Storage.Core.Models;
77
using Microsoft.AspNetCore.Http;
88

99
namespace ManagedCode.Storage.AspNetExtensions;

ManagedCode.Storage.AspNetExtensions/Options/UploadToStorageOptions.cs

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 26 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
using System;
21
using System.Collections.Generic;
3-
using System.IO;
42
using System.Runtime.CompilerServices;
53
using System.Threading;
64
using System.Threading.Tasks;
5+
using ManagedCode.Communication;
76
using ManagedCode.MimeTypes;
8-
using ManagedCode.Storage.AspNetExtensions.Options;
97
using ManagedCode.Storage.Core;
108
using ManagedCode.Storage.Core.Models;
119
using Microsoft.AspNetCore.Http;
@@ -17,42 +15,25 @@ public static class StorageExtensions
1715
{
1816
private const int MinLengthForLargeFile = 256 * 1024;
1917

20-
public static async Task<BlobMetadata> UploadToStorageAsync(this IStorage storage, IFormFile formFile, UploadToStorageOptions? options = null,
18+
public static async Task<Result<BlobMetadata>> UploadToStorageAsync(this IStorage storage, IFormFile formFile, UploadOptions? options = null,
2119
CancellationToken cancellationToken = default)
2220
{
23-
options ??= new UploadToStorageOptions();
24-
25-
var extension = Path.GetExtension(formFile.FileName);
26-
27-
BlobMetadata blobMetadata = new()
28-
{
29-
Name = options.UseRandomName ? "" : formFile.FileName,
30-
MimeType = formFile.ContentType,
31-
};
21+
options ??= new UploadOptions(fileName: formFile.FileName, mimeType: formFile.ContentType);
3222

3323
if (formFile.Length > MinLengthForLargeFile)
3424
{
3525
var localFile = await formFile.ToLocalFileAsync(cancellationToken);
36-
await storage.UploadAsync(localFile.FileInfo, cancellationToken);
26+
return await storage.UploadAsync(localFile.FileInfo, options, cancellationToken);
3727
}
38-
else
39-
{
40-
using (var stream = formFile.OpenReadStream())
41-
{
42-
await storage.UploadAsync(stream, uploadOptions =>
43-
{
44-
uploadOptions.FileName = options.UseRandomName ? "" : formFile.FileName;
45-
uploadOptions.MimeType = formFile.ContentType;
4628

47-
} , cancellationToken);
48-
}
29+
using (var stream = formFile.OpenReadStream())
30+
{
31+
return await storage.UploadAsync(stream, options, cancellationToken);
4932
}
50-
51-
return blobMetadata;
5233
}
5334

54-
public static async IAsyncEnumerable<BlobMetadata> UploadToStorageAsync(this IStorage storage, IFormFileCollection formFiles,
55-
UploadToStorageOptions? options = null,
35+
public static async IAsyncEnumerable<Result<BlobMetadata>> UploadToStorageAsync(this IStorage storage, IFormFileCollection formFiles,
36+
UploadOptions? options = null,
5637
[EnumeratorCancellation] CancellationToken cancellationToken = default)
5738
{
5839
foreach (var formFile in formFiles)
@@ -61,34 +42,39 @@ public static async IAsyncEnumerable<BlobMetadata> UploadToStorageAsync(this ISt
6142
}
6243
}
6344

64-
public static async Task<FileResult?> DownloadAsFileResult(this IStorage storage, string blobName, CancellationToken cancellationToken = default)
45+
public static async Task<Result<FileResult>> DownloadAsFileResult(this IStorage storage, string blobName,
46+
CancellationToken cancellationToken = default)
6547
{
66-
var localFile = await storage.DownloadAsync(blobName, cancellationToken);
48+
var result = await storage.DownloadAsync(blobName, cancellationToken);
6749

68-
if (localFile is null)
50+
if (result.IsError)
6951
{
70-
return null;
52+
return Result<FileResult>.Fail(result.Error);
7153
}
7254

73-
return new FileStreamResult(localFile.Value.FileStream, MimeHelper.GetMimeType(localFile.Value.FileInfo.Extension))
55+
var fileStream = new FileStreamResult(result.Value!.FileStream, MimeHelper.GetMimeType(result.Value.FileInfo.Extension))
7456
{
75-
FileDownloadName = localFile.Value.FileName
57+
FileDownloadName = result.Value.FileName
7658
};
59+
60+
return Result<FileResult>.Succeed(fileStream);
7761
}
7862

79-
public static async Task<FileResult?> DownloadAsFileResult(this IStorage storage, BlobMetadata blobMetadata,
63+
public static async Task<Result<FileResult>> DownloadAsFileResult(this IStorage storage, BlobMetadata blobMetadata,
8064
CancellationToken cancellationToken = default)
8165
{
82-
var localFile = await storage.DownloadAsync(blobMetadata.Name, cancellationToken);
66+
var result = await storage.DownloadAsync(blobMetadata.Name, cancellationToken);
8367

84-
if (localFile is null)
68+
if (result.IsError)
8569
{
86-
return null;
70+
return Result.Fail<FileResult>(result.Error);
8771
}
8872

89-
return new FileStreamResult(localFile.Value.FileStream, MimeHelper.GetMimeType(localFile.Value.FileInfo.Extension))
73+
var fileStream = new FileStreamResult(result.Value!.FileStream, MimeHelper.GetMimeType(result.Value.FileInfo.Extension))
9074
{
91-
FileDownloadName = localFile.Value.FileName
75+
FileDownloadName = result.Value.FileName
9276
};
77+
78+
return Result<FileResult>.Succeed(fileStream);
9379
}
9480
}

0 commit comments

Comments
 (0)