Setup Git Repository Parsing

This commit is contained in:
Simon Martens
2023-09-10 01:09:20 +02:00
parent 4e3c65dc6f
commit 8fd0050cf3
69 changed files with 1228 additions and 1461 deletions

View File

@@ -20,265 +20,27 @@ using Microsoft.AspNetCore.Http.Features;
using System.Text;
// Controlling all the API-Endpoints
[FeatureGate(Features.AdminService)]
public class APIController : Controller {
// DI
private IHaDocumentWrappper _lib;
private IReaderService _readerService;
private readonly long _fileSizeLimit;
private readonly string _targetFilePath;
private readonly IXMLService _xmlService;
private readonly IXMLProvider _xmlProvider;
private readonly IXMLTestService _testService;
private readonly IXMLFileProvider _xmlProvider;
// Options
private static readonly string[] _permittedExtensions = { ".xml" };
private static readonly FormOptions _defaultFormOptions = new FormOptions();
public APIController(IHaDocumentWrappper lib, IReaderService readerService, IXMLService xmlService, IXMLProvider xmlProvider, IXMLTestService testService, IConfiguration config) {
public APIController(IHaDocumentWrappper lib, IXMLFileProvider xmlProvider) {
_lib = lib;
_xmlProvider = xmlProvider;
_readerService = readerService;
_xmlService = xmlService;
_testService = testService;
_fileSizeLimit = config.GetValue<long>("FileSizeLimit");
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
_targetFilePath = config.GetValue<string>("StoredFilePathWindows");
} else {
_targetFilePath = config.GetValue<string>("StoredFilePathLinux");
}
}
[HttpGet]
[Route("API/Syntaxcheck/{id}")]
[DisableFormValueModelBinding]
[ValidateAntiForgeryToken]
[FeatureGate(Features.UploadService, Features.AdminService)]
public IActionResult SyntaxCheck(string id) {
return Ok();
}
//// UPLOAD ////
[HttpPost]
[Route("API/Upload")]
[DisableFormValueModelBinding]
[ValidateAntiForgeryToken]
[FeatureGate(Features.UploadService, Features.AdminService)]
public async Task<IActionResult> Upload() {
List<XMLRootDocument>? docs = null;
//// 1. Stage: Check Request format and request spec
// Checks the Content-Type Field (must be multipart + Boundary)
if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType)) {
ModelState.AddModelError("Error", $"Wrong / No Content Type on the Request");
return BadRequest(ModelState);
}
// Divides the multipart document into it's sections and sets up a reader
var boundary = MultipartRequestHelper.GetBoundary(MediaTypeHeaderValue.Parse(Request.ContentType), _defaultFormOptions.MultipartBoundaryLengthLimit);
var reader = new MultipartReader(boundary, HttpContext.Request.Body);
MultipartSection? section = null;
try {
section = await reader.ReadNextSectionAsync();
} catch (Exception ex) {
ModelState.AddModelError("Error", "The Request is bad: " + ex.Message);
return BadRequest(ModelState);
}
while (section != null) {
// Multipart document content disposition header read for a section:
// Starts with boundary, contains field name, content-dispo, filename, content-type
var hasContentDispositionHeader = ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out var contentDisposition);
if (contentDisposition != null && contentDisposition.Name == "__RequestVerificationToken") {
try {
section = await reader.ReadNextSectionAsync();
} catch (Exception ex) {
ModelState.AddModelError("Error", "The Request is bad: " + ex.Message);
}
continue;
}
if (hasContentDispositionHeader && contentDisposition != null) {
// Checks if it is a section with content-disposition, name, filename
if (!MultipartRequestHelper.HasFileContentDisposition(contentDisposition)) {
ModelState.AddModelError("Error", $"Wrong Content-Dispostion Headers in Multipart Document");
return BadRequest(ModelState);
}
//// 2. Stage: Check File. Sanity checks on the file on a byte level, extension checking, is it empty etc.
var streamedFileContent = await XMLFileHelpers.ProcessStreamedFile(
section, contentDisposition, ModelState,
_permittedExtensions, _fileSizeLimit);
if (!ModelState.IsValid || streamedFileContent == null)
return BadRequest(ModelState);
//// 3. Stage: Valid XML checking using a simple XDocument.Load()
var xdocument = await XDocumentFileHelper.ProcessStreamedFile(streamedFileContent, ModelState);
if (!ModelState.IsValid || xdocument == null)
return UnprocessableEntity(ModelState);
//// 4. Stage: Is it a Hamann-Document? What kind?
var retdocs = _xmlService.ProbeFile(xdocument, ModelState);
if (!ModelState.IsValid || retdocs == null || !retdocs.Any())
return UnprocessableEntity(ModelState);
//// 5. Stage: Saving the File(s)
foreach (var doc in retdocs) {
// Physical saving
await _xmlProvider.Save(doc, _targetFilePath, ModelState);
// Setting the new docuemnt as used
_xmlService.Use(doc);
// Unsetting all old docuemnts as ununsed
_xmlService.AutoUse(doc.Prefix);
if (!ModelState.IsValid) return StatusCode(500, ModelState);
if (docs == null) docs = new List<XMLRootDocument>();
docs.Add(doc);
}
xdocument = null;
retdocs = null;
streamedFileContent = null;
}
try {
section = await reader.ReadNextSectionAsync();
} catch (Exception ex) {
ModelState.AddModelError("Error", "The Request is bad: " + ex.Message);
return BadRequest(ModelState);
}
}
// 6. Stage: Success! Returning Ok, and redirecting
JsonSerializerOptions options = new() {
ReferenceHandler = ReferenceHandler.Preserve,
Converters = {
new IdentificationStringJSONConverter()
}
};
string json = JsonSerializer.Serialize(docs);
_testService.Test();
return Created(nameof(UploadController), json);
}
//// PUBLISH ////
[HttpPost]
[Route("API/LocalPublish")]
[DisableFormValueModelBinding]
[ValidateAntiForgeryToken]
[FeatureGate(Features.LocalPublishService, Features.AdminService, Features.UploadService)]
public async Task<IActionResult> LocalPublish() {
var element = _xmlService.MergeUsedDocuments(ModelState);
if (!ModelState.IsValid || element == null)
return BadRequest(ModelState);
var savedfile = await _xmlProvider.SaveHamannFile(element, _targetFilePath, ModelState);
if (!ModelState.IsValid || savedfile == null) {
if (savedfile != null)
_xmlProvider.DeleteHamannFile(savedfile.Name);
return BadRequest(ModelState);
}
_ = _lib.SetLibrary(savedfile.PhysicalPath, ModelState);
if (!ModelState.IsValid) {
_xmlProvider.DeleteHamannFile(savedfile.Name);
return BadRequest(ModelState);
}
_xmlProvider.SetInProduction(savedfile);
_xmlService.SetInProduction();
return Created("/", _xmlProvider.GetHamannFiles());
}
[HttpPost]
[Route("API/SetUsed/{id}")]
[DisableFormValueModelBinding]
[ValidateAntiForgeryToken]
[FeatureGate(Features.UploadService, Features.AdminService)]
public async Task<IActionResult> SetUsed(string id) {
var f = _xmlProvider.GetFiles(id);
if (f == null) {
ModelState.AddModelError("Error", "Wrong Endpoint");
return BadRequest(ModelState);
}
var files = f.GetFileList();
if (files == null) {
ModelState.AddModelError("Error", "Wrong Endpoint");
return BadRequest(ModelState);
}
List<XMLRootDocument>? newUsed = null;
if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType)) {
ModelState.AddModelError("Error", $"Wrong / No Content Type on the Request");
return BadRequest(ModelState);
}
// Same as above, check Upload()
var boundary = MultipartRequestHelper.GetBoundary(MediaTypeHeaderValue.Parse(Request.ContentType), _defaultFormOptions.MultipartBoundaryLengthLimit);
var reader = new MultipartReader(boundary, HttpContext.Request.Body);
MultipartSection? section = null;
try {
section = await reader.ReadNextSectionAsync();
} catch (Exception ex) {
ModelState.AddModelError("Error", "The Request is bad: " + ex.Message);
return BadRequest(ModelState);
}
while (section != null) {
var hasContentDispositionHeader = ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out var contentDisposition);
if (contentDisposition != null && contentDisposition.Name == "__RequestVerificationToken") {
try {
section = await reader.ReadNextSectionAsync();
} catch (Exception ex) {
ModelState.AddModelError("Error", "The Request is bad: " + ex.Message);
}
continue;
}
var filename = string.Empty;
if (hasContentDispositionHeader && contentDisposition != null) {
if (!MultipartRequestHelper.HasFormDataContentDisposition(contentDisposition)) {
ModelState.AddModelError("Error", $"Wrong Content-Dispostion Headers in Multipart Document");
return BadRequest(ModelState);
}
filename = XMLFileHelpers.StreamToString(section.Body, ModelState);
if (!ModelState.IsValid) return BadRequest(ModelState);
var isFile = files.Where(x => x.FileName == filename);
if (isFile == null || !isFile.Any()) {
ModelState.AddModelError("Error", "Tried to add a file that does not exist.");
return BadRequest(ModelState);
}
if (newUsed == null) newUsed = new List<XMLRootDocument>();
newUsed.Add(isFile.First());
}
try {
section = await reader.ReadNextSectionAsync();
} catch (Exception ex) {
ModelState.AddModelError("Error", "The Request is bad: " + ex.Message);
return BadRequest(ModelState);
}
}
_xmlService.UnUse(id);
if (newUsed != null && newUsed.Any()) {
newUsed.ForEach(x => _xmlService.Use(x));
}
_testService.Test();
return Created("/", newUsed);
}
[HttpPost]
[Route("API/SetInProduction")]
[DisableFormValueModelBinding]
[ValidateAntiForgeryToken]
[FeatureGate(Features.UploadService, Features.AdminService)]
[FeatureGate(Features.LocalPublishService, Features.AdminService)]
public async Task<IActionResult> SetInProduction() {
var hF = _xmlProvider.GetHamannFiles();
if (hF == null) {
@@ -334,31 +96,18 @@ public class APIController : Controller {
}
if (filename == null) {
ModelState.AddModelError("Error", "No filename given");
ModelState.AddModelError("Error", "Kein Dateiname.");
return BadRequest(ModelState);
}
var newFile = hF.Where(x => x.Name == filename);
if (newFile == null || !newFile.Any()) {
ModelState.AddModelError("Error", "Trying to set a unavailable file.");
ModelState.AddModelError("Error", "Versuch, auf eine unverfügbare Datei zuzugreifen.");
return BadRequest(ModelState);
}
_ = _lib.SetLibrary(newFile.First().PhysicalPath, ModelState);
_ = _lib.SetLibrary(newFile.First(), null, ModelState);
if (!ModelState.IsValid) return BadRequest(ModelState);
_xmlProvider.SetInProduction(newFile.First());
return Created("/", newFile.First());
}
[HttpPost]
[Route("API/SetYearSetting")]
[ValidateAntiForgeryToken]
[FeatureGate(Features.UploadService, Features.AdminService)]
public async Task<IActionResult>? SetEndYear(YearSetting startendyear) {
_lib.SetEndYear(startendyear.EndYear);
return Created("/", "");;
}
}

