ZUGFeRDService: Add config option MaxAttachmentSizeInMegaBytes

This commit is contained in:
Jonathan Jenne 2021-07-13 13:38:54 +02:00
parent b81d6a1314
commit d8c534c3e3
7 changed files with 70 additions and 15 deletions

View File

@ -86,7 +86,10 @@ Public Class Form1
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim args As New WorkerArgs()
Dim args As New WorkerArgs() With {
.MaxAttachmentSizeInMegaBytes = 10,
.IgnoreRejectionStatus = False
}
args = LoadFolderConfig(args)
args = LoadPropertyMapFor(args, "DEFAULT")
args.InsertIntoSQLServer = True

View File

@ -23,6 +23,8 @@
Public Const EMAIL_NO_FERDS = "<p>Ihre Email enthielt keine ZUGFeRD-Dokumente.</p>"
Public Const EMAIL_FILE_SIZE_REACHED = "<p>Ihre Email enthielt Dateien, die die erlaubte Größe von {0}MB überschreiten.</p>"
Public Const EMAIL_INVALID_DOCUMENT = """
<p>Ihre Email enthielt ein ZUGFeRD Dokument, welches aber inkorrekt formatiert wurde.</p>
<p>Mögliche Gründe für ein inkorrektes Format:<ul>

View File

@ -206,11 +206,24 @@ Public Class ImportZUGFeRDFiles
If Not oFile.Name.EndsWith(".pdf") Then
_logger.Debug("Skipping non-pdf file {0}", oFile.Name)
oEmailAttachmentFiles.Add(oFile)
' Checking filesize for attachment files
If Check_FileSize(oFile, oArgs.MaxAttachmentSizeInMegaBytes) = False Then
_logger.Warn("Filesize for File [{0}] exceeded limit of {1} MB", oFile.Name, oArgs.MaxAttachmentSizeInMegaBytes)
Throw New FileSizeLimitReachedException(oFile.Name, oArgs.MaxAttachmentSizeInMegaBytes)
End If
Continue For
End If
_logger.Info("Start processing file {0}", oFile.Name)
' Checking filesize for pdf files
If Check_FileSize(oFile, oArgs.MaxAttachmentSizeInMegaBytes) = False Then
_logger.Warn("Filesize for File [{0}] exceeded limit of {1} MB", oFile.Name, oArgs.MaxAttachmentSizeInMegaBytes)
Throw New FileSizeLimitReachedException(oFile.Name, oArgs.MaxAttachmentSizeInMegaBytes)
End If
Try
oDocument = _zugferd.ExtractZUGFeRDFileWithGDPicture(oFile.FullName)
Catch ex As ZUGFeRDExecption
@ -241,7 +254,7 @@ Public Class ImportZUGFeRDFiles
End If
' Check the Checksum and rejection status
oMD5CheckSum = Check_MD5Sum(oFile.FullName, oArgs.IgnoreRejectionStatus)
oMD5CheckSum = GenerateAndCheck_MD5Sum(oFile.FullName, oArgs.IgnoreRejectionStatus)
' Check if there are more than one ZUGFeRD files
If oZUGFeRDCount = 1 Then
@ -334,8 +347,6 @@ Public Class ImportZUGFeRDFiles
Catch ex As MD5HashException
_logger.Error(ex)
'Dim oSQL = $"UPDATE TBEDM_ZUGFERD_HISTORY_IN SET COMMENT = '{oMessage}' WHERE MESSAGE_ID = '{oMessageId}'"
'_firebird.ExecuteNonQuery(oSQL, oFBTransaction)
Dim oMessage = "REJECTED - Already processed (MD5Hash)"
Update_HistoryEntry(oMessageId, oMD5CheckSum, oMessage, oFBTransaction)
@ -347,9 +358,6 @@ Public Class ImportZUGFeRDFiles
Catch ex As InvalidFerdException
_logger.Error(ex)
'Dim oSQL = $"UPDATE TBEDM_ZUGFERD_HISTORY_IN SET COMMENT = 'REJECTED - ZUGFeRD yes but incorrect format' WHERE GUID = '{HISTORY_ID}'"
'_firebird.ExecuteNonQuery(oSQL, oFBTransaction)
' When InvalidFerdException is thrown, we don't have a MD5Hash yet.
' That 's why we set it to String.Empty here.
Create_HistoryEntry(oMessageId, String.Empty, "REJECTED - ZUGFeRD yes but incorrect format", oFBTransaction)
@ -362,8 +370,6 @@ Public Class ImportZUGFeRDFiles
Catch ex As TooMuchFerdsException
_logger.Error(ex)
'Dim oSQL = $"UPDATE TBEDM_ZUGFERD_HISTORY_IN SET COMMENT = 'REJECTED - More than one ZUGFeRD-document in email' WHERE GUID = '{HISTORY_ID}'"
'_firebird.ExecuteNonQuery(oSQL, oFBTransaction)
Create_HistoryEntry(oMessageId, oMD5CheckSum, "REJECTED - More than one ZUGFeRD-document in email", oFBTransaction)
Dim oBody = EmailStrings.EMAIL_TOO_MUCH_FERDS
@ -374,8 +380,6 @@ Public Class ImportZUGFeRDFiles
Catch ex As NoFerdsException
_logger.Error(ex)
'Dim oSQL = $"UPDATE TBEDM_ZUGFERD_HISTORY_IN SET COMMENT = 'REJECTED - no ZUGFeRD-Document in email' WHERE GUID = '{HISTORY_ID}'"
'_firebird.ExecuteNonQuery(oSQL, oFBTransaction)
Create_HistoryEntry(oMessageId, oMD5CheckSum, "REJECTED - no ZUGFeRD-Document in email", oFBTransaction)
Dim oBody = EmailStrings.EMAIL_NO_FERDS
@ -390,8 +394,7 @@ Public Class ImportZUGFeRDFiles
For Each prop In oMissingProperties
oMessage &= $"- {prop}"
Next
'Dim oSQL = $"UPDATE TBEDM_ZUGFERD_HISTORY_IN SET COMMENT = 'REJECTED - Missing Required Properties: [{oMessage}]' WHERE GUID = '{HISTORY_ID}'"
'_firebird.ExecuteNonQuery(oSQL, oFBTransaction)
Create_HistoryEntry(oMessageId, oMD5CheckSum, $"REJECTED - Missing Required Properties: [{oMessage}]", oFBTransaction)
Dim oBody = CreateBodyForMissingProperties(ex.File.Name, oMissingProperties)
@ -399,6 +402,17 @@ Public Class ImportZUGFeRDFiles
_email.AddToEmailQueueMSSQL(oMessageId, oBody, oEmailData, "MissingValueException", _EmailOutAccountId)
AddRejectedState(oMessageId, "MissingValueException", "Es fehlten ZugferdSpezifikationen", oMessage, oSQLTransaction)
Catch ex As FileSizeLimitReachedException
_logger.Error(ex)
Create_HistoryEntry(oMessageId, oMD5CheckSum, "REJECTED - File size limit reached", oFBTransaction)
Dim oBody = String.Format(EmailStrings.EMAIL_FILE_SIZE_REACHED, oArgs.MaxAttachmentSizeInMegaBytes)
Dim oEmailData = MoveAndRenameEmailToRejected(oArgs, oMessageId)
_email.AddToEmailQueueMSSQL(oMessageId, oBody, oEmailData, "FileSizeLimitReachedException", _EmailOutAccountId)
AddRejectedState(oMessageId, "FileSizeLimitReachedException", "Erlaubte Dateigröße überschritten", "", oSQLTransaction)
Catch ex As OutOfMemoryException
_logger.Warn("OutOfMemory Error occurred: {0}", ex.Message)
_logger.Error(ex)
@ -678,7 +692,7 @@ Public Class ImportZUGFeRDFiles
''' <param name="pIgnoreRejectionStatus">Should the check take into account the rejection status of the file?</param>
''' <returns>The MD5 Checksum of the file, or an empty string, if the Checksum could not be created</returns>
''' <exception cref="MD5HashException">Throws, when the file should be rejected, ie. if it already exists in the table</exception>
Private Function Check_MD5Sum(pFilePath As String, pIgnoreRejectionStatus As Boolean) As String
Private Function GenerateAndCheck_MD5Sum(pFilePath As String, pIgnoreRejectionStatus As Boolean) As String
Dim oMD5CheckSum = CreateMD5(pFilePath)
' Exit if MD5 could not be created
@ -740,4 +754,23 @@ Public Class ImportZUGFeRDFiles
Return oMD5CheckSum
End Function
''' <summary>
''' Checks the size of the supplied file.
''' </summary>
''' <param name="pFileInfo"></param>
''' <param name="pMaxFileSizeInMegaBytes"></param>
''' <returns></returns>
Private Function Check_FileSize(pFileInfo As FileInfo, pMaxFileSizeInMegaBytes As Integer) As Boolean
If pMaxFileSizeInMegaBytes <= 0 Then
Return True
End If
Dim oMaxSize = pMaxFileSizeInMegaBytes * 1024 * 1024
If oMaxSize > 0 And pFileInfo.Length > oMaxSize Then
Return False
Else
Return True
End If
End Function
End Class

View File

@ -13,6 +13,7 @@ Public Class WorkerArgs
Public ExceptionEmailAddress As String
Public IgnoreRejectionStatus As Boolean
Public MaxAttachmentSizeInMegaBytes As Integer
Public Sub New()
WatchDirectories = New List(Of String)
@ -26,5 +27,6 @@ Public Class WorkerArgs
ExceptionEmailAddress = Nothing
IgnoreRejectionStatus = False
MaxAttachmentSizeInMegaBytes = -1
End Sub
End Class

View File

@ -21,6 +21,14 @@ Public Class Exceptions
End Sub
End Class
Public Class FileSizeLimitReachedException
Inherits ApplicationException
Public Sub New(pFilePath As String, pFileSizeLimitInMegaBytes As Integer)
MyBase.New($"At least one file exceeded the filesize limit of {pFileSizeLimitInMegaBytes}MB: {pFilePath}")
End Sub
End Class
Public Class InvalidFerdException
Inherits ApplicationException

View File

@ -21,6 +21,12 @@
''' regardless of the REJECTED status.
''' </summary>
Public Property IgnoreRejectionStatus As Boolean = False
''' <summary>
''' Maximum Size for attachment files in MB. If This is set to -1,
''' there's no size limit.
''' </summary>
Public Property MaxAttachmentSizeInMegaBytes As Integer = -1
End Class
Public Class FirebirdConfig

View File

@ -51,7 +51,8 @@ Public Class ThreadRunner
Dim oArgs As New WorkerArgs With {
.ExceptionEmailAddress = _config.Config.ExceptionEmailAddress,
.IgnoreRejectionStatus = _config.Config.Custom.IgnoreRejectionStatus
.IgnoreRejectionStatus = _config.Config.Custom.IgnoreRejectionStatus,
.MaxAttachmentSizeInMegaBytes = _config.Config.Custom.MaxAttachmentSizeInMegaBytes
}
oArgs = LoadFolderConfig(oArgs)
oArgs = LoadPropertyMapFor(oArgs, "DEFAULT")