more logging

This commit is contained in:
Jonathan Jenne 2020-03-24 10:57:07 +01:00
parent c9fdf80936
commit 52149cbeb8
2 changed files with 114 additions and 55 deletions

View File

@ -21,42 +21,99 @@ Public Class PDFAttachments
''' Extracts all embedded files from a PDF file. ''' Extracts all embedded files from a PDF file.
''' Note: This does NOT filter out `ZUGFeRD-invoice.xml` anymore to allow for a more generic use. ''' Note: This does NOT filter out `ZUGFeRD-invoice.xml` anymore to allow for a more generic use.
''' </summary> ''' </summary>
''' <param name="FileName"></param> ''' <param name="FilePath">Filepath of the pdf</param>
''' <param name="AllowedExtensions"></param> ''' <param name="AllowedExtensions">List of allowed extensions to be extracted</param>
''' <returns></returns> Public Function Extract(FilePath As String, AllowedExtensions As List(Of String)) As List(Of AttachmentResult)
Public Function Extract(FileName As String, AllowedExtensions As List(Of String)) As List(Of AttachmentResult)
Dim oResults As New List(Of AttachmentResult) Dim oResults As New List(Of AttachmentResult)
Dim oExtensions = AllowedExtensions.ConvertAll(New Converter(Of String, String)(Function(ext) ext.ToUpper)) Dim oExtensions = AllowedExtensions.ConvertAll(New Converter(Of String, String)(Function(ext) ext.ToUpper))
Logger.Debug("Extracting embedded files from [{0}]", FilePath)
Try Try
Using oGDPicturePDF As New GdPicturePDF() Using oGDPicturePDF As New GdPicturePDF()
If oGDPicturePDF.LoadFromFile(FileName, False) = GdPictureStatus.OK Then If oGDPicturePDF.LoadFromFile(FilePath, False) = GdPictureStatus.OK Then
Dim oEmbeddedFileCount As Integer = oGDPicturePDF.GetEmbeddedFileCount() oResults = DoExtract(oGDPicturePDF, oExtensions)
If oGDPicturePDF.GetStat() = GdPictureStatus.OK Then 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
Catch ex As Exception
Logger.Warn("Unexpected Error while Extracting attachments from File [{0}]", FilePath)
Logger.Error(ex)
Return Nothing
End Try
End Function
''' <summary>
''' Extracts all embedded files from a PDF file.
''' Note: This does NOT filter out `ZUGFeRD-invoice.xml` anymore to allow for a more generic use.
''' </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)
Dim oExtensions = AllowedExtensions.ConvertAll(New Converter(Of String, String)(Function(ext) ext.ToUpper))
Logger.Debug("Extracting embedded files from stream")
Try
Using oGDPicturePDF As New GdPicturePDF()
If oGDPicturePDF.LoadFromStream(Stream, False) = GdPictureStatus.OK Then
oResults = DoExtract(oGDPicturePDF, oExtensions)
Else
Dim oMessage = String.Format("The filestream can't be loaded. Status: [{0}]", oGDPicturePDF.GetStat().ToString())
Throw New ApplicationException(oMessage)
End If
End Using
Return oResults
Catch ex As Exception
Logger.Warn("Unexpected Error while Extracting attachments from Filestream")
Logger.Error(ex)
Return Nothing
End Try
End Function
Private Function DoExtract(GDPicturePDF As GdPicturePDF, Extensions As List(Of String)) As List(Of AttachmentResult)
Dim oResults As New List(Of AttachmentResult)
Dim oEmbeddedFileCount As Integer = GDPicturePDF.GetEmbeddedFileCount()
If GDPicturePDF.GetStat() = GdPictureStatus.OK Then
Logger.Debug("Embedded file count is: [{0}]", oEmbeddedFileCount)
If oEmbeddedFileCount > 0 Then If oEmbeddedFileCount > 0 Then
For index = 0 To oEmbeddedFileCount - 1 For oIndex = 0 To oEmbeddedFileCount - 1
Dim oFileName As String = oGDPicturePDF.GetEmbeddedFileName(index) Dim oFileName As String = GDPicturePDF.GetEmbeddedFileName(oIndex)
If GDPicturePDF.GetStat() = GdPictureStatus.OK Then
Logger.Debug("Extracting embedded file [{0}]", oFileName)
If oGDPicturePDF.GetStat() = GdPictureStatus.OK Then
Dim oExtension = New FileInfo(oFileName).Extension.ToUpper.Substring(1) Dim oExtension = New FileInfo(oFileName).Extension.ToUpper.Substring(1)
If oExtensions.Contains(oExtension) Then If Extensions.Contains(oExtension) Then
Dim FileSize As Integer = oGDPicturePDF.GetEmbeddedFileSize(index) Dim oFileSize As Integer = GDPicturePDF.GetEmbeddedFileSize(oIndex)
If oGDPicturePDF.GetStat() = GdPictureStatus.OK Then If GDPicturePDF.GetStat() = GdPictureStatus.OK Then
Dim oFileData As Byte() = New Byte(FileSize) {} Logger.Debug("Filesize of embedded file is [{0}]", oFileSize)
Dim status As GdPictureStatus = oGDPicturePDF.ExtractEmbeddedFile(index, oFileData)
Dim oFileData As Byte() = New Byte(oFileSize) {}
Dim oStatus As GdPictureStatus = GDPicturePDF.ExtractEmbeddedFile(oIndex, oFileData)
If oStatus = GdPictureStatus.OK Then
Logger.Debug("Embedded file [{0}] extracted sucessfully!", oFileName)
If status = GdPictureStatus.OK Then
oResults.Add(New AttachmentResult() With { oResults.Add(New AttachmentResult() With {
.FileContents = oFileData, .FileContents = oFileData,
.FileName = oFileName .FileName = oFileName
}) })
Else Else
Logger.Error("The embedded file [{0}] has failed to extract. Status: {1}", oFileName, oGDPicturePDF.GetStat().ToString()) Logger.Error("The embedded file [{0}] has failed to extract. Status: {1}", oFileName, GDPicturePDF.GetStat().ToString())
Continue For Continue For
End If End If
Else Else
Logger.Error("An error occurred getting the file size for [{0}]. Status: {1}", oFileName, oGDPicturePDF.GetStat().ToString()) Logger.Error("An error occurred getting the file size for [{0}]. Status: {1}", oFileName, GDPicturePDF.GetStat().ToString())
Continue For Continue For
End If End If
Else Else
@ -64,26 +121,16 @@ Public Class PDFAttachments
Continue For Continue For
End If End If
Else Else
Logger.Error("An error occurred getting the file name for [{0}]. Status: {1}", oFileName, oGDPicturePDF.GetStat().ToString()) Logger.Error("An error occurred getting the file name for [{0}]. Status: {1}", oFileName, GDPicturePDF.GetStat().ToString())
Continue For Continue For
End If End If
Next Next
End If End If
Else
Dim oMessage = String.Format("An error occurred getting the number of embedded files. Status: {0}", oGDPicturePDF.GetStat().ToString())
Throw New ApplicationException(oMessage)
End If
Else
Dim oMessage = String.Format("The file [{0}] can't be loaded. Status: [{1}]", FileName, oGDPicturePDF.GetStat().ToString())
Throw New ApplicationException(oMessage)
End If
End Using
Return oResults Return oResults
Catch ex As Exception Else
Logger.Warn("Unexpected Error while Extracting attachments from File [{0}]", FileName) Dim oMessage = String.Format("An error occurred getting the number of embedded files. Status: {0}", GDPicturePDF.GetStat().ToString())
Logger.Error(ex) Throw New ApplicationException(oMessage)
Return Nothing End If
End Try
End Function End Function
End Class End Class

View File

@ -38,7 +38,7 @@ namespace ZUGFeRDRESTService.Controllers
public ValidationResponse() public ValidationResponse()
{ {
status = RESPONSE_OK; status = RESPONSE_OK;
message = String.Empty; message = string.Empty;
errors = new List<string>(); errors = new List<string>();
} }
@ -109,17 +109,22 @@ namespace ZUGFeRDRESTService.Controllers
oResult = _props.CheckPropertyValues(oDocument, _propertyMap, "MESSAGEID"); oResult = _props.CheckPropertyValues(oDocument, _propertyMap, "MESSAGEID");
_logger.Debug("Result of checking against the database: {0} valid properties, {1} missing properties", oResult.ValidProperties, oResult.MissingProperties); _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) { if (oResult.MissingProperties.Count > 0) {
throw new ZUGFeRDExecption(ZUGFeRDInterface.ErrorType.MissingProperties, throw new ZUGFeRDExecption(ZUGFeRDInterface.ErrorType.MissingProperties,
"Die hochgeladene Datei ist eine gültige ZUGFeRD-Rechnung, allerdings fehlen benötigte Daten."); "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() return new ValidationResponse()
{ {
status = RESPONSE_OK, status = RESPONSE_OK,
message = "Die hochgeladene Datei ist eine gültige-ZUGFeRD Rechnung" message = oMessage
}; };
} }
catch (ZUGFeRDExecption ex) catch (ZUGFeRDExecption ex)
@ -144,6 +149,8 @@ namespace ZUGFeRDRESTService.Controllers
_ => new List<string>() _ => new List<string>()
}; };
_logger.Debug($"Replying with: [{oMessage}]");
return new ValidationResponse() return new ValidationResponse()
{ {
status = RESPONSE_ERROR, status = RESPONSE_ERROR,
@ -153,10 +160,15 @@ namespace ZUGFeRDRESTService.Controllers
catch (Exception ex) catch (Exception ex)
{ {
_logger.Error(ex); _logger.Error(ex);
string oMessage = "Die hochgeladene Datei kann nicht validiert werden!";
_logger.Debug($"Replying with: [{oMessage}]");
return new ValidationResponse() return new ValidationResponse()
{ {
status = RESPONSE_ERROR, status = RESPONSE_ERROR,
message = "Die hochgeladene Datei kann nicht validiert werden!" message = oMessage
}; };
} }
} }