View File

@@ -18,6 +18,6 @@ public class AdminController : Controller {
[Route("Admin")]
[FeatureGate(Features.AdminService)]
public IActionResult Index() {
return Redirect("/Admin/Upload");
return Redirect("/Admin/XMLState");
}
}

View File

@@ -143,7 +143,7 @@ public class Briefecontroller : Controller {
}
private static List<(string Sender, string Receiver)> generateSendersRecievers(List<Person>? senders, List<Person>? receivers, bool generatePersonLinks) {
private static List<(string Sender, string Receiver)>? generateSendersRecievers(List<Person>? senders, List<Person>? receivers, bool generatePersonLinks) {
var res = new List<(string Sender, string Receiver)>();
if (senders == null || receivers == null) return null;
if (!generatePersonLinks) {

View File

@@ -13,11 +13,11 @@ namespace HaWeb.Controllers;
public class IndexController : Controller {
private IHaDocumentWrappper _lib;
private IReaderService _readerService;
private IXMLService _xmlService;
private IXMLInteractionService _xmlService;
private int _lettersForPage;
private int _endYear;
public IndexController(IHaDocumentWrappper lib, IReaderService readerService, IXMLService service, IConfiguration config) {
public IndexController(IXMLFileProvider _, IHaDocumentWrappper lib, IReaderService readerService, IXMLInteractionService service, IConfiguration config) {
_lib = lib;
_readerService = readerService;
_xmlService = service;

View File

@@ -18,10 +18,10 @@ namespace HaWeb.Controllers;
public class SucheController : Controller {
private IHaDocumentWrappper _lib;
private IReaderService _readerService;
private IXMLService _xmlService;
private IXMLInteractionService _xmlService;
private int _lettersForPage;
public SucheController(IHaDocumentWrappper lib, IReaderService readerService, IXMLService service, IConfiguration config) {
public SucheController(IHaDocumentWrappper lib, IReaderService readerService, IXMLInteractionService service, IConfiguration config) {
_lib = lib;
_readerService = readerService;
_xmlService = service;
@@ -206,7 +206,7 @@ public class SucheController : Controller {
string activeSearch,
SearchType ST,
SearchResultType SRT,
List<CommentModel> comments) {
List<CommentModel>? comments) {
// Model init & return
var model = new SucheViewModel(ST, SRT, null, 0, null, activeSearch, null, null, comments, null);
return View("~/Views/HKB/Dynamic/Suche.cshtml", model);

View File

@@ -1,107 +0,0 @@
namespace HaWeb.Controllers;
using Microsoft.AspNetCore.Mvc;
using HaDocument.Interfaces;
using HaXMLReader.Interfaces;
using Microsoft.FeatureManagement.Mvc;
using System.Runtime.InteropServices;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.Extensions.Configuration;
using HaWeb.Filters;
using HaWeb.XMLParser;
using HaWeb.Models;
using HaWeb.FileHelpers;
using Microsoft.AspNetCore.Mvc.Rendering;
public class UploadController : Controller {
// DI
private IHaDocumentWrappper _lib;
private IReaderService _readerService;
private readonly long _fileSizeLimit;
private readonly string _targetFilePath;
private readonly IXMLService _xmlService;
private readonly IXMLProvider _xmlProvider;
// Options
private static readonly string[] _permittedExtensions = { ".xml" };
private static readonly FormOptions _defaultFormOptions = new FormOptions();
public UploadController(IHaDocumentWrappper lib, IReaderService readerService, IXMLService xmlService, IXMLProvider xmlProvider, IConfiguration config) {
_lib = lib;
_readerService = readerService;
_xmlService = xmlService;
_xmlProvider = xmlProvider;
_fileSizeLimit = config.GetValue<long>("FileSizeLimit");
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
_targetFilePath = config.GetValue<string>("StoredFilePathWindows");
} else {
_targetFilePath = config.GetValue<string>("StoredFilePathLinux");
}
}
[HttpGet]
[Route("Admin/Upload/{id?}")]
[FeatureGate(Features.AdminService)]
[GenerateAntiforgeryTokenCookie]
public IActionResult Index(string? id) {
var library = _lib.GetLibrary();
var roots = _xmlService.GetRootsList();
if (roots == null) return error404();
var hF = _xmlProvider.GetHamannFiles();
List<FileModel>? hamannFiles = null;
if (hF != null)
hamannFiles = hF
.OrderByDescending(x => x.LastModified)
.Select(x => new FileModel(x.Name, string.Empty, x.LastModified.LocalDateTime, false, x == _xmlProvider.GetInProduction()))
.ToList();
var uF = _xmlService.GetUsedDictionary();
var pF = _xmlService.GetInProduction();
Dictionary<string, List<FileModel>?>? usedFiles = null;
if (uF != null) {
usedFiles = new Dictionary<string, List<FileModel>?>();
foreach (var kv in uF) {
if (kv.Value == null) continue;
usedFiles.Add(kv.Key, XMLFileHelpers.ToFileModel(kv.Value, pF, uF));
}
}
Dictionary<string, List<FileModel>?>? productionFiles = null;
if (pF != null) {
productionFiles = new Dictionary<string, List<FileModel>?>();
foreach (var kv in pF) {
if (kv.Value == null) continue;
productionFiles.Add(kv.Key, XMLFileHelpers.ToFileModel(kv.Value, pF, uF));
}
}
var availableYears = library.MetasByYear.Select(x => x.Key).Union(library.ExcludedMetasByYear.Select(x => x.Key)).ToList();
availableYears.Sort();
if (id != null) {
id = id.ToLower();
var root = _xmlService.GetRoot(id);
if (root == null) return error404();
var model = new UploadViewModel(root.Type, id, roots, usedFiles, _lib.GetStartYear(), _lib.GetEndYear(), availableYears);
model.ProductionFiles = productionFiles;
model.HamannFiles = hamannFiles;
model.AvailableFiles = XMLFileHelpers.ToFileModel(_xmlProvider.GetFiles(id), pF, uF);
return View("~/Views/Admin/Dynamic/Upload.cshtml", model);
} else {
var model = new UploadViewModel("Upload & Veröffentlichen", id, roots, usedFiles, _lib.GetStartYear(), _lib.GetEndYear(), availableYears);
model.ProductionFiles = productionFiles;
model.HamannFiles = hamannFiles;
return View("~/Views/Admin/Dynamic/Upload.cshtml", model);
}
}
private IActionResult error404() {
Response.StatusCode = 404;
return Redirect("/Error404");
}
}

View File

@@ -0,0 +1,50 @@
namespace HaWeb.Controllers;
using Microsoft.AspNetCore.Mvc;
using Microsoft.FeatureManagement.Mvc;
using HaWeb.Filters;
using HaWeb.XMLParser;
using HaWeb.Models;
using HaWeb.FileHelpers;
using HaWeb.BackgroundTask;
[FeatureGate(Features.AdminService)]
public class XMLStateController : Controller {
// DI
private IHaDocumentWrappper _lib;
private readonly IXMLInteractionService _xmlService;
private readonly IXMLFileProvider _xmlProvider;
private readonly IMonitorLoop _loop;
public XMLStateController(IMonitorLoop loop, IHaDocumentWrappper lib, IXMLInteractionService xmlService, IXMLFileProvider xmlProvider) {
_lib = lib;
_xmlService = xmlService;
_xmlProvider = xmlProvider;
_loop = loop;
}
[HttpGet]
[Route("Admin/XMLState/")]
[FeatureGate(Features.AdminService)]
[GenerateAntiforgeryTokenCookie]
public IActionResult Index() {
_loop.StartMonitorLoop();
var library = _lib.GetLibrary();
var roots = _xmlService.GetRootsList();
if (roots == null) return error404();
var hF = _xmlProvider.GetHamannFiles()?.OrderByDescending(x => x.LastModified).ToList();
var mF = _xmlService.GetManagedFiles();
var gD = _xmlProvider.GetGitData();
var activeF = _lib.GetActiveFile();
var vS = _xmlService.GetValidState();
var model = new XMLStateViewModel("Dateiübersicht", gD, roots, hF, mF, vS) {
ActiveFile = activeF,
};
return View("~/Views/Admin/Dynamic/XMLState.cshtml", model);
}
private IActionResult error404() {
Response.StatusCode = 404;
return Redirect("/Error404");
}
}