initial commit
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
global using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using ParadoxSaveParser.Lib;
|
||||
|
||||
namespace ParadoxSaveParser.WebAPI;
|
||||
|
||||
public enum SaveFileProcessingStatus
|
||||
{
|
||||
NotFound, Uploading, Parsing, SavingResults, Done, Error
|
||||
}
|
||||
|
||||
public class SaveFileMetadata
|
||||
{
|
||||
public required string guid;
|
||||
public required SaveFileProcessingStatus status { get; set; }
|
||||
}
|
||||
|
||||
public class Program
|
||||
{
|
||||
private const string DATA_DIR = "data";
|
||||
private static string SAVES_DIR = Path.Join(DATA_DIR, "saves");
|
||||
private static ConcurrentDictionary<string, SaveFileMetadata> _saveMetadataStorage = new();
|
||||
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
var app = builder.Build();
|
||||
|
||||
foreach (var metaFilePath in Directory.GetFiles(SAVES_DIR, "*.meta.json", SearchOption.TopDirectoryOnly))
|
||||
{
|
||||
using var metaFile = File.Open(metaFilePath, FileMode.Open, FileAccess.Read);
|
||||
var meta = JsonSerializer.Deserialize<SaveFileMetadata>(metaFile) ?? throw new NullReferenceException(metaFilePath);
|
||||
if (meta.status != SaveFileProcessingStatus.Done)
|
||||
{
|
||||
app.Logger.Log(LogLevel.Warning, $"metadata file '{metaFilePath}' status has invalid status {meta.status}"));
|
||||
}
|
||||
|
||||
if(!_saveMetadataStorage.TryAdd(meta.guid, meta))
|
||||
throw new Exception("Guid collision!");
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
app.MapPost("/parse/eu4", async httpContext =>
|
||||
{
|
||||
var remoteFile = httpContext.Request.Form.Files.FirstOrDefault();
|
||||
if(remoteFile is null)
|
||||
return;
|
||||
string save_id = Guid.NewGuid().ToString();
|
||||
string meta_file_path = Path.Join(SAVES_DIR, save_id + ".meta.json");
|
||||
// string save_file_path = Path.Join(SAVES_DIR, save_id + ".eu4");
|
||||
// await using var metaFile = File.Open(meta_file_path, FileMode.CreateNew, FileAccess.Write);
|
||||
if (File.Exists(meta_file_path))
|
||||
{
|
||||
httpContext.Response.StatusCode = StatusCodes.Status500InternalServerError;
|
||||
throw new Exception($"Guid collision! file {meta_file_path} already exists.")
|
||||
}
|
||||
|
||||
await using var saveFile =
|
||||
await remoteFile.OpenReadStream().CopyToAsync();
|
||||
var parser = new ParserEU4(stream);
|
||||
|
||||
|
||||
});
|
||||
|
||||
app.Run();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user