pages/Program.cs

54 lines
1.5 KiB
C#
Raw Permalink Normal View History

2024-10-06 19:50:15 +00:00
using System.IO.Compression;
2024-12-05 12:28:04 +00:00
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.FileProviders;
using pages;
2024-10-06 19:50:15 +00:00
2024-12-05 12:28:04 +00:00
if (!Directory.Exists("pages"))
Directory.CreateDirectory("pages");
2024-10-06 19:50:15 +00:00
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
2024-12-05 12:28:04 +00:00
builder.Services.AddAuthentication().AddScheme<AuthenticationSchemeOptions, FixedTokenAuthenticationHandler>("token", null);
builder.Services.AddAuthorization();
WebApplication app = builder.Build();
2024-10-06 19:50:15 +00:00
app.UseStatusCodePages();
2024-12-05 12:28:04 +00:00
2024-10-07 07:50:27 +00:00
app.UseDefaultFiles();
2024-10-06 19:50:15 +00:00
app.UseStaticFiles(new StaticFileOptions {
2024-12-05 12:28:04 +00:00
FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "pages"))
2024-10-06 19:50:15 +00:00
});
2024-12-05 12:28:04 +00:00
app.UseAuthentication();
app.UseAuthorization();
2024-10-06 19:50:15 +00:00
app.MapPost("/pages/{projectName}", async (string projectName, HttpRequest request) => {
2024-10-07 06:59:46 +00:00
if (!request.HasFormContentType || request.Form.Files["zipfile"] is null)
2024-10-06 19:50:15 +00:00
return Results.BadRequest();
2024-10-07 06:59:46 +00:00
IFormFile zipFile = request.Form.Files["zipfile"]!;
2024-10-06 19:50:15 +00:00
if (zipFile.Length == 0)
return Results.BadRequest();
2024-12-05 12:28:04 +00:00
var extractPath = Path.Combine("pages", projectName);
2024-10-06 19:50:15 +00:00
if (Directory.Exists(extractPath))
Directory.Delete(extractPath, true);
Directory.CreateDirectory(extractPath);
try {
await using Stream stream = zipFile.OpenReadStream();
using var zipArchive = new ZipArchive(stream);
zipArchive.ExtractToDirectory(extractPath, true);
} catch {
return Results.BadRequest();
}
return Results.Ok();
2024-12-05 12:28:04 +00:00
}).RequireAuthorization();
2024-10-06 19:50:15 +00:00
app.Run();