mirror of
https://github.com/Theodor-Springmann-Stiftung/hamann-ausgabe-core.git
synced 2025-10-29 17:25:32 +00:00
Setup Git Repository Parsing
This commit is contained in:
@@ -17,8 +17,6 @@ public class FileList {
|
||||
throw new Exception("Diese Liste kann nur Elemente des Typs " + XMLRoot.Prefix + " enthalten");
|
||||
|
||||
if (_Files == null) _Files = new HashSet<XMLRootDocument>();
|
||||
var replacing = _Files.Where(x => x.FileName == document.FileName);
|
||||
if (replacing != null && replacing.Any()) _Files.Remove(replacing.First());
|
||||
_Files.Add(document);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,19 +1,46 @@
|
||||
using System.Text;
|
||||
using Microsoft.Extensions.FileProviders;
|
||||
|
||||
namespace HaWeb.Models;
|
||||
|
||||
public class FileModel {
|
||||
public string FileName { get; private set; }
|
||||
public string Prefix { get; private set; }
|
||||
public DateTime LastModified { get; private set; }
|
||||
public bool IsUsed { get; private set; }
|
||||
public bool InProduction { get; private set; }
|
||||
public List<(string, string?)>? Fields { get; set; }
|
||||
public string? Messages { get; set; }
|
||||
public IFileInfo File { get; private set; }
|
||||
|
||||
public FileModel(string name, string prefix, DateTime lastModified, bool isUsed, bool inProduction) {
|
||||
// This affects only repo files
|
||||
public bool IsValid { get; private set; } = false;
|
||||
public List<XMLRootDocument>? Content { get; set; }
|
||||
public List<(string, string?)>? Fields { get; set; }
|
||||
public string? Prefix { get; set; }
|
||||
|
||||
private StringBuilder? _log;
|
||||
|
||||
public FileModel(string name, IFileInfo file) {
|
||||
FileName = name;
|
||||
IsUsed = isUsed;
|
||||
LastModified = lastModified;
|
||||
InProduction = inProduction;
|
||||
Prefix = prefix;
|
||||
File = file;
|
||||
}
|
||||
|
||||
public string? GetLog() {
|
||||
if (_log == null) return null;
|
||||
return _log.ToString();
|
||||
}
|
||||
|
||||
public void Log(string msg) {
|
||||
if (_log == null) _log = new StringBuilder();
|
||||
var prefix = DateTime.Now.ToShortTimeString() + " ";
|
||||
if (File != null) prefix += File.Name + ": ";
|
||||
_log.AppendLine(prefix + msg);
|
||||
}
|
||||
|
||||
public void ResetLog() {
|
||||
if (_log != null) _log.Clear();
|
||||
}
|
||||
|
||||
public void Validate() {
|
||||
IsValid = true;
|
||||
}
|
||||
|
||||
public DateTime GetLastModified() {
|
||||
return File.LastModified.ToLocalTime().DateTime;
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,7 @@ using System.Collections.Generic;
|
||||
public class SearchResult {
|
||||
public string Search { get; private set; }
|
||||
public string Index { get; private set; }
|
||||
public string Identifier { get; set; }
|
||||
public string? Identifier { get; set; }
|
||||
public string? Page { get; set; }
|
||||
public string? Line { get; set; }
|
||||
public string? Preview { get; set; }
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
namespace HaWeb.Models;
|
||||
using HaWeb.XMLParser;
|
||||
using Microsoft.Extensions.FileProviders;
|
||||
using Microsoft.AspNetCore.Mvc.Rendering;
|
||||
|
||||
public class UploadViewModel {
|
||||
public string ActiveTitle { get; private set; }
|
||||
public string? Prefix { get; private set; }
|
||||
public List<IXMLRoot>? AvailableRoots { get; private set; }
|
||||
public List<FileModel>? AvailableFiles { get; set; }
|
||||
public Dictionary<string, List<FileModel>?>? UsedFiles { get; private set; }
|
||||
public Dictionary<string, List<FileModel>?>? ProductionFiles { get; set; }
|
||||
public List<int> AvailableYears { get; private set; }
|
||||
public int StartYear { get; private set; }
|
||||
public int EndYear { get; private set; }
|
||||
public List<FileModel>? HamannFiles { get; set; }
|
||||
|
||||
public UploadViewModel(string title, string? prefix, List<IXMLRoot>? roots, Dictionary<string, List<FileModel>?>? usedFiles, int startYear, int endYear, List<int> availableYears) {
|
||||
Prefix = prefix;
|
||||
ActiveTitle = title;
|
||||
AvailableRoots = roots;
|
||||
UsedFiles = usedFiles;
|
||||
StartYear = startYear;
|
||||
EndYear = endYear;
|
||||
AvailableYears = availableYears;
|
||||
}
|
||||
}
|
||||
@@ -7,154 +7,26 @@ using HaWeb.XMLParser;
|
||||
using System.Text;
|
||||
|
||||
public class XMLRootDocument {
|
||||
private XElement? _Element;
|
||||
private string? _filename;
|
||||
private IFileInfo? _file;
|
||||
private StringBuilder? _log;
|
||||
|
||||
[JsonIgnore]
|
||||
public XElement? Element { get; private set; }
|
||||
[JsonIgnore]
|
||||
public IXMLRoot XMLRoot { get; private set; }
|
||||
public FileModel File { get; private set; }
|
||||
|
||||
public string FileName {
|
||||
get {
|
||||
if (_filename == null)
|
||||
_filename = _CreateFilename();
|
||||
return _filename;
|
||||
}
|
||||
}
|
||||
|
||||
[JsonIgnore]
|
||||
public IFileInfo? File {
|
||||
get {
|
||||
return _file;
|
||||
}
|
||||
set {
|
||||
_file = value;
|
||||
_Element = null;
|
||||
} }
|
||||
public string Prefix { get; private set; }
|
||||
public DateTime Date { get; private set; }
|
||||
|
||||
// UNUSED AS OF NOW
|
||||
public (string?, string?) IdentificationString { get; private set; }
|
||||
// TODO: Fields
|
||||
public List<(string, string?)>? Fields { get; set; }
|
||||
|
||||
// Entry point for file reading
|
||||
public XMLRootDocument(IXMLRoot xmlRoot, IFileInfo file) {
|
||||
XMLRoot = xmlRoot;
|
||||
Prefix = xmlRoot.Prefix;
|
||||
File = file;
|
||||
Date = file.LastModified.LocalDateTime;
|
||||
_filename = file.Name;
|
||||
_GenerateFieldsFromFilename(file.Name);
|
||||
}
|
||||
|
||||
// Entry point for XML upload reading
|
||||
public XMLRootDocument(IXMLRoot xmlRoot, string prefix, (string?, string?) idString, XElement element) {
|
||||
// Entry point for XML file reading
|
||||
public XMLRootDocument(IXMLRoot xmlRoot, string prefix, (string?, string?) idString, XElement element, FileModel file) {
|
||||
XMLRoot = xmlRoot;
|
||||
Prefix = prefix;
|
||||
IdentificationString = idString;
|
||||
Date = DateTime.Now;
|
||||
_Element = element;
|
||||
File = file;
|
||||
Element = element;
|
||||
}
|
||||
|
||||
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()) + "." + Path.GetRandomFileName();
|
||||
return filename + ".xml";
|
||||
}
|
||||
|
||||
private string _removeInvalidChars(string? s) {
|
||||
if (String.IsNullOrWhiteSpace(s)) return string.Empty;
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
public string? GetLog() {
|
||||
if (_log == null) return null;
|
||||
return _log.ToString();
|
||||
}
|
||||
|
||||
public void Log(string msg) {
|
||||
if (_log == null) _log = new StringBuilder();
|
||||
var prefix = DateTime.Now.ToShortTimeString() + " ";
|
||||
if (File != null) prefix += File.Name + ": ";
|
||||
_log.AppendLine(prefix + msg);
|
||||
}
|
||||
|
||||
public void ResetLog() {
|
||||
if (_log != null) _log.Clear();
|
||||
}
|
||||
|
||||
// Call on UnUse to prevent memory hogging
|
||||
public void UnUse() {
|
||||
_Element = null;
|
||||
_log = null;
|
||||
}
|
||||
|
||||
public XElement GetElement() {
|
||||
if (_Element == null)
|
||||
_Element = _GetElement();
|
||||
return _Element;
|
||||
}
|
||||
|
||||
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.");
|
||||
|
||||
if (XMLRoot == 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 = XMLRoot.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) {
|
||||
if (XMLRoot == null) {
|
||||
state.AddModelError("Error", "No corresponding Root Element found.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (_Element == null) {
|
||||
if (File == null) {
|
||||
state.AddModelError("Error", "There is neither a file nor a saved element for this Document aborting the save.");
|
||||
return;
|
||||
}
|
||||
_Element = GetElement();
|
||||
}
|
||||
|
||||
await XMLRoot.CreateHamannDocument(_Element).SaveAsync(stream, SaveOptions.DisableFormatting, new CancellationToken());
|
||||
}
|
||||
|
||||
}
|
||||
36
HaWeb/Models/XMLStateViewModel.cs
Normal file
36
HaWeb/Models/XMLStateViewModel.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
namespace HaWeb.Models;
|
||||
using HaWeb.XMLParser;
|
||||
using Microsoft.Extensions.FileProviders;
|
||||
using Microsoft.AspNetCore.Mvc.Rendering;
|
||||
|
||||
public class XMLStateViewModel {
|
||||
// Titel der Seite / Aktiver Präfix
|
||||
public string ActiveTitle { get; private set; }
|
||||
public IFileInfo? ActiveFile { get; set; }
|
||||
public (DateTime PullTime, string Hash)? GitData { get; private set; }
|
||||
public bool ValidState { get; private set; }
|
||||
|
||||
// Verfügbare Datei-Typen
|
||||
public List<IXMLRoot>? AvailableRoots { get; private set; }
|
||||
|
||||
// Akuell geladene Dateien
|
||||
public List<FileModel>? ManagedFiles { get; private set; }
|
||||
|
||||
// Verfügbare (Gesamt-)Dateien
|
||||
public List<IFileInfo>? HamannFiles { get; set; }
|
||||
|
||||
public XMLStateViewModel(
|
||||
string title,
|
||||
(DateTime PullTime, string Hash)? gitData,
|
||||
List<IXMLRoot>? roots,
|
||||
List<IFileInfo>? hamannFiles,
|
||||
List<FileModel>? managedFiles,
|
||||
bool validState) {
|
||||
ActiveTitle = title;
|
||||
AvailableRoots = roots;
|
||||
HamannFiles = hamannFiles;
|
||||
ManagedFiles = managedFiles;
|
||||
GitData = gitData;
|
||||
ValidState = validState;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user