3 Commits

Author SHA1 Message Date
Jonathan Jenne
acaa2e7ad6 zugferdrestapi: Fix missing properties 2020-03-25 13:22:32 +01:00
Jonathan Jenne
5ac28e2032 Interfaces: Version 1.0.1.0 2020-03-25 13:22:09 +01:00
Jonathan Jenne
0e592de288 Interfaces: Extract attachments from stream 2020-03-25 13:21:34 +01:00
3 changed files with 90 additions and 69 deletions

View File

@@ -31,5 +31,5 @@ Imports System.Runtime.InteropServices
' übernehmen, indem Sie "*" eingeben: ' übernehmen, indem Sie "*" eingeben:
' <Assembly: AssemblyVersion("1.0.*")> ' <Assembly: AssemblyVersion("1.0.*")>
<Assembly: AssemblyVersion("1.0.0.0")> <Assembly: AssemblyVersion("1.0.1.0")>
<Assembly: AssemblyFileVersion("1.0.0.0")> <Assembly: AssemblyFileVersion("1.0.0.0")>

View File

@@ -67,6 +67,16 @@ Public Class ZUGFeRDInterface
Return SerializeZUGFeRDDocument(oXmlDocument) Return SerializeZUGFeRDDocument(oXmlDocument)
End Function End Function
Public Function ExtractZUGFeRDFileWithGDPicture(Stream As Stream) As CrossIndustryDocumentType
Dim oXmlDocument = ValidateZUGFeRDFileWithGDPicture(Stream)
If IsNothing(oXmlDocument) Then
Throw New ZUGFeRDExecption(ErrorType.NoZugferd, "Datei ist keine ZUGFeRD Datei.")
End If
Return SerializeZUGFeRDDocument(oXmlDocument)
End Function
Public Function ValidateZUGFeRDFile(Path As String) As XPathDocument Public Function ValidateZUGFeRDFile(Path As String) As XPathDocument
Dim oProcessOutput, oProcessError As String Dim oProcessOutput, oProcessError As String
Dim oXmlDocument As XPathDocument Dim oXmlDocument As XPathDocument
@@ -111,46 +121,63 @@ Public Class ZUGFeRDInterface
Return oXmlDocument Return oXmlDocument
End Function End Function
Public Function ValidateZUGFeRDFileWithGDPicture(Stream As Stream) As XPathDocument
Dim oAttachmentExtractor = New PDFAttachments(_logConfig)
Dim oAllowedExtensions = New List(Of String) From {"xml"}
Try
Dim oResults = oAttachmentExtractor.Extract(Stream, oAllowedExtensions)
Return HandleAttachments(oResults)
Catch ex As Exception
_logger.Error(ex)
Throw ex
End Try
End Function
Public Function ValidateZUGFeRDFileWithGDPicture(Path As String) As XPathDocument Public Function ValidateZUGFeRDFileWithGDPicture(Path As String) As XPathDocument
Dim oAttachmentExtractor = New PDFAttachments(_logConfig) Dim oAttachmentExtractor = New PDFAttachments(_logConfig)
Dim oAllowedExtensions = New List(Of String) From {"xml"} Dim oAllowedExtensions = New List(Of String) From {"xml"}
Dim oXmlDocument As XPathDocument
Try Try
Dim oResults = oAttachmentExtractor.Extract(Path, oAllowedExtensions) Dim oResults = oAttachmentExtractor.Extract(Path, oAllowedExtensions)
Return HandleAttachments(oResults)
Catch ex As Exception
_logger.Error(ex)
Throw ex
End Try
End Function
If oResults Is Nothing Then Private Function HandleAttachments(Results As List(Of PDFAttachments.AttachmentResult)) As XPathDocument
Throw New ZUGFeRDExecption(ErrorType.NoZugferd, "Datei ist keine ZUGFeRD Datei.") Dim oXmlDocument As XPathDocument
If Results Is Nothing Then
Throw New ZUGFeRDExecption(ErrorType.NoZugferd, "Datei ist keine ZUGFeRD Datei.")
End If
If Results.Count = 0 Then
Throw New ZUGFeRDExecption(ErrorType.NoZugferd, "Datei ist keine ZUGFeRD Datei.")
End If
Dim oFound As Boolean = False
Dim oFoundResult As PDFAttachments.AttachmentResult = Nothing
For Each oResult In Results
If oResult.FileName.ToUpper() = PDFAttachments.ZUGFERD_XML_FILENAME.ToUpper() Then
oFound = True
oFoundResult = oResult
End If End If
Next
If oResults.Count = 0 Then If Not oFound Then
Throw New ZUGFeRDExecption(ErrorType.NoZugferd, "Datei ist keine ZUGFeRD Datei.") Throw New ZUGFeRDExecption(ErrorType.NoZugferd, "Datei ist keine ZUGFeRD Datei.")
End If End If
Dim oFound As Boolean = False Try
Dim oFoundResult As PDFAttachments.AttachmentResult = Nothing Using oStream As New MemoryStream(oFoundResult.FileContents)
oXmlDocument = New XPathDocument(oStream)
End Using
For Each oResult In oResults Return oXmlDocument
If oResult.FileName.ToUpper() = PDFAttachments.ZUGFERD_XML_FILENAME.ToUpper() Then
oFound = True
oFoundResult = oResult
End If
Next
If Not oFound Then
Throw New ZUGFeRDExecption(ErrorType.NoZugferd, "Datei ist keine ZUGFeRD Datei.")
End If
Try
Using oStream As New MemoryStream(oFoundResult.FileContents)
oXmlDocument = New XPathDocument(oStream)
End Using
Return oXmlDocument
Catch ex As Exception
_logger.Error(ex)
Throw ex
End Try
Catch ex As Exception Catch ex As Exception
_logger.Error(ex) _logger.Error(ex)
Throw ex Throw ex

