mirror of
https://github.com/Theodor-Springmann-Stiftung/hamann-ausgabe-core.git
synced 2025-10-30 01:35:32 +00:00
Added Upload functionality; still a bit janky in selecting the files to use...
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user