Interfaces: Rename PDFAttachments to PDFEmbeds, also throw zugferd exception when zugferd-invoice.xml could not be parsed

This commit is contained in:
Jonathan Jenne 2021-04-14 11:12:05 +02:00
parent ec986a36f2
commit 1c00cb9fc6
5 changed files with 41 additions and 26 deletions

View File

@ -167,7 +167,7 @@ Public Class Form1
End Sub
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
Dim oExtractor = New PDFAttachments(_logConfig)
Dim oExtractor = New PDFEmbeds(_logConfig)
Dim oResult = OpenFileDialog1.ShowDialog()
If oResult = DialogResult.OK Then

View File

@ -112,7 +112,7 @@
<Compile Include="ZUGFeRDInterface\CrossIndustryDocumentType.vb" />
<Compile Include="ZUGFeRDInterface.vb" />
<Compile Include="ZUGFeRDInterface\FileGroups.vb" />
<Compile Include="ZUGFeRDInterface\PDFAttachments.vb" />
<Compile Include="ZUGFeRDInterface\PDFEmbeds.vb" />
<Compile Include="ZUGFeRDInterface\PropertyValues.vb" />
<Compile Include="ZUGFeRDInterface\XmlItemProperty.vb" />
</ItemGroup>

View File

@ -122,12 +122,21 @@ Public Class ZUGFeRDInterface
End Function
Public Function ValidateZUGFeRDFileWithGDPicture(Stream As Stream) As XPathDocument
Dim oAttachmentExtractor = New PDFAttachments(_logConfig)
Dim oEmbedExtractor = New PDFEmbeds(_logConfig)
Dim oAllowedExtensions = New List(Of String) From {"xml"}
Try
Dim oResults = oAttachmentExtractor.Extract(Stream, oAllowedExtensions)
Return HandleAttachments(oResults)
Dim oFiles = oEmbedExtractor.Extract(Stream, oAllowedExtensions)
' Attachments are in this case the files that are embedded into a pdf file,
' like for example the zugferd-invoice.xml file
Return HandleEmbeddedFiles(oFiles)
Catch ex As ZUGFeRDExecption
' Don't log ZUGFeRD Exceptions here, they should be handled by the calling code.
' It also produces misleading error messages when checking if an attachment is a zugferd file.
Throw ex
Catch ex As Exception
_logger.Error(ex)
Throw ex
@ -135,23 +144,28 @@ Public Class ZUGFeRDInterface
End Function
Public Function ValidateZUGFeRDFileWithGDPicture(Path As String) As XPathDocument
Dim oAttachmentExtractor = New PDFAttachments(_logConfig)
Dim oEmbedExtractor = New PDFEmbeds(_logConfig)
Dim oAllowedExtensions = New List(Of String) From {"xml"}
Try
Dim oResults = oAttachmentExtractor.Extract(Path, oAllowedExtensions)
Return HandleAttachments(oResults)
Dim oFiles = oEmbedExtractor.Extract(Path, oAllowedExtensions)
' Attachments are in this case the files that are embedded into a pdf file,
' like for example the zugferd-invoice.xml file
Return HandleEmbeddedFiles(oFiles)
Catch ex As ZUGFeRDExecption
' Don't log ZUGFeRD Exceptions here, they should be handled by the calling code.
' It also produces misleading error messages when checking if an attachment is a zugferd file.
Throw ex
Catch ex As Exception
_logger.Error(ex)
Throw ex
End Try
End Function
Private Function HandleAttachments(Results As List(Of PDFAttachments.AttachmentResult)) As XPathDocument
Private Function HandleEmbeddedFiles(Results As List(Of PDFEmbeds.EmbeddedFile)) As XPathDocument
Dim oXmlDocument As XPathDocument
If Results Is Nothing Then
@ -163,10 +177,10 @@ Public Class ZUGFeRDInterface
End If
Dim oFound As Boolean = False
Dim oFoundResult As PDFAttachments.AttachmentResult = Nothing
Dim oFoundResult As PDFEmbeds.EmbeddedFile = Nothing
For Each oResult In Results
If oResult.FileName.ToUpper() = PDFAttachments.ZUGFERD_XML_FILENAME.ToUpper() Then
If oResult.FileName.ToUpper() = PDFEmbeds.ZUGFERD_XML_FILENAME.ToUpper() Then
oFound = True
oFoundResult = oResult
End If
@ -186,9 +200,10 @@ Public Class ZUGFeRDInterface
' Don't log ZUGFeRD Exceptions here, they should be handled by the calling code.
' It also produces misleading error messages when checking if an attachment is a zugferd file.
Throw ex
Catch ex As Exception
_logger.Error(ex)
Throw ex
Throw New ZUGFeRDExecption(ErrorType.NoValidZugferd, "Datei ist eine ungültige ZUGFeRD Datei.")
End Try
End Function

View File

