From 0e592de288b1f923c02633f37730e3f35e48f871 Mon Sep 17 00:00:00 2001 From: Jonathan Jenne Date: Wed, 25 Mar 2020 13:21:34 +0100 Subject: [PATCH] Interfaces: Extract attachments from stream --- Modules.Interfaces/ZUGFeRDInterface.vb | 79 +++++++++++++++++--------- 1 file changed, 53 insertions(+), 26 deletions(-) diff --git a/Modules.Interfaces/ZUGFeRDInterface.vb b/Modules.Interfaces/ZUGFeRDInterface.vb index fbdd34aa..af058500 100644 --- a/Modules.Interfaces/ZUGFeRDInterface.vb +++ b/Modules.Interfaces/ZUGFeRDInterface.vb @@ -67,6 +67,16 @@ Public Class ZUGFeRDInterface Return SerializeZUGFeRDDocument(oXmlDocument) 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 Dim oProcessOutput, oProcessError As String Dim oXmlDocument As XPathDocument @@ -111,46 +121,63 @@ Public Class ZUGFeRDInterface Return oXmlDocument 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 Dim oAttachmentExtractor = New PDFAttachments(_logConfig) Dim oAllowedExtensions = New List(Of String) From {"xml"} - Dim oXmlDocument As XPathDocument Try 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 - Throw New ZUGFeRDExecption(ErrorType.NoZugferd, "Datei ist keine ZUGFeRD Datei.") - End If + Private Function HandleAttachments(Results As List(Of PDFAttachments.AttachmentResult)) As XPathDocument + Dim oXmlDocument As XPathDocument - If oResults.Count = 0 Then - Throw New ZUGFeRDExecption(ErrorType.NoZugferd, "Datei ist keine ZUGFeRD Datei.") - End If + If Results Is Nothing Then + Throw New ZUGFeRDExecption(ErrorType.NoZugferd, "Datei ist keine ZUGFeRD Datei.") + End If - Dim oFound As Boolean = False - Dim oFoundResult As PDFAttachments.AttachmentResult = Nothing + If Results.Count = 0 Then + Throw New ZUGFeRDExecption(ErrorType.NoZugferd, "Datei ist keine ZUGFeRD Datei.") + End If - For Each oResult In oResults - If oResult.FileName.ToUpper() = PDFAttachments.ZUGFERD_XML_FILENAME.ToUpper() Then - oFound = True - oFoundResult = oResult - End If - Next + Dim oFound As Boolean = False + Dim oFoundResult As PDFAttachments.AttachmentResult = Nothing - If Not oFound Then - Throw New ZUGFeRDExecption(ErrorType.NoZugferd, "Datei ist keine ZUGFeRD Datei.") + For Each oResult In Results + If oResult.FileName.ToUpper() = PDFAttachments.ZUGFERD_XML_FILENAME.ToUpper() Then + oFound = True + oFoundResult = oResult End If + Next - Try - Using oStream As New MemoryStream(oFoundResult.FileContents) - oXmlDocument = New XPathDocument(oStream) - End Using + 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 + Return oXmlDocument Catch ex As Exception _logger.Error(ex) Throw ex