|
| 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 | +} |
0 commit comments