diff --git a/fundamentals/minimal-apis/samples/IFormFile/IFormFile.csproj b/fundamentals/minimal-apis/samples/IFormFile/IformFile.csproj similarity index 100% rename from fundamentals/minimal-apis/samples/IFormFile/IFormFile.csproj rename to fundamentals/minimal-apis/samples/IFormFile/IformFile.csproj diff --git a/fundamentals/minimal-apis/samples/IFormFile/Program.cs b/fundamentals/minimal-apis/samples/IFormFile/Program.cs index 3d856dd..9d00025 100644 --- a/fundamentals/minimal-apis/samples/IFormFile/Program.cs +++ b/fundamentals/minimal-apis/samples/IFormFile/Program.cs @@ -1,3 +1,4 @@ +// using Microsoft.AspNetCore.Antiforgery; using Microsoft.AspNetCore.Http.HttpResults; using Microsoft.AspNetCore.Mvc; @@ -8,28 +9,68 @@ builder.Services.AddAntiforgery(); var app = builder.Build(); app.UseAntiforgery(); -string GetOrCreateFilePath(string fileName, string filesDirectory = "uploadFiles") -{ - var directoryPath = Path.Combine(app.Environment.ContentRootPath, filesDirectory); - Directory.CreateDirectory(directoryPath); - return Path.Combine(directoryPath, fileName); -} -async Task UploadFileWithName(IFormFile file, string fileSaveName) -{ - var filePath = GetOrCreateFilePath(fileSaveName); - await using var fileStream = new FileStream(filePath, FileMode.Create); - await file.CopyToAsync(fileStream); -} +var MyHtml = new MyUtils(); +// Generate a form with an anti-forgery token and an /upload endpoint. app.MapGet("/", (HttpContext context, IAntiforgery antiforgery) => { var token = antiforgery.GetAndStoreTokens(context); - var html = $""" + var html = MyHtml.GenerateHtmlForm(token.FormFieldName, token.RequestToken!); + return Results.Content(html, "text/html"); +}); + +app.MapPost("/upload", async Task,BadRequest>> + ([FromForm] FileUpload file, HttpContext context, IAntiforgery antiforgery) => +{ + try + { + await antiforgery.ValidateRequestAsync(context); + await MyHtml.UploadFileWithName(file.FileDocument!, file.Name!, + app.Environment.ContentRootPath); + return TypedResults.Ok($"Your file with the description:" + + $" {file.Description} has been uploaded successfully"); + } + catch (AntiforgeryValidationException e) + { + return TypedResults.BadRequest("Invalid anti-forgery token" + e.HResult); + } +}); + +app.Run(); +// + +public class FileUpload +{ + public string? Name { get; set; } + public string? Description { get; set; } + public IFormFile? FileDocument { get; set; } +} + +public class MyUtils +{ + string GetOrCreateFilePath(string fileName, string contentRootPath, + string filesDirectory = "uploadFiles") + { + var directoryPath = Path.Combine(contentRootPath, filesDirectory); + Directory.CreateDirectory(directoryPath); + return Path.Combine(directoryPath, fileName); + } + + public async Task UploadFileWithName(IFormFile file, string fileSaveName, string contentRootPath) + { + var filePath = GetOrCreateFilePath(fileSaveName, contentRootPath); + await using var fileStream = new FileStream(filePath, FileMode.Create); + await file.CopyToAsync(fileStream); + } + + public string GenerateHtmlForm(string formFieldName, string requestToken) + { + return $"""
- +

@@ -42,30 +83,5 @@ app.MapGet("/", (HttpContext context, IAntiforgery antiforgery) => """; - - return Results.Content(html, "text/html"); -}); - -app.MapPost("/upload", async Task, - BadRequest>> ([FromForm] FileUpload file, HttpContext context, IAntiforgery antiforgery) => -{ - try - { - await antiforgery.ValidateRequestAsync(context); - await UploadFileWithName(file.FileDocument, file.Name); - return TypedResults.Ok($"Your file with the description: {file.Description} has been uploaded successfully"); } - catch (AntiforgeryValidationException e) - { - return TypedResults.BadRequest("Invalid anti-forgery token"); - } -}); - -app.Run(); - -public class FileUpload -{ - public string? Name { get; set; } - public string? Description { get; set; } - public IFormFile? FileDocument { get; set; } } diff --git a/fundamentals/minimal-apis/samples/IFormFile/appsettings.json b/fundamentals/minimal-apis/samples/IFormFile/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/fundamentals/minimal-apis/samples/IFormFile/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +}