Skip to content

Commit 626c6c1

Browse files
committed
Extensions
1 parent ba974be commit 626c6c1

File tree

2 files changed

+94
-35
lines changed

2 files changed

+94
-35
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using System.IO;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
using ManagedCode.Communication;
5+
using ManagedCode.MimeTypes;
6+
using ManagedCode.Storage.Core;
7+
using ManagedCode.Storage.Core.Models;
8+
using Microsoft.AspNetCore.Mvc;
9+
10+
namespace ManagedCode.Storage.Server.Extensions;
11+
12+
public static class ControllerDownloadExtensions
13+
{
14+
public static async Task<Result<FileResult>> DownloadFileAsync(
15+
this ControllerBase controller,
16+
IStorage storage,
17+
string blobName,
18+
CancellationToken cancellationToken = default)
19+
{
20+
var result = await storage.DownloadAsync(blobName, cancellationToken);
21+
if (result.IsFailed)
22+
return Result<FileResult>.Fail(result.Errors);
23+
24+
var fileStream = new FileStreamResult(result.Value!.FileStream,
25+
MimeHelper.GetMimeType(result.Value.FileInfo.Extension))
26+
{
27+
FileDownloadName = result.Value.Name
28+
};
29+
30+
return Result<FileResult>.Succeed(fileStream);
31+
}
32+
33+
public static async Task<Result<FileResult>> StreamVideoAsync(
34+
this ControllerBase controller,
35+
IStorage storage,
36+
string blobName,
37+
CancellationToken cancellationToken = default)
38+
{
39+
var result = await storage.DownloadAsync(blobName, cancellationToken);
40+
if (result.IsFailed)
41+
return Result<FileResult>.Fail(result.Errors);
42+
43+
var fileStream = new FileStreamResult(result.Value!.FileStream,
44+
MimeHelper.GetMimeType(result.Value.FileInfo.Extension))
45+
{
46+
EnableRangeProcessing = true,
47+
FileDownloadName = result.Value.Name
48+
};
49+
50+
return Result<FileResult>.Succeed(fileStream);
51+
}
52+
53+
public static async Task<Result<FileContentResult>> DownloadAsByteArrayAsync(
54+
this ControllerBase controller,
55+
IStorage storage,
56+
string blobName,
57+
CancellationToken cancellationToken = default)
58+
{
59+
var result = await storage.DownloadAsync(blobName, cancellationToken);
60+
if (result.IsFailed)
61+
return Result<FileContentResult>.Fail(result.Errors);
62+
63+
using var memoryStream = new MemoryStream();
64+
await result.Value!.FileStream.CopyToAsync(memoryStream, cancellationToken);
65+
66+
var fileContent = new FileContentResult(memoryStream.ToArray(),
67+
MimeHelper.GetMimeType(result.Value.FileInfo.Extension))
68+
{
69+
FileDownloadName = result.Value.Name
70+
};
71+
72+
return Result<FileContentResult>.Succeed(fileContent);
73+
}
74+
}

Integraions/ManagedCode.Storage.Server/Extensions/Controller/ControllerBaseExtensions.cs renamed to Integraions/ManagedCode.Storage.Server/Extensions/Controller/ControllerUploadExtensions.cs

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,28 @@
1414

1515
namespace ManagedCode.Storage.Server.Extensions;
1616

17-
public static class ControllerBaseExtensions
17+
public static class ControllerUploadExtensions
1818
{
1919
private const int DefaultMultipartBoundaryLengthLimit = 70;
20+
private const int MinLengthForLargeFile = 256 * 1024;
2021

21-
public static async Task<Result<BlobMetadata>> UploadFromFormFileAsync(
22+
public static async Task<Result<BlobMetadata>> UploadFormFileAsync(
2223
this ControllerBase controller,
2324
IStorage storage,
2425
IFormFile file,
2526
UploadOptions? options = null,
2627
CancellationToken cancellationToken = default)
2728
{
28-
if (options == null)
29+
options ??= new UploadOptions(file.FileName, mimeType: file.ContentType);
30+
31+
if (file.Length > MinLengthForLargeFile)
2932
{
30-
options = new UploadOptions
31-
{
32-
FileName = file.FileName,
33-
MimeType = file.ContentType
34-
};
33+
var localFile = await file.ToLocalFileAsync(cancellationToken);
34+
return await storage.UploadAsync(localFile.FileInfo, options, cancellationToken);
3535
}
36-
37-
return await storage.UploadToStorageAsync(file, options, cancellationToken);
36+
37+
await using var stream = file.OpenReadStream();
38+
return await storage.UploadAsync(stream, options, cancellationToken);
3839
}
3940

4041
public static async Task<Result<BlobMetadata>> UploadFromBrowserFileAsync(
@@ -44,16 +45,16 @@ public static async Task<Result<BlobMetadata>> UploadFromBrowserFileAsync(
4445
UploadOptions? options = null,
4546
CancellationToken cancellationToken = default)
4647
{
47-
if (options == null)
48+
options ??= new UploadOptions(file.Name, mimeType: file.ContentType);
49+
50+
if (file.Size > MinLengthForLargeFile)
4851
{
49-
options = new UploadOptions
50-
{
51-
FileName = file.Name,
52-
MimeType = file.ContentType
53-
};
52+
var localFile = await file.ToLocalFileAsync(cancellationToken);
53+
return await storage.UploadAsync(localFile.FileInfo, options, cancellationToken);
5454
}
55-
56-
return await storage.UploadToStorageAsync(file, options);
55+
56+
await using var stream = file.OpenReadStream();
57+
return await storage.UploadAsync(stream, options, cancellationToken);
5758
}
5859

5960
public static async Task<Result<BlobMetadata>> UploadFromStreamAsync(
@@ -83,14 +84,7 @@ public static async Task<Result<BlobMetadata>> UploadFromStreamAsync(
8384
var fileName = contentDisposition.FileName.Value;
8485
var contentType = section.ContentType;
8586

86-
if (options == null)
87-
{
88-
options = new UploadOptions
89-
{
90-
FileName = fileName,
91-
MimeType = contentType
92-
};
93-
}
87+
options ??= new UploadOptions(fileName, mimeType: contentType);
9488

9589
using var memoryStream = new MemoryStream();
9690
await section.Body.CopyToAsync(memoryStream, cancellationToken);
@@ -104,13 +98,4 @@ public static async Task<Result<BlobMetadata>> UploadFromStreamAsync(
10498

10599
return Result<BlobMetadata>.Fail(HttpStatusCode.BadRequest, "No file found in request");
106100
}
107-
108-
public static async Task<Result<FileResult>> DownloadAsFileResultAsync(
109-
this ControllerBase controller,
110-
IStorage storage,
111-
string blobName,
112-
CancellationToken cancellationToken = default)
113-
{
114-
return await storage.DownloadAsFileResult(blobName, cancellationToken);
115-
}
116101
}

0 commit comments

Comments
 (0)