mirror of
https://github.com/Theodor-Springmann-Stiftung/hamann-ausgabe-core.git
synced 2025-10-30 09:45:32 +00:00
Deployment v1
This commit is contained in:
@@ -19,88 +19,50 @@ using System.Runtime.InteropServices;
|
||||
using Microsoft.AspNetCore.Http.Features;
|
||||
using System.Text;
|
||||
|
||||
public class FileListForm {
|
||||
public string file { get; set; }
|
||||
public string __RequestVerificationToken { get; set; }
|
||||
}
|
||||
|
||||
// Controlling all the API-Endpoints
|
||||
[FeatureGate(Features.AdminService)]
|
||||
[ApiController]
|
||||
public class APIController : Controller {
|
||||
|
||||
// DI
|
||||
private IHaDocumentWrappper _lib;
|
||||
private readonly IHaDocumentWrappper _lib;
|
||||
private readonly IXMLFileProvider _xmlProvider;
|
||||
|
||||
private readonly IXMLInteractionService _xmlService;
|
||||
|
||||
// Options
|
||||
private static readonly FormOptions _defaultFormOptions = new FormOptions();
|
||||
|
||||
|
||||
public APIController(IHaDocumentWrappper lib, IXMLFileProvider xmlProvider) {
|
||||
public APIController(IHaDocumentWrappper lib, IXMLInteractionService xmlService, IXMLFileProvider xmlProvider) {
|
||||
_lib = lib;
|
||||
_xmlProvider = xmlProvider;
|
||||
_xmlService = xmlService;
|
||||
}
|
||||
|
||||
|
||||
// TODO: this is trash
|
||||
[HttpPost]
|
||||
[Route("API/SetInProduction")]
|
||||
[DisableFormValueModelBinding]
|
||||
[ValidateAntiForgeryToken]
|
||||
[FeatureGate(Features.LocalPublishService, Features.AdminService)]
|
||||
public async Task<IActionResult> SetInProduction() {
|
||||
public async Task<IActionResult> SetInProduction([FromForm] FileListForm _form) {
|
||||
var hF = _xmlProvider.GetHamannFiles();
|
||||
if (hF == null) {
|
||||
ModelState.AddModelError("Error", "There are no Hamman.xml files available.");
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType)) {
|
||||
ModelState.AddModelError("Error", $"Wrong / No Content Type on the Request");
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
// Same as above, check Upload()
|
||||
string? filename = null;
|
||||
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;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
try {
|
||||
section = await reader.ReadNextSectionAsync();
|
||||
} catch (Exception ex) {
|
||||
ModelState.AddModelError("Error", "The Request is bad: " + ex.Message);
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
}
|
||||
|
||||
if (filename == null) {
|
||||
if (_form == null || String.IsNullOrWhiteSpace(_form.file)) {
|
||||
ModelState.AddModelError("Error", "Kein Dateiname.");
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
var newFile = hF.Where(x => x.Name == filename);
|
||||
var newFile = hF.Where(x => x.Name == _form.file);
|
||||
if (newFile == null || !newFile.Any()) {
|
||||
ModelState.AddModelError("Error", "Versuch, auf eine unverfügbare Datei zuzugreifen.");
|
||||
return BadRequest(ModelState);
|
||||
@@ -110,4 +72,19 @@ public class APIController : Controller {
|
||||
if (!ModelState.IsValid) return BadRequest(ModelState);
|
||||
return Created("/", newFile.First());
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("API/SyntaxCheck")]
|
||||
// [ValidateAntiForgeryToken]
|
||||
[DisableFormValueModelBinding]
|
||||
[FeatureGate(Features.SyntaxCheck, Features.AdminService)]
|
||||
public ActionResult<Dictionary<string, SyntaxCheckModel>?> GetSyntaxCheck(string? id) {
|
||||
var SCCache = _xmlService.GetSCCache();
|
||||
if (_xmlProvider.HasChanged() || SCCache == null) {
|
||||
var commit = _xmlProvider.GetGitState();
|
||||
SCCache = _xmlService.Test(_xmlService.GetState(), commit != null ? commit.Commit : string.Empty);
|
||||
_xmlService.SetSCCache(SCCache);
|
||||
}
|
||||
return Ok(SCCache);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user