Skip to content

Commit

Permalink
Updated methods and fixed tests (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
dtymakhov authored Jul 22, 2022
1 parent f0ce4d5 commit 9a447cd
Show file tree
Hide file tree
Showing 46 changed files with 1,518 additions and 1,996 deletions.
4 changes: 2 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageReadmeFile>README.md</PackageReadmeFile>
<Product>Managed Code - Storage</Product>
<Version>1.2.1</Version>
<PackageVersion>1.2.1</PackageVersion>
<Version>2.0.0</Version>
<PackageVersion>2.0.0</PackageVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
Expand Down
2 changes: 1 addition & 1 deletion ManagedCode.Storage.AspNetExtensions/FormFileExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using ManagedCode.Storage.Core;
using ManagedCode.Storage.Core.Models;
using Microsoft.AspNetCore.Http;

namespace ManagedCode.Storage.AspNetExtensions;
Expand Down

This file was deleted.

66 changes: 26 additions & 40 deletions ManagedCode.Storage.AspNetExtensions/StorageExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using ManagedCode.Communication;
using ManagedCode.MimeTypes;
using ManagedCode.Storage.AspNetExtensions.Options;
using ManagedCode.Storage.Core;
using ManagedCode.Storage.Core.Models;
using Microsoft.AspNetCore.Http;
Expand All @@ -17,42 +15,25 @@ public static class StorageExtensions
{
private const int MinLengthForLargeFile = 256 * 1024;

public static async Task<BlobMetadata> UploadToStorageAsync(this IStorage storage, IFormFile formFile, UploadToStorageOptions? options = null,
public static async Task<Result<BlobMetadata>> UploadToStorageAsync(this IStorage storage, IFormFile formFile, UploadOptions? options = null,
CancellationToken cancellationToken = default)
{
options ??= new UploadToStorageOptions();

var extension = Path.GetExtension(formFile.FileName);

BlobMetadata blobMetadata = new()
{
Name = options.UseRandomName ? "" : formFile.FileName,
MimeType = formFile.ContentType,
};
options ??= new UploadOptions(fileName: formFile.FileName, mimeType: formFile.ContentType);

if (formFile.Length > MinLengthForLargeFile)
{
var localFile = await formFile.ToLocalFileAsync(cancellationToken);
await storage.UploadAsync(localFile.FileInfo, cancellationToken);
return await storage.UploadAsync(localFile.FileInfo, options, cancellationToken);
}
else
{
using (var stream = formFile.OpenReadStream())
{
await storage.UploadAsync(stream, uploadOptions =>
{
uploadOptions.FileName = options.UseRandomName ? "" : formFile.FileName;
uploadOptions.MimeType = formFile.ContentType;

} , cancellationToken);
}
using (var stream = formFile.OpenReadStream())
{
return await storage.UploadAsync(stream, options, cancellationToken);
}

return blobMetadata;
}

public static async IAsyncEnumerable<BlobMetadata> UploadToStorageAsync(this IStorage storage, IFormFileCollection formFiles,
UploadToStorageOptions? options = null,
public static async IAsyncEnumerable<Result<BlobMetadata>> UploadToStorageAsync(this IStorage storage, IFormFileCollection formFiles,
UploadOptions? options = null,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
foreach (var formFile in formFiles)
Expand All @@ -61,34 +42,39 @@ public static async IAsyncEnumerable<BlobMetadata> UploadToStorageAsync(this ISt
}
}

public static async Task<FileResult?> DownloadAsFileResult(this IStorage storage, string blobName, CancellationToken cancellationToken = default)
public static async Task<Result<FileResult>> DownloadAsFileResult(this IStorage storage, string blobName,
CancellationToken cancellationToken = default)
{
var localFile = await storage.DownloadAsync(blobName, cancellationToken);
var result = await storage.DownloadAsync(blobName, cancellationToken);

if (localFile is null)
if (result.IsError)
{
return null;
return Result<FileResult>.Fail(result.Error);
}

return new FileStreamResult(localFile.Value.FileStream, MimeHelper.GetMimeType(localFile.Value.FileInfo.Extension))
var fileStream = new FileStreamResult(result.Value!.FileStream, MimeHelper.GetMimeType(result.Value.FileInfo.Extension))
{
FileDownloadName = localFile.Value.FileName
FileDownloadName = result.Value.FileName
};

return Result<FileResult>.Succeed(fileStream);
}

public static async Task<FileResult?> DownloadAsFileResult(this IStorage storage, BlobMetadata blobMetadata,
public static async Task<Result<FileResult>> DownloadAsFileResult(this IStorage storage, BlobMetadata blobMetadata,
CancellationToken cancellationToken = default)
{
var localFile = await storage.DownloadAsync(blobMetadata.Name, cancellationToken);
var result = await storage.DownloadAsync(blobMetadata.Name, cancellationToken);

if (localFile is null)
if (result.IsError)
{
return null;
return Result.Fail<FileResult>(result.Error);
}

return new FileStreamResult(localFile.Value.FileStream, MimeHelper.GetMimeType(localFile.Value.FileInfo.Extension))
var fileStream = new FileStreamResult(result.Value!.FileStream, MimeHelper.GetMimeType(result.Value.FileInfo.Extension))
{
FileDownloadName = localFile.Value.FileName
FileDownloadName = result.Value.FileName
};

return Result<FileResult>.Succeed(fileStream);
}
}
Loading

0 comments on commit 9a447cd

Please sign in to comment.