-
Notifications
You must be signed in to change notification settings - Fork 1
/
FileWebApiEndpoint.cs
118 lines (89 loc) · 4.07 KB
/
FileWebApiEndpoint.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using System.Net.Mime;
using System.Text.Json.Serialization;
namespace Futurum.WebApiEndpoint.Micro.Sample.Features;
[WebApiEndpoint(prefixRoute: "file", group: "feature")]
public partial class FileWebApiEndpoint
{
protected override void Build(IEndpointRouteBuilder builder)
{
builder.MapPost("upload", UploadHandler)
.DisableAntiforgery();
builder.MapPost("uploads", UploadsHandler)
.DisableAntiforgery();
builder.MapPost("upload-with-payload", UploadWithPayloadHandler);
builder.MapPost("uploads-with-payload", UploadsWithPayloadHandler);
builder.MapGet("download", DownloadHandler);
}
private static Task<Results<Ok<FileDetailsDto>, BadRequest<ProblemDetails>>> UploadHandler(HttpContext context, IFormFile file)
{
return RunToOkAsync(Execute, context, "Failed to read file");
async Task<FileDetailsDto> Execute()
{
var tempFile = Path.GetTempFileName();
await using var stream = File.OpenWrite(tempFile);
await file.CopyToAsync(stream);
return new FileDetailsDto(file.FileName);
}
}
private static Task<Results<Ok<IEnumerable<FileDetailsDto>>, BadRequest<ProblemDetails>>> UploadsHandler(HttpContext context, IFormFileCollection files)
{
return RunToOkAsync(Execute, context, "Failed to read file");
async Task<IEnumerable<FileDetailsDto>> Execute()
{
var fileDetails = new List<FileDetailsDto>();
foreach (var file in files)
{
var tempFile = Path.GetTempFileName();
await using var stream = File.OpenWrite(tempFile);
await file.CopyToAsync(stream);
fileDetails.Add(new FileDetailsDto(file.FileName));
}
return fileDetails;
}
}
private static Task<Results<Ok<FileDetailsWithPayloadDto>, BadRequest<ProblemDetails>>> UploadWithPayloadHandler(HttpContext context, FormFileWithPayload<PayloadDto> fileWithPayload)
{
return RunToOkAsync(Execute, context, "Failed to read file");
async Task<FileDetailsWithPayloadDto> Execute()
{
var tempFile = Path.GetTempFileName();
await using var stream = File.OpenWrite(tempFile);
await fileWithPayload.File.CopyToAsync(stream);
return new FileDetailsWithPayloadDto(fileWithPayload.File.FileName, fileWithPayload.Payload.Name);
}
}
private static Task<Results<Ok<IEnumerable<FileDetailsWithPayloadDto>>, BadRequest<ProblemDetails>>> UploadsWithPayloadHandler(
HttpContext context, FormFilesWithPayload<PayloadDto> filesWithPayload)
{
return RunToOkAsync(Execute, context, "Failed to read file");
async Task<IEnumerable<FileDetailsWithPayloadDto>> Execute()
{
var fileDetails = new List<FileDetailsWithPayloadDto>();
foreach (var file in filesWithPayload.Files)
{
var tempFile = Path.GetTempFileName();
await using var stream = File.OpenWrite(tempFile);
await file.CopyToAsync(stream);
fileDetails.Add(new FileDetailsWithPayloadDto(file.FileName, filesWithPayload.Payload.Name));
}
return fileDetails;
}
}
private static Results<NotFound, FileStreamHttpResult, BadRequest<ProblemDetails>> DownloadHandler(HttpContext context)
{
return Run(Execute, context, "Failed to read file");
Results<NotFound, FileStreamHttpResult> Execute()
{
var path = "./Data/hello-world.txt";
if (!File.Exists(path))
{
return TypedResults.NotFound();
}
var fileStream = File.OpenRead(path);
return TypedResults.File(fileStream, MediaTypeNames.Application.Octet, "hello-world.txt");
}
}
}
public record FileDetailsDto(string Name);
public record PayloadDto([property: JsonPropertyName("name")] string Name);
public record FileDetailsWithPayloadDto(string Name, string Payload);