mirror of
https://github.com/Theodor-Springmann-Stiftung/hamann-ausgabe-core.git
synced 2025-10-29 09:15:33 +00:00
Seperation of concerns: Seperated used File Management form Overall FIle Management
This commit is contained in:
25
HaWeb/Models/FileList.cs
Normal file
25
HaWeb/Models/FileList.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
namespace HaWeb.Models;
|
||||
using HaWeb.XMLParser;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
public class FileList {
|
||||
private HashSet<XMLRootDocument>? _Files;
|
||||
|
||||
[JsonIgnore]
|
||||
public IXMLRoot XMLRoot { get; private set; }
|
||||
|
||||
public FileList(IXMLRoot xmlRoot) {
|
||||
XMLRoot = xmlRoot;
|
||||
}
|
||||
|
||||
public void Add(XMLRootDocument document) {
|
||||
if (document.Prefix != XMLRoot.Prefix)
|
||||
throw new Exception("Diese Liste kann nur Elemente des Typs " + XMLRoot.Prefix + " enthalten");
|
||||
|
||||
if (_Files == null) _Files = new HashSet<XMLRootDocument>();
|
||||
if (!_Files.Contains(document)) _Files.Add(document);
|
||||
}
|
||||
|
||||
public List<XMLRootDocument>? GetFileList()
|
||||
=> this._Files != null ? this._Files.ToList() : null;
|
||||
}
|
||||
@@ -5,11 +5,11 @@ public class UploadViewModel {
|
||||
public string ActiveTitle { get; private set; }
|
||||
public string? Prefix { get; private set; }
|
||||
public List<IXMLRoot>? AvailableRoots { get; private set; }
|
||||
public List<XMLRootDocument>? AvailableFiles { get; private set; }
|
||||
public Dictionary<string, List<XMLRootDocument>>? UsedFiles { get; private set; }
|
||||
public FileList? AvailableFiles { get; private set; }
|
||||
public Dictionary<string, FileList>? UsedFiles { get; private set; }
|
||||
|
||||
|
||||
public UploadViewModel(string title, string? prefix, List<IXMLRoot>? roots, List<XMLRootDocument>? availableFiles, Dictionary<string, List<XMLRootDocument>>? usedFiles) {
|
||||
public UploadViewModel(string title, string? prefix, List<IXMLRoot>? roots, FileList? availableFiles, Dictionary<string, FileList>? usedFiles) {
|
||||
Prefix = prefix;
|
||||
ActiveTitle = title;
|
||||
AvailableRoots = roots;
|
||||
|
||||
126
HaWeb/Models/XMLRootDocument.cs
Normal file
126
HaWeb/Models/XMLRootDocument.cs
Normal file
@@ -0,0 +1,126 @@
|
||||
namespace HaWeb.Models;
|
||||
using System.Xml.Linq;
|
||||
using System.Text.Json.Serialization;
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
using Microsoft.Extensions.FileProviders;
|
||||
using HaWeb.XMLParser;
|
||||
|
||||
public class XMLRootDocument {
|
||||
private XElement? _Element;
|
||||
private string? _filename;
|
||||
public IXMLRoot XMLRoot { get; private set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public XElement Root {
|
||||
get {
|
||||
if (_Element == null) {
|
||||
_Element = _GetElement();
|
||||
}
|
||||
return _Element;
|
||||
}
|
||||
}
|
||||
|
||||
public string FileName {
|
||||
get {
|
||||
if (_filename == null)
|
||||
_filename = _CreateFilename();
|
||||
return _filename;
|
||||
}
|
||||
}
|
||||
|
||||
[JsonIgnore]
|
||||
public IFileInfo? File { get; private set; }
|
||||
public string Prefix { get; private set; }
|
||||
public DateTime Date { get; private set; }
|
||||
|
||||
public (string?, string?) IdentificationString { get; private set; }
|
||||
public List<(string, string?)>? Fields { get; set; }
|
||||
|
||||
// Entry point for file reading
|
||||
public XMLRootDocument(IXMLRoot xmlRoot, IFileInfo file) {
|
||||
XMLRoot = xmlRoot;
|
||||
Prefix = xmlRoot.Prefix;
|
||||
SetFile(file);
|
||||
}
|
||||
|
||||
// Entry point for XML upload reading
|
||||
public XMLRootDocument(IXMLRoot xmlRoot, string prefix, (string?, string?) idString, XElement element) {
|
||||
XMLRoot = xmlRoot;
|
||||
Prefix = prefix;
|
||||
IdentificationString = idString;
|
||||
Date = DateTime.Today;
|
||||
_Element = element;
|
||||
}
|
||||
|
||||
public void SetFile(IFileInfo file) {
|
||||
File = file;
|
||||
Date = file.LastModified.DateTime;
|
||||
_GenerateFieldsFromFilename(file.Name);
|
||||
}
|
||||
|
||||
private string _CreateFilename() {
|
||||
var filename = _removeInvalidChars(Prefix) + "_";
|
||||
if (!String.IsNullOrWhiteSpace(IdentificationString.Item1)) {
|
||||
var hash = IdentificationString.Item1.GetHashCode().ToString("X8");
|
||||
filename += hash + "_";
|
||||
}
|
||||
if (!String.IsNullOrWhiteSpace(IdentificationString.Item2)) filename += _removeInvalidChars(IdentificationString.Item2) + "_";
|
||||
filename += _removeInvalidChars(Date.Year.ToString() + "-" + Date.Month.ToString() + "-" + Date.Day.ToString());
|
||||
return filename + ".xml";
|
||||
}
|
||||
|
||||
private string _removeInvalidChars(string? s) {
|
||||
if (String.IsNullOrWhiteSpace(s)) return "";
|
||||
foreach (var c in Path.GetInvalidFileNameChars()) {
|
||||
s = s.Replace(c, '-');
|
||||
}
|
||||
s = s.Replace('_', '-');
|
||||
return s;
|
||||
}
|
||||
|
||||
private void _GenerateFieldsFromFilename(string filename) {
|
||||
var split = filename.Split('_');
|
||||
Prefix = split[0];
|
||||
if (split.Length == 3) {
|
||||
IdentificationString = (null, split[1]);
|
||||
} else if (split.Length == 4) {
|
||||
IdentificationString = (split[1], split[2]);
|
||||
} else {
|
||||
IdentificationString = (null, null);
|
||||
}
|
||||
}
|
||||
|
||||
private XElement _GetElement() {
|
||||
if (File == null || String.IsNullOrWhiteSpace(File.PhysicalPath) || !File.Exists)
|
||||
throw new Exception("Es ist kein Pfad für die XML-Datei vorhanden.");
|
||||
|
||||
var root = XMLRoot;
|
||||
if (root == null)
|
||||
throw new Exception("Kein gültiges Hamann-Dokument: " + File.PhysicalPath + "Vom Prefix: " + Prefix);
|
||||
|
||||
XDocument? doc = null;
|
||||
try {
|
||||
doc = XDocument.Load(File.PhysicalPath, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
|
||||
} catch (Exception ex) {
|
||||
throw new Exception("Fehler beim Lesen des Dokuments: " + ex.Message);
|
||||
}
|
||||
|
||||
if (doc == null || doc.Root == null)
|
||||
throw new Exception("Das Dokument ist ungültig und kann nicht gelesen werden: " + File.PhysicalPath);
|
||||
|
||||
var element = root.IsTypeOf(doc.Root);
|
||||
if (element == null || !element.Any())
|
||||
throw new Exception("Kein gültiges Hamann-Dokument: " + File.PhysicalPath + "Vom Prefix: " + Prefix);
|
||||
|
||||
return element.First();
|
||||
}
|
||||
|
||||
public async Task Save(Stream stream, ModelStateDictionary state) {
|
||||
var root = XMLRoot;
|
||||
if (root == null) {
|
||||
state.AddModelError("Error", "No corresponding Root Element found.");
|
||||
return;
|
||||
}
|
||||
await root.CreateHamannDocument(Root).SaveAsync(stream, SaveOptions.DisableFormatting, new CancellationToken());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user