@ -3,12 +3,12 @@ Imports System.IO
Imports DigitalData.Modules.Logging
Imports GdPicture14
Public Class PDFAttachments
Public Class PDFEmbeds
Private ReadOnly Logger As Logger
Public Const ZUGFERD_XML_FILENAME = "ZUGFeRD-invoice.xml"
Public Class AttachmentResult
Public Class EmbeddedFile
Public FileName As String
Public FileContents As Byte()
End Class
@ -23,8 +23,8 @@ Public Class PDFAttachments
''' </summary>
''' <param name="FilePath">Filepath of the pdf</param>
''' <param name="AllowedExtensions">List of allowed extensions to be extracted</param>
Public Function Extract(FilePath As String, AllowedExtensions As List(Of String)) As List(Of AttachmentResult)
Dim oResults As New List(Of AttachmentResult)
Public Function Extract(FilePath As String, AllowedExtensions As List(Of String)) As List(Of EmbeddedFile)
Dim oFile As New List(Of EmbeddedFile)
Dim oExtensions = AllowedExtensions.ConvertAll(New Converter(Of String, String)(Function(ext) ext.ToUpper))
Logger.Debug("Extracting embedded files from [{0}]", FilePath)
@ -32,14 +32,14 @@ Public Class PDFAttachments
Try
Using oGDPicturePDF As New GdPicturePDF()
If oGDPicturePDF.LoadFromFile(FilePath, False) = GdPictureStatus.OK Then
oResults = DoExtract(oGDPicturePDF, oExtensions)
oFile = DoExtract(oGDPicturePDF, oExtensions)
Else
Dim oMessage = String.Format("The file [{0}] can't be loaded. Status: [{1}]", FilePath, oGDPicturePDF.GetStat().ToString())
Throw New ApplicationException(oMessage)
End If
End Using
Return oResults
Return oFile
Catch ex As Exception
Logger.Warn("Unexpected Error while Extracting attachments from File [{0}]", FilePath)
Logger.Error(ex)
@ -53,8 +53,8 @@ Public Class PDFAttachments
''' </summary>
''' <param name="Stream">Filestream of the pdf</param>
''' <param name="AllowedExtensions">List of allowed extensions to be extracted</param>
Public Function Extract(Stream As Stream, AllowedExtensions As List(Of String)) As List(Of AttachmentResult)
Dim oResults As New List(Of AttachmentResult)
Public Function Extract(Stream As Stream, AllowedExtensions As List(Of String)) As List(Of EmbeddedFile)
Dim oResults As New List(Of EmbeddedFile)
Dim oExtensions = AllowedExtensions.ConvertAll(New Converter(Of String, String)(Function(ext) ext.ToUpper))
Logger.Debug("Extracting embedded files from stream")
@ -77,8 +77,8 @@ Public Class PDFAttachments
End Try
End Function
Private Function DoExtract(GDPicturePDF As GdPicturePDF, pExtensions As List(Of String)) As List(Of AttachmentResult)
Dim oResults As New List(Of AttachmentResult)
Private Function DoExtract(GDPicturePDF As GdPicturePDF, pExtensions As List(Of String)) As List(Of EmbeddedFile)
Dim oResults As New List(Of EmbeddedFile)
Dim oEmbeddedFileCount As Integer = GDPicturePDF.GetEmbeddedFileCount()
If GDPicturePDF.GetStat() = GdPictureStatus.OK Then
@ -104,7 +104,7 @@ Public Class PDFAttachments
If oStatus = GdPictureStatus.OK Then
Logger.Debug("Embedded file [{0}] extracted sucessfully!", oFileName)
oResults.Add(New AttachmentResult() With {
oResults.Add(New EmbeddedFile() With {
.FileContents = oFileData,
.FileName = oFileName
})

View File

@ -124,7 +124,7 @@ Public Class ImportZUGFeRDFiles
Public Sub Start(Arguments As Object) Implements IJob.Start
Dim oArgs As WorkerArgs = Arguments
Dim oPropertyExtractor = New PropertyValues(_logConfig)
Dim oAttachmentExtractor = New PDFAttachments(_logConfig)
Dim oAttachmentExtractor = New PDFEmbeds(_logConfig)
_logger.Debug("Starting Job {0}", [GetType].Name)
@ -172,7 +172,7 @@ Public Class ImportZUGFeRDFiles
' Create file lists
Dim oFileGroupFiles As List(Of FileInfo) = oFileGroup.Value
Dim oEmailAttachmentFiles As New List(Of FileInfo)
Dim oEmbeddedAttachmentFiles As New List(Of PDFAttachments.AttachmentResult)
Dim oEmbeddedAttachmentFiles As New List(Of PDFEmbeds.EmbeddedFile)
Dim oMessageId As String = oFileGroup.Key
Dim oMissingProperties As New List(Of String)
@ -462,7 +462,7 @@ Public Class ImportZUGFeRDFiles
MessageId As String,
Files As List(Of FileInfo),
AttachmentFiles As List(Of FileInfo),
EmbeddedAttachments As List(Of PDFAttachments.AttachmentResult),
EmbeddedAttachments As List(Of PDFEmbeds.EmbeddedFile),
MoveDirectory As String,
IsSuccess As Boolean)