mirror of
https://github.com/Theodor-Springmann-Stiftung/hamann-ausgabe-core.git
synced 2025-10-29 09:15:33 +00:00
Added Models for displaying files and all kinds of stuff
This commit is contained in:
@@ -1,26 +1,43 @@
|
||||
namespace HaWeb.FileHelpers;
|
||||
using HaDocument.Interfaces;
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
using Microsoft.Extensions.FileProviders;
|
||||
|
||||
public class HaDocumentWrapper : IHaDocumentWrappper {
|
||||
public ILibrary Library;
|
||||
private ILibrary Library;
|
||||
private IXMLProvider _xmlProvider;
|
||||
|
||||
private int _startYear;
|
||||
private int _endYear;
|
||||
|
||||
public HaDocumentWrapper(IXMLProvider xmlProvider, IConfiguration configuration) {
|
||||
_xmlProvider = xmlProvider;
|
||||
|
||||
_startYear = configuration.GetValue<int>("AvailableStartYear");
|
||||
_endYear = configuration.GetValue<int>("AvailableEndYear");
|
||||
|
||||
var filelist = xmlProvider.GetHamannFiles();
|
||||
if (filelist != null && filelist.Any())
|
||||
_AutoLoad(filelist);
|
||||
|
||||
// Use Fallback library
|
||||
if (Library == null)
|
||||
Library = HaDocument.Document.Create(new HaWeb.Settings.HaDocumentOptions() { AvailableYearRange = (_startYear, _endYear) });
|
||||
|
||||
public HaDocumentWrapper() {
|
||||
Library = HaDocument.Document.Create(new HaWeb.Settings.HaDocumentOptions());
|
||||
}
|
||||
|
||||
public ILibrary SetLibrary() {
|
||||
Library = HaDocument.Document.Create(new HaWeb.Settings.HaDocumentOptions());
|
||||
public ILibrary ResetLibrary() {
|
||||
Library = HaDocument.Document.Create(new HaWeb.Settings.HaDocumentOptions() { AvailableYearRange = (_startYear, _endYear) });
|
||||
return Library;
|
||||
}
|
||||
|
||||
public ILibrary? SetLibrary(string filepath, ModelStateDictionary ModelState) {
|
||||
public ILibrary? SetLibrary(string filepath, ModelStateDictionary? ModelState = null) {
|
||||
try
|
||||
{
|
||||
Library = HaDocument.Document.Create(new HaWeb.Settings.HaDocumentOptions() { HamannXMLFilePath = filepath });
|
||||
Library = HaDocument.Document.Create(new HaWeb.Settings.HaDocumentOptions() { HamannXMLFilePath = filepath, AvailableYearRange = (_startYear, _endYear) });
|
||||
}
|
||||
catch (Exception ex) {
|
||||
ModelState.AddModelError("Error:", "Das Dokument konnte nicht geparst werden: " + ex.Message);
|
||||
if (ModelState != null) ModelState.AddModelError("Error:", "Das Dokument konnte nicht geparst werden: " + ex.Message);
|
||||
return null;
|
||||
}
|
||||
return Library;
|
||||
@@ -29,4 +46,11 @@ public class HaDocumentWrapper : IHaDocumentWrappper {
|
||||
public ILibrary GetLibrary() {
|
||||
return Library;
|
||||
}
|
||||
|
||||
private void _AutoLoad(List<IFileInfo> files) {
|
||||
var orderdlist = files.OrderByDescending(x => x.LastModified);
|
||||
foreach(var item in orderdlist) {
|
||||
if (SetLibrary(item.PhysicalPath) != null) return;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@ using HaDocument.Interfaces;
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
|
||||
public interface IHaDocumentWrappper {
|
||||
public ILibrary SetLibrary();
|
||||
public ILibrary ResetLibrary();
|
||||
public ILibrary? SetLibrary(string filepath, ModelStateDictionary ModelState);
|
||||
public ILibrary GetLibrary();
|
||||
}
|
||||
@@ -9,4 +9,5 @@ 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);
|
||||
public List<IFileInfo>? GetHamannFiles();
|
||||
}
|
||||
@@ -11,6 +11,7 @@ using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
using Microsoft.AspNetCore.WebUtilities;
|
||||
using Microsoft.Net.Http.Headers;
|
||||
using HaWeb.Models;
|
||||
|
||||
public static class XMLFileHelpers {
|
||||
// File Signatures Database (https://www.filesignatures.net/)
|
||||
@@ -139,6 +140,34 @@ public static class XMLFileHelpers {
|
||||
// return Array.Empty<byte>();
|
||||
// }
|
||||
|
||||
public static List<FileModel>? ToFileModel(FileList? fileList, Dictionary<string, FileList?>? productionFiles = null, Dictionary<string, FileList?>? usedFiles = null) {
|
||||
if (fileList == null) return null;
|
||||
var fL = fileList.GetFileList();
|
||||
if (fL == null) return null;
|
||||
var ret = new List<FileModel>();
|
||||
foreach (var f in fL) {
|
||||
if (f.File == null) continue;
|
||||
ret.Add(ToFileModel(f, productionFiles, usedFiles));
|
||||
};
|
||||
return ret.OrderBy(x => x.LastModified).ToList();
|
||||
}
|
||||
|
||||
public static FileModel ToFileModel(XMLRootDocument document, Dictionary<string, FileList?>? productionFiles = null, Dictionary<string, FileList?>? usedFiles = null) {
|
||||
string id = document.Prefix;
|
||||
|
||||
bool inProduction = false;
|
||||
if (productionFiles != null && productionFiles.ContainsKey(id)) {
|
||||
inProduction = productionFiles[id]!.Contains(document);
|
||||
}
|
||||
|
||||
bool isUsed = false;
|
||||
if (usedFiles != null && usedFiles.ContainsKey(id)) {
|
||||
isUsed = usedFiles[id]!.Contains(document);
|
||||
}
|
||||
|
||||
return new FileModel(document.FileName, document.Prefix, document.File.LastModified.LocalDateTime, isUsed, inProduction) { Fields = document.Fields };
|
||||
}
|
||||
|
||||
public static async Task<byte[]?> ProcessStreamedFile(
|
||||
MultipartSection section, ContentDispositionHeaderValue contentDisposition,
|
||||
ModelStateDictionary modelState, string[] permittedExtensions, long sizeLimit) {
|
||||
|
||||
@@ -10,11 +10,13 @@ public class XMLProvider : IXMLProvider {
|
||||
private Dictionary<string, FileList?>? _Files;
|
||||
private Dictionary<string, IXMLRoot>? _Roots;
|
||||
private List<IFileInfo>? _HamannFiles;
|
||||
private IFileInfo? _InProduction;
|
||||
|
||||
public XMLProvider(IFileProvider provider, IXMLService xmlservice) {
|
||||
_fileProvider = provider;
|
||||
_Roots = xmlservice.GetRootsDictionary();
|
||||
_Files = _ScanFiles();
|
||||
_HamannFiles = _ScanHamannFiles();
|
||||
|
||||
if (_Files != null)
|
||||
foreach(var category in _Files)
|
||||
@@ -22,6 +24,8 @@ public class XMLProvider : IXMLProvider {
|
||||
xmlservice.AutoUse(category.Value);
|
||||
}
|
||||
|
||||
public List<IFileInfo>? GetHamannFiles() => this._HamannFiles;
|
||||
|
||||
public FileList? GetFiles(string prefix)
|
||||
=> _Files != null && _Files.ContainsKey(prefix) ? _Files[prefix] : null;
|
||||
|
||||
@@ -80,7 +84,7 @@ public class XMLProvider : IXMLProvider {
|
||||
|
||||
if (_HamannFiles == null) _HamannFiles = new List<IFileInfo>();
|
||||
_HamannFiles.Add(info);
|
||||
|
||||
_InProduction = info;
|
||||
return info;
|
||||
}
|
||||
|
||||
@@ -110,4 +114,12 @@ public class XMLProvider : IXMLProvider {
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
private List<IFileInfo>? _ScanHamannFiles() {
|
||||
var dir = _fileProvider.GetDirectoryContents(string.Empty).Where(x => x.IsDirectory && x.Name == "hamann");
|
||||
if (dir == null || !dir.Any()) return null;
|
||||
var files = _fileProvider.GetDirectoryContents(dir.First().Name).Where(x => !x.IsDirectory && x.Name.StartsWith("hamann") && x.Name.EndsWith(".xml"));
|
||||
if (files == null || !files.Any()) return null;
|
||||
return files.ToList();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user