pages/Program.cs

53 lines
1.5 KiB
C#

using System.IO.Compression;
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.FileProviders;
using pages;
if (!Directory.Exists("pages"))
Directory.CreateDirectory("pages");
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
builder.Services.AddAuthentication().AddScheme<AuthenticationSchemeOptions, FixedTokenAuthenticationHandler>("token", null);
builder.Services.AddAuthorization();
WebApplication app = builder.Build();
app.UseStatusCodePages();
app.UseDefaultFiles();
app.UseStaticFiles(new StaticFileOptions {
FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "pages"))
});
app.UseAuthentication();
app.UseAuthorization();
app.MapPost("/pages/{projectName}", async (string projectName, HttpRequest request) => {
if (!request.HasFormContentType || request.Form.Files["zipfile"] is null)
return Results.BadRequest();
IFormFile zipFile = request.Form.Files["zipfile"]!;
if (zipFile.Length == 0)
return Results.BadRequest();
var extractPath = Path.Combine("pages", projectName);
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();
}).RequireAuthorization();
app.Run();