View File

@@ -77,7 +77,7 @@ namespace ZUGFeRDRESTService.Controllers
} }
/// <summary> /// <summary>
/// POST: /api/Validation /// POST: /api/validation
/// </summary> /// </summary>
/// <param name="files">This parameter's name needs to correspond to the html form's file-input name</param> /// <param name="files">This parameter's name needs to correspond to the html form's file-input name</param>
[HttpPost] [HttpPost]
@@ -85,46 +85,39 @@ namespace ZUGFeRDRESTService.Controllers
{ {
_logger.Debug("Start processing request to ValidationController"); _logger.Debug("Start processing request to ValidationController");
string oFilePath = Path.Combine(Path.GetTempPath(), file.FileName);
_logger.Debug("Saving file to temp-path [{0}]", oFilePath);
using FileStream oStream = new FileStream(oFilePath, FileMode.Create);
await file.CopyToAsync(oStream);
// TODO: Delete temp file / Do we even need the temp file?
_logger.Debug("File successfully saved!");
CrossIndustryDocumentType oDocument; CrossIndustryDocumentType oDocument;
PropertyValues.CheckPropertyValuesResult oResult = new PropertyValues.CheckPropertyValuesResult(); PropertyValues.CheckPropertyValuesResult oResult = new PropertyValues.CheckPropertyValuesResult();
try try
{ {
_logger.Debug("Extracting ZUGFeRD Data from file [{0}]", oFilePath); using Stream oStream = file.OpenReadStream();
oDocument = _zugferd.ExtractZUGFeRDFileWithGDPicture(oFilePath);
_logger.Debug("Checking ZUGFeRD Data against the database");
oResult = _props.CheckPropertyValues(oDocument, _propertyMap, "MESSAGEID");
_logger.Debug("Result of checking against the database: {0} valid properties, {1} missing properties",
oResult.ValidProperties.Count, oResult.MissingProperties.Count);
if (oResult.MissingProperties.Count > 0) {
throw new ZUGFeRDExecption(ZUGFeRDInterface.ErrorType.MissingProperties,
"Die hochgeladene Datei ist eine gültige ZUGFeRD-Rechnung, allerdings fehlen benötigte Daten.");
}
string oMessage = "Die hochgeladene Datei ist eine gültige-ZUGFeRD Rechnung";
_logger.Debug($"Replying with: [{oMessage}]");
return new ValidationResponse()
{ {
status = RESPONSE_OK, _logger.Debug("Extracting ZUGFeRD Data from file [{0}]", file.FileName);
message = oMessage
oDocument = _zugferd.ExtractZUGFeRDFileWithGDPicture(oStream);
_logger.Debug("Checking ZUGFeRD Data against the database");
oResult = _props.CheckPropertyValues(oDocument, _propertyMap, "MESSAGEID");
_logger.Debug("Result of checking against the database: {0} valid properties, {1} missing properties",
oResult.ValidProperties.Count, oResult.MissingProperties.Count);
if (oResult.MissingProperties.Count > 0)
{
throw new ZUGFeRDExecption(ZUGFeRDInterface.ErrorType.MissingProperties,
"Die hochgeladene Datei ist eine gültige ZUGFeRD-Rechnung, allerdings fehlen benötigte Daten.");
}
string oMessage = "Die hochgeladene Datei ist eine gültige-ZUGFeRD Rechnung";
_logger.Debug($"Replying with: [{oMessage}]");
return new ValidationResponse()
{
status = RESPONSE_OK,
message = oMessage
};
}; };
} }
catch (ZUGFeRDExecption ex) catch (ZUGFeRDExecption ex)
@@ -154,14 +147,15 @@ namespace ZUGFeRDRESTService.Controllers
return new ValidationResponse() return new ValidationResponse()
{ {
status = RESPONSE_ERROR, status = RESPONSE_ERROR,
message = oMessage message = oMessage,
errors = oErrors
}; };
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.Error(ex); _logger.Error(ex);
string oMessage = "Die hochgeladene Datei kann nicht validiert werden!"; string oMessage = "Die hochgeladene Datei kann nicht validiert werden, weil ein unbekannter Fehler aufgetreten ist.";
_logger.Debug($"Replying with: [{oMessage}]"); _logger.Debug($"Replying with: [{oMessage}]");