diff --git a/GUIs.Test.ZUGFeRDTest/Form1.vb b/GUIs.Test.ZUGFeRDTest/Form1.vb
index 3665395b..dab5e055 100644
--- a/GUIs.Test.ZUGFeRDTest/Form1.vb
+++ b/GUIs.Test.ZUGFeRDTest/Form1.vb
@@ -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
diff --git a/Modules.Jobs/EDMI/ZUGFeRD/EmailStrings.vb b/Modules.Jobs/EDMI/ZUGFeRD/EmailStrings.vb
index 2b94fb59..d68c3beb 100644
--- a/Modules.Jobs/EDMI/ZUGFeRD/EmailStrings.vb
+++ b/Modules.Jobs/EDMI/ZUGFeRD/EmailStrings.vb
@@ -23,6 +23,8 @@
Public Const EMAIL_NO_FERDS = "
Ihre Email enthielt keine ZUGFeRD-Dokumente.
"
+ Public Const EMAIL_FILE_SIZE_REACHED = "Ihre Email enthielt Dateien, die die erlaubte Größe von {0}MB überschreiten.
"
+
Public Const EMAIL_INVALID_DOCUMENT = """
Ihre Email enthielt ein ZUGFeRD Dokument, welches aber inkorrekt formatiert wurde.
Mögliche Gründe für ein inkorrektes Format:
diff --git a/Modules.Jobs/EDMI/ZUGFeRD/ImportZUGFeRDFiles.vb b/Modules.Jobs/EDMI/ZUGFeRD/ImportZUGFeRDFiles.vb
index 328090ac..b8797d48 100644
--- a/Modules.Jobs/EDMI/ZUGFeRD/ImportZUGFeRDFiles.vb
+++ b/Modules.Jobs/EDMI/ZUGFeRD/ImportZUGFeRDFiles.vb
@@ -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
''' Should the check take into account the rejection status of the file?
''' The MD5 Checksum of the file, or an empty string, if the Checksum could not be created
''' Throws, when the file should be rejected, ie. if it already exists in the table
- 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
+
+ '''
+ ''' Checks the size of the supplied file.
+ '''
+ '''
+ '''
+ '''
+ 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
diff --git a/Modules.Jobs/EDMI/ZUGFeRD/WorkerArgs.vb b/Modules.Jobs/EDMI/ZUGFeRD/WorkerArgs.vb
index 32db905e..fc3b7572 100644
--- a/Modules.Jobs/EDMI/ZUGFeRD/WorkerArgs.vb
+++ b/Modules.Jobs/EDMI/ZUGFeRD/WorkerArgs.vb
@@ -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
\ No newline at end of file
diff --git a/Modules.Jobs/Exceptions.vb b/Modules.Jobs/Exceptions.vb
index 894ed27e..a649fe2a 100644
--- a/Modules.Jobs/Exceptions.vb
+++ b/Modules.Jobs/Exceptions.vb
@@ -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
diff --git a/Services.ZUGFeRDService/Config.vb b/Services.ZUGFeRDService/Config.vb
index f0359b1a..3ced11da 100644
--- a/Services.ZUGFeRDService/Config.vb
+++ b/Services.ZUGFeRDService/Config.vb
@@ -21,6 +21,12 @@
''' regardless of the REJECTED status.
'''
Public Property IgnoreRejectionStatus As Boolean = False
+
+ '''
+ ''' Maximum Size for attachment files in MB. If This is set to -1,
+ ''' there's no size limit.
+ '''
+ Public Property MaxAttachmentSizeInMegaBytes As Integer = -1
End Class
Public Class FirebirdConfig
diff --git a/Services.ZUGFeRDService/ThreadRunner.vb b/Services.ZUGFeRDService/ThreadRunner.vb
index 8df1a938..79ff2077 100644
--- a/Services.ZUGFeRDService/ThreadRunner.vb
+++ b/Services.ZUGFeRDService/ThreadRunner.vb
@@ -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")