Added Upload functionality; still a bit janky in selecting the files to use...

This commit is contained in:
schnulller
2022-06-05 21:04:22 +02:00
parent 0d33dcd4e5
commit b5aae5ddf0
27 changed files with 188456 additions and 40 deletions

View File

@@ -50,7 +50,7 @@ public class APIController : Controller {
[Route("API/Syntaxcheck/{id}")]
[DisableFormValueModelBinding]
[ValidateAntiForgeryToken]
[FeatureGate(Features.AdminService)]
[FeatureGate(Features.UploadService, Features.AdminService)]
public async Task<IActionResult> SyntaxCheck(string id) {
return Ok();
}
@@ -60,8 +60,8 @@ public class APIController : Controller {
[Route("API/Upload")]
[DisableFormValueModelBinding]
[ValidateAntiForgeryToken]
[FeatureGate(Features.UploadService)]
public async Task<IActionResult> Upload(string? id) {
[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)
@@ -114,19 +114,18 @@ public class APIController : Controller {
return UnprocessableEntity(ModelState);
//// 4. Stage: Is it a Hamann-Document? What kind?
var retdocs = await _xmlService.ProbeHamannFile(xdocument, ModelState);
var retdocs = _xmlService.ProbeHamannFile(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
var task = _xmlProvider.Save(doc, _targetFilePath, ModelState);
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);
await task;
if (!ModelState.IsValid) return StatusCode(500, ModelState);
if (docs == null) docs = new List<XMLRootDocument>();
docs.Add(doc);
@@ -137,6 +136,7 @@ public class APIController : Controller {
section = await reader.ReadNextSectionAsync();
} catch (Exception ex) {
ModelState.AddModelError("Error", "The Request is bad: " + ex.Message);
return BadRequest(ModelState);
}
}
@@ -151,4 +151,24 @@ public class APIController : Controller {
string json = JsonSerializer.Serialize(docs);
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)
return BadRequest(ModelState);
_ = _lib.SetLibrary(savedfile.PhysicalPath, ModelState);
if (!ModelState.IsValid)
return BadRequest(ModelState);
return Ok();
}
}

View File

@@ -31,8 +31,8 @@ public class Briefecontroller : Controller {
// Normalisation and Validation, (some) data aquisition
if (id == null) return Redirect(url + defaultID);
this.id = id.ToLower();
var preliminarymeta = lib.Metas.Where(x => x.Value.Autopsic == this.id);
id = id.ToLower();
var preliminarymeta = lib.Metas.Where(x => x.Value.Autopsic == id);
if (preliminarymeta == null || !preliminarymeta.Any()) return error404();
// Get all neccessary data
@@ -54,7 +54,7 @@ public class Briefecontroller : Controller {
// Model creation
var hasMarginals = false;
if (marginals != null && marginals.Any()) hasMarginals = true;
var model = new BriefeViewModel(this.id, index, generateMetaViewModel(lib, meta, hasMarginals));
var model = new BriefeViewModel(id, index, generateMetaViewModel(lib, meta, hasMarginals));
if (nextmeta != null) model.MetaData.Next = (generateMetaViewModel(lib, nextmeta, false), url + nextmeta.Autopsic);
if (prevmeta != null) model.MetaData.Prev = (generateMetaViewModel(lib, prevmeta, false), url + prevmeta.Autopsic);
if (hands != null && hands.Any()) model.ParsedHands = HaWeb.HTMLHelpers.LetterHelpers.CreateHands(lib, hands);

View File

@@ -1,5 +1,6 @@
namespace HaWeb.FileHelpers;
using HaDocument.Interfaces;
using Microsoft.AspNetCore.Mvc.ModelBinding;
public class HaDocumentWrapper : IHaDocumentWrappper {
public ILibrary Library;
@@ -13,6 +14,18 @@ public class HaDocumentWrapper : IHaDocumentWrappper {
return Library;
}
public ILibrary? SetLibrary(string filepath, ModelStateDictionary ModelState) {
try
{
Library = HaDocument.Document.Create(new HaWeb.Settings.HaDocumentOptions() { HamannXMLFilePath = filepath });
}
catch (Exception ex) {
ModelState.AddModelError("Error:", "Das Dokument konnte nicht geparst werden: " + ex.Message);
return null;
}
return Library;
}
public ILibrary GetLibrary() {
return Library;
}

View File

@@ -1,7 +1,9 @@
namespace HaWeb.FileHelpers;
using HaDocument.Interfaces;
using Microsoft.AspNetCore.Mvc.ModelBinding;
public interface IHaDocumentWrappper {
public ILibrary SetLibrary();
public ILibrary? SetLibrary(string filepath, ModelStateDictionary ModelState);
public ILibrary GetLibrary();
}

View File

@@ -1,8 +1,12 @@
namespace HaWeb.FileHelpers;
using Microsoft.Extensions.FileProviders;
using System.Xml.Linq;
using HaWeb.Models;
using Microsoft.AspNetCore.Mvc.ModelBinding;
public interface IXMLProvider {
public FileList? GetFiles(string prefix);
public Task Save(XMLRootDocument doc, string basefilepath, ModelStateDictionary ModelState);
public Task<IFileInfo?> SaveHamannFile(XElement element, string basefilepath, ModelStateDictionary ModelState);
}

View File

@@ -3,11 +3,13 @@ using Microsoft.Extensions.FileProviders;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using HaWeb.Models;
using HaWeb.XMLParser;
using System.Xml.Linq;
public class XMLProvider : IXMLProvider {
private IFileProvider _fileProvider;
private Dictionary<string, FileList?>? _Files;
private Dictionary<string, IXMLRoot>? _Roots;
private List<IFileInfo>? _HamannFiles;
public XMLProvider(IFileProvider provider, IXMLService xmlservice) {
_fileProvider = provider;
@@ -53,6 +55,35 @@ public class XMLProvider : IXMLProvider {
_Files[doc.Prefix]!.Add(doc);
}
public async Task<IFileInfo?> SaveHamannFile(XElement element, string basefilepath, ModelStateDictionary ModelState) {
var date = DateTime.Now;
var filename = "hamann_" + date.Year + "-" + date.Month + "-" + date.Day + ".xml";
var directory = Path.Combine(basefilepath, "hamann");
var path = Path.Combine(directory, filename);
try {
if (!Directory.Exists(directory))
Directory.CreateDirectory(directory);
using (var targetStream = System.IO.File.Create(path))
await element.SaveAsync(targetStream, SaveOptions.DisableFormatting, new CancellationToken());
}
catch (Exception ex) {
ModelState.AddModelError("Error", "Die Datei konnte nicht gespeichert werden: " + ex.Message);
return null;
}
var info = _fileProvider.GetFileInfo(Path.Combine("hamann", filename));
if (info == null) {
ModelState.AddModelError("Error", "Auf die neu erstellte Dtaei konnte nicht zugegriffen werden.");
return null;
}
if (_HamannFiles == null) _HamannFiles = new List<IFileInfo>();
_HamannFiles.Add(info);
return info;
}
private Dictionary<string, FileList?>? _ScanFiles() {
if (_Roots == null) return null;
Dictionary<string, FileList?>? res = null;

View File

@@ -53,7 +53,7 @@ public class XMLRootDocument {
XMLRoot = xmlRoot;
Prefix = prefix;
IdentificationString = idString;
Date = DateTime.Today;
Date = DateTime.Now;
_Element = element;
}

View File

@@ -1,7 +1,14 @@
namespace HaWeb;
public static class Features {
// If Admin Pages are reachable
public const string AdminService = "AdminService";
// If the Upload of files is possible, also syntaxcheck and crossreference check
public const string UploadService = "UploadService";
public const string UpdateService = "UpdateService";
// If uploaded Files can be published locally
public const string LocalPublishService = "LocalPublishService";
// If this server can publish files remotely (e.g. www.hamann-ausgabe.de)
public const string RemotePublishService = "RemotePublishService";
// If this server can accept files from a remote authenticated source
public const string RemotePublishSourceService = "RemotePublishSourceService";
}

View File

@@ -5,5 +5,5 @@ class HaDocumentOptions : IHaDocumentOptions {
public string HamannXMLFilePath { get; set; } = @"Hamann.xml";
public string[] AvailableVolumes { get; set; } = { };
public bool NormalizeWhitespace { get; set; } = true;
public (int, int) AvailableYearRange {get; set; } = (1751, 1788);
public (int, int) AvailableYearRange {get; set; } = (1700, 1790);
}

View File

@@ -43,4 +43,10 @@ public class CommentRoot : HaWeb.XMLParser.IXMLRoot {
return opus;
}
public void MergeIntoFile(XElement file, XMLRootDocument document) {
if (file.Element("kommentare") == null)
file.AddFirst(new XElement("kommentare"));
file.Element("kommentare")!.AddFirst(document.Root);
}
}

View File

@@ -13,12 +13,12 @@ public class DescriptionsRoot : HaWeb.XMLParser.IXMLRoot {
return false;
};
public Func<XElement, string?> GetKey { get; } = (elem) => {
var index = elem.Attribute("ref");
if (index != null && !String.IsNullOrWhiteSpace(index.Value))
return index.Value;
else return null;
};
// public Func<XElement, string?> GetKey { get; } = (elem) => {
// var index = elem.Attribute("ref");
// if (index != null && !String.IsNullOrWhiteSpace(index.Value))
// return index.Value;
// else return null;
// };
public List<(string, string?)>? GenerateFields(XMLRootDocument document) {
return null;
@@ -38,4 +38,14 @@ public class DescriptionsRoot : HaWeb.XMLParser.IXMLRoot {
return opus;
}
public void MergeIntoFile(XElement file, XMLRootDocument document) {
if (file.Element("descriptions") == null)
file.AddFirst(new XElement("descriptions"));
var elements = document.Root.Elements().Where(x => IsCollectedObject(x));
var root = file.Element("descriptions");
foreach (var element in elements) {
root!.Add(element);
}
}
}

View File

@@ -2,6 +2,7 @@ namespace HaWeb.Settings.XMLRoots;
using System.Xml.Linq;
using HaWeb.Models;
using HaWeb.XMLParser;
using System.IO;
public class DocumentRoot : HaWeb.XMLParser.IXMLRoot {
public string Type { get; } = "Brieftext";
@@ -38,4 +39,14 @@ public class DocumentRoot : HaWeb.XMLParser.IXMLRoot {
return opus;
}
public void MergeIntoFile(XElement file, XMLRootDocument document) {
if (file.Element("document") == null)
file.AddFirst(new XElement("document"));
var elements = document.Root.Elements().Where(x => IsCollectedObject(x));
var root = file.Element("document");
foreach (var element in elements) {
root!.Add(element);
}
}
}

View File

@@ -38,4 +38,14 @@ public class EditsRoot : HaWeb.XMLParser.IXMLRoot {
return opus;
}
public void MergeIntoFile(XElement file, XMLRootDocument document) {
if (file.Element("edits") == null)
file.AddFirst(new XElement("edits"));
var elements = document.Root.Elements().Where(x => IsCollectedObject(x));
var root = file.Element("edits");
foreach (var element in elements) {
root!.Add(element);
}
}
}

View File

@@ -38,4 +38,14 @@ public class MarginalsRoot : HaWeb.XMLParser.IXMLRoot {
return opus;
}
public void MergeIntoFile(XElement file, XMLRootDocument document) {
if (file.Element("marginalien") == null)
file.AddFirst(new XElement("marginalien"));
var elements = document.Root.Elements().Where(x => IsCollectedObject(x));
var root = file.Element("marginalien");
foreach (var element in elements) {
root!.Add(element);
}
}
}

View File

@@ -36,4 +36,14 @@ public class ReferencesRoot : HaWeb.XMLParser.IXMLRoot {
return opus;
}
public void MergeIntoFile(XElement file, XMLRootDocument document) {
if (file.Element("definitions") == null)
file.AddFirst(new XElement("definitions"));
var elements = document.Root.Elements().Where(x => IsCollectedObject(x));
var root = file.Element("definitions");
foreach (var element in elements) {
root!.Add(element);
}
}
}

View File

@@ -38,4 +38,13 @@ public class TraditionsRoot : HaWeb.XMLParser.IXMLRoot {
return opus;
}
public void MergeIntoFile(XElement file, XMLRootDocument document) {
if (file.Element("traditions") == null)
file.AddFirst(new XElement("traditions"));
var elements = document.Root.Elements().Where(x => IsCollectedObject(x));
var root = file.Element("traditions");
foreach (var element in elements) {
root!.Add(element);
}
}
}

View File

@@ -36,14 +36,13 @@
</div>
</form>
<form class="ha-publishform" action="Upload" method="post" enctype="multipart/form-data">
<label class="filelabel">
<input class="hidden" type="file" accept=".xml" name="file" />
<form class="ha-publishform" id="ha-publishform" asp-controller="API" asp-action="LocalPublish" method="post" enctype="multipart/form-data">
<label class="ha-uploadfilelabel" id="ha-uploadfilelabel">
Veröffentlichen
</label>
<div class="ha-uploadmessage">
<output form="uploadForm" name="result"></output>
<div class="ha-publishmessage" id="ha-publishmessage">
@* Fehler!<br/> *@
<output form="uploadForm" name="publish-result"></output>
</div>
</form>
</div>
@@ -119,6 +118,27 @@
ev.preventDefault();
}
const LOCALPUBLISHSubmit = async function (oFormElement) {
var fd = new FormData();
document.getElementById("ha-publishmessage").style.opacity = "0";
await fetch(oFormElement.action, {
method: 'POST',
headers: {
'RequestVerificationToken': getCookie('RequestVerificationToken')
}
})
.then(response => response.json())
.then(json => {
if ("Error" in json) {
document.getElementById("ha-publishmessage").style.opacity = "1";
oFormElement.elements.namedItem("update-result").value = json.Error;
} else {
oFormElement.elements.namedItem("update-result").value = "Erfolg!";
}
})
.catch ((e) => console.log('Error:', e))
}
const UPLOADSubmit = async function (oFormElement, file = null) {
var fd = new FormData();
if (file !== null) fd.append("file", file);
@@ -159,6 +179,9 @@
var submitelement = document.getElementById("file");
var formelement = document.getElementById("uploadForm");
var dropzone = document.getElementById("dropzone");
var publishelement = document.getElementById("ha-publishform");
var publishbutton = document.getElementById("ha-uploadfilelabel");
publishbutton.addEventListener("click", () => LOCALPUBLISHSubmit(publishelement));
submitelement.addEventListener("change", () => UPLOADSubmit(formelement));
dropzone.addEventListener("drop", (ev) => dropHandler(formelement, ev, dropzone));
dropzone.addEventListener("dragover", (ev) => dragOverHandler(ev, dropzone));

View File

@@ -17,7 +17,7 @@ public interface IXMLRoot {
public abstract Predicate<XElement> IsCollectedObject { get; }
// Gets the Key of a collected object
public abstract Func<XElement, string?> GetKey { get; }
// public abstract Func<XElement, string?> GetKey { get; }
// Can the Root be found within that document?
public List<XElement>? IsTypeOf(XElement root) {
@@ -45,18 +45,20 @@ public interface IXMLRoot {
// Further deciding which of two documents replaces which
public abstract bool Replaces(XMLRootDocument doc1, XMLRootDocument doc2);
public Dictionary<string, XElement>? GetCollectedObjects(XMLRootDocument document) {
Dictionary<string, XElement>? ret = null;
var root = document.Root;
root.Elements().Where(x => this.IsCollectedObject(x)).ToList().ForEach(x => {
var id = this.GetKey(x);
if (id != null) {
if (ret == null) ret = new Dictionary<string, XElement>();
ret.Add(id, x);
}
});
return ret;
}
// public Dictionary<string, XElement>? GetCollectedObjects(XMLRootDocument document) {
// Dictionary<string, XElement>? ret = null;
// var root = document.Root;
// root.Elements().Where(x => this.IsCollectedObject(x)).ToList().ForEach(x => {
// var id = this.GetKey(x);
// if (id != null) {
// if (ret == null) ret = new Dictionary<string, XElement>();
// ret.Add(id, x);
// }
// });
// return ret;
// }
public abstract XElement CreateHamannDocument(XElement element);
public abstract void MergeIntoFile(XElement file, XMLRootDocument document);
}

View File

@@ -7,8 +7,9 @@ public interface IXMLService {
public IXMLRoot? GetRoot(string name);
public List<IXMLRoot>? GetRootsList();
public Dictionary<string, IXMLRoot>? GetRootsDictionary();
public Task<List<XMLRootDocument>?> ProbeHamannFile(XDocument document, ModelStateDictionary ModelState);
public List<XMLRootDocument>? ProbeHamannFile(XDocument document, ModelStateDictionary ModelState);
public Dictionary<string, FileList?>? GetUsedDictionary();
public XElement? MergeUsedDocuments(ModelStateDictionary ModelState);
public void Use(XMLRootDocument doc);
public void AutoUse(string prefix);
public void AutoUse(FileList filelist);

View File

@@ -30,7 +30,7 @@ public class XMLService : IXMLService {
public Dictionary<string, IXMLRoot>? GetRootsDictionary() => this._Roots == null ? null : this._Roots;
public async Task<List<XMLRootDocument>?> ProbeHamannFile(XDocument document, ModelStateDictionary ModelState) {
public List<XMLRootDocument>? ProbeHamannFile(XDocument document, ModelStateDictionary ModelState) {
if (document.Root!.Name != "opus") {
ModelState.AddModelError("Error", "A valid Hamann-Docuemnt must begin with <opus>");
return null;
@@ -86,6 +86,27 @@ public class XMLService : IXMLService {
}
}
public XElement? MergeUsedDocuments(ModelStateDictionary ModelState) {
if (_Used == null || _Roots == null) {
ModelState.AddModelError("Error", "Keine Dokumente ausgewählt");
return null;
}
var opus = new XElement("opus");
foreach (var category in _Used) {
if (category.Value == null || category.Value.GetFileList() == null || !category.Value.GetFileList()!.Any()) {
ModelState.AddModelError("Error", _Roots![category.Key].Type + " nicht vorhanden.");
return null;
}
var documents = category.Value.GetFileList();
foreach (var document in documents!) {
document.XMLRoot.MergeIntoFile(opus, document);
}
}
return opus;
}
private XMLRootDocument _createXMLRootDocument(IXMLRoot Root, XElement element) {
var doc = new XMLRootDocument(Root, Root.Prefix, Root.GenerateIdentificationString(element), element);
doc.Fields = Root.GenerateFields(doc);

View File

@@ -8,7 +8,9 @@
"FeatureManagement": {
"AdminService": true,
"UploadService": true,
"UpdateService": false
"LocalPublishService": true,
"RemotePublishService": false,
"RemotePublishSourceService": false
},
"AllowedHosts": "*",
"StoredFilePathLinux": "/home/simon/Downloads/test/",