Added Classes for detecting Root Elements of Hamann-Collections

This commit is contained in:
schnulller
2022-06-02 19:26:43 +02:00
parent 2762a5e310
commit 35ce2034f7
20 changed files with 526 additions and 40 deletions

View File

@@ -0,0 +1,38 @@
namespace HaWeb.FileHelpers;
using System.Xml.Linq;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using System.Text;
using System.Xml;
public static class XDocumentFileHelper {
private readonly static XmlReaderSettings _Settings = new XmlReaderSettings()
{
CloseInput = true,
CheckCharacters = false,
ConformanceLevel = ConformanceLevel.Fragment,
IgnoreComments = true,
IgnoreProcessingInstructions = true,
IgnoreWhitespace = false
};
public static async Task<XDocument?> ProcessStreamedFile(byte[] bytes, ModelStateDictionary modelState) {
try
{
using (var stream = new MemoryStream(bytes))
{
using (var xmlreader = XmlReader.Create(stream, _Settings))
{
return XDocument.Load(xmlreader, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
}
}
}
catch (Exception ex)
{
modelState.AddModelError("Error", $"Kein gültiges XML-Dokument geladen. Error: {ex.Message}");
}
return null;
}
}

View File

@@ -140,7 +140,7 @@ public static class XMLFileHelpers
// return Array.Empty<byte>();
// }
public static async Task<byte[]> ProcessStreamedFile(
public static async Task<byte[]?> ProcessStreamedFile(
MultipartSection section, ContentDispositionHeaderValue contentDisposition,
ModelStateDictionary modelState, string[] permittedExtensions, long sizeLimit)
{
@@ -152,16 +152,16 @@ public static class XMLFileHelpers
// Check if the file is empty or exceeds the size limit.
if (memoryStream.Length == 0)
modelState.AddModelError("File", "The file is empty.");
modelState.AddModelError("Error", "The file is empty.");
else if (memoryStream.Length > sizeLimit)
{
var megabyteSizeLimit = sizeLimit / 1048576;
modelState.AddModelError("File", $"The file exceeds {megabyteSizeLimit:N1} MB.");
modelState.AddModelError("Error", $"The file exceeds {megabyteSizeLimit:N1} MB.");
}
// Check file extension and first bytes
else if (!IsValidFileExtensionAndSignature(contentDisposition.FileName.Value, memoryStream, permittedExtensions))
modelState.AddModelError("File", "The file must be of the following specs:<br>" +
modelState.AddModelError("Error", "The file must be of the following specs:<br>" +
"1. The file must hava a .xml File-Extension<br>" +
"2. To make sure the file isn't executable the file must start with: <?xml version=\"1.0\" encoding=\"utf-8\"?> or <?xml version=\"1.0\"?>");
@@ -171,10 +171,10 @@ public static class XMLFileHelpers
}
catch (Exception ex)
{
modelState.AddModelError("File", $"The upload failed. Error: {ex.HResult}");
modelState.AddModelError("Error", $"The upload failed. Error: {ex.Message}");
}
return Array.Empty<byte>();
return null;
}
private static bool IsValidFileExtensionAndSignature(string fileName, Stream data, string[] permittedExtensions)