Skip to content

Commit ba974be

Browse files
committed
wip
1 parent 1ab3add commit ba974be

File tree

16 files changed

+257
-339
lines changed

16 files changed

+257
-339
lines changed

Integraions/ManagedCode.Storage.Server/BaseController.cs

Lines changed: 0 additions & 250 deletions
This file was deleted.
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
using System.IO;
2+
using System.Net;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
using ManagedCode.Communication;
6+
using ManagedCode.Storage.Core;
7+
using ManagedCode.Storage.Core.Models;
8+
using ManagedCode.Storage.Server.Helpers;
9+
using Microsoft.AspNetCore.Components.Forms;
10+
using Microsoft.AspNetCore.Http;
11+
using Microsoft.AspNetCore.Mvc;
12+
using Microsoft.AspNetCore.WebUtilities;
13+
using Microsoft.Net.Http.Headers;
14+
15+
namespace ManagedCode.Storage.Server.Extensions;
16+
17+
public static class ControllerBaseExtensions
18+
{
19+
private const int DefaultMultipartBoundaryLengthLimit = 70;
20+
21+
public static async Task<Result<BlobMetadata>> UploadFromFormFileAsync(
22+
this ControllerBase controller,
23+
IStorage storage,
24+
IFormFile file,
25+
UploadOptions? options = null,
26+
CancellationToken cancellationToken = default)
27+
{
28+
if (options == null)
29+
{
30+
options = new UploadOptions
31+
{
32+
FileName = file.FileName,
33+
MimeType = file.ContentType
34+
};
35+
}
36+
37+
return await storage.UploadToStorageAsync(file, options, cancellationToken);
38+
}
39+
40+
public static async Task<Result<BlobMetadata>> UploadFromBrowserFileAsync(
41+
this ControllerBase controller,
42+
IStorage storage,
43+
IBrowserFile file,
44+
UploadOptions? options = null,
45+
CancellationToken cancellationToken = default)
46+
{
47+
if (options == null)
48+
{
49+
options = new UploadOptions
50+
{
51+
FileName = file.Name,
52+
MimeType = file.ContentType
53+
};
54+
}
55+
56+
return await storage.UploadToStorageAsync(file, options);
57+
}
58+
59+
public static async Task<Result<BlobMetadata>> UploadFromStreamAsync(
60+
this ControllerBase controller,
61+
IStorage storage,
62+
HttpRequest request,
63+
UploadOptions? options = null,
64+
CancellationToken cancellationToken = default)
65+
{
66+
if (!StreamHelper.IsMultipartContentType(request.ContentType))
67+
{
68+
return Result<BlobMetadata>.Fail(HttpStatusCode.BadRequest, "Not a multipart request");
69+
}
70+
71+
var boundary = StreamHelper.GetBoundary(
72+
MediaTypeHeaderValue.Parse(request.ContentType),
73+
DefaultMultipartBoundaryLengthLimit);
74+
75+
var multipartReader = new MultipartReader(boundary, request.Body);
76+
var section = await multipartReader.ReadNextSectionAsync(cancellationToken);
77+
78+
while (section != null)
79+
{
80+
if (ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out var contentDisposition)
81+
&& StreamHelper.HasFileContentDisposition(contentDisposition))
82+
{
83+
var fileName = contentDisposition.FileName.Value;
84+
var contentType = section.ContentType;
85+
86+
if (options == null)
87+
{
88+
options = new UploadOptions
89+
{
90+
FileName = fileName,
91+
MimeType = contentType
92+
};
93+
}
94+
95+
using var memoryStream = new MemoryStream();
96+
await section.Body.CopyToAsync(memoryStream, cancellationToken);
97+
memoryStream.Position = 0;
98+
99+
return await storage.UploadAsync(memoryStream, options, cancellationToken);
100+
}
101+
102+
section = await multipartReader.ReadNextSectionAsync(cancellationToken);
103+
}
104+
105+
return Result<BlobMetadata>.Fail(HttpStatusCode.BadRequest, "No file found in request");
106+
}
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+
}
116+
}

Integraions/ManagedCode.Storage.Server/Extensions/StorageServiceCollectionExtensions.cs renamed to Integraions/ManagedCode.Storage.Server/Extensions/DependencyInjection/StorageServiceCollectionExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ public static IServiceCollection AddStorageSetupService(this IServiceCollection
88
{
99
return services.AddHostedService<StorageSetupBackgroundService>();
1010
}
11-
}
11+
}

Integraions/ManagedCode.Storage.Server/Extensions/BrowserFileExtensions.cs renamed to Integraions/ManagedCode.Storage.Server/Extensions/File/BrowserFileExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ public static async Task<LocalFile> ToLocalFileAsync(this IBrowserFile formFile,
1313
await localFile.CopyFromStreamAsync(formFile.OpenReadStream());
1414
return localFile;
1515
}
16-
}
16+
}

Integraions/ManagedCode.Storage.Server/Extensions/FormFileExtensions.cs renamed to Integraions/ManagedCode.Storage.Server/Extensions/File/FormFileExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ public static async IAsyncEnumerable<LocalFile> ToLocalFilesAsync(this IFormFile
2222
foreach (var formFile in formFileCollection)
2323
yield return await formFile.ToLocalFileAsync(cancellationToken);
2424
}
25-
}
25+
}

Integraions/ManagedCode.Storage.Server/Extensions/StorageBrowserFileExtensions.cs renamed to Integraions/ManagedCode.Storage.Server/Extensions/Storage/StorageBrowserFileExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,4 @@ public static async Task<Result<BlobMetadata>> UploadToStorageAsync(this IStorag
4646
return await storage.UploadAsync(stream, options, cancellationToken);
4747
}
4848
}
49-
}
49+
}

0 commit comments

Comments
 (0)