This commit is contained in:
Developer01
2025-03-26 15:27:35 +01:00
parent 6fb82c20ee
commit 8c829d490f
26 changed files with 182 additions and 99 deletions

View File

@@ -80,7 +80,6 @@ Namespace ZUGFeRD
For Each oFile In pAttachmentFiles
Try
Dim oFilePath = _filesystem.GetVersionedFilename(Path.Combine(oAttachmentDirectory, oFile.Name))
_filesystem.MoveTo(oFile.FullName, oFilePath, oAttachmentDirectory)
_logger.Info("Attachment moved to {0}", oFilePath)
Catch ex As Exception
@@ -98,11 +97,22 @@ Namespace ZUGFeRD
If Not File.Exists(oAttachmentDirectory) Then
Directory.CreateDirectory(oAttachmentDirectory)
End If
System.IO.File.WriteAllBytes(oFilePath, oResult.FileContents)
If CheckBytes(oFilePath, oResult.FileContents) = True Then
If FileOpenwithError(oFilePath) Then
_logger.Info("Embedded Attachment moved to {0}", oFilePath)
Else
_logger.Info("File is corrupt. Deleting the created file ...")
File.Delete(oFilePath)
End If
Else
_logger.Info("File is corrupt (CheckBytes). Deleting the created file ...")
File.Delete(oFilePath)
End If
'Using oWriter As New FileStream(oFilePath, FileMode.Create)
' oWriter.Write(oResult.FileContents, 0, oResult.FileContents.Length)
Using oWriter As New FileStream(oFilePath, FileMode.Create)
oWriter.Write(oResult.FileContents, 0, oResult.FileContents.Length)
_logger.Info("Embedded Attachment moved to {0}", oFilePath)
End Using
'End Using
Catch ex As Exception
_logger.Warn("Could not save embedded attachment {0}", oResult.FileName)
_logger.Error(ex)
@@ -111,7 +121,15 @@ Namespace ZUGFeRD
_logger.Info("Finished moving files")
End Sub
Private Function CheckBytes(oFilePath As String, oFileContents As IEnumerable(Of Byte)) As Boolean
Dim savedBytes() As Byte = System.IO.File.ReadAllBytes(oFilePath)
If savedBytes.SequenceEqual(oFileContents) Then
Return True
Else
Return False
End If
End Function
Public Function MoveAndRenameEmailToRejected(pArgs As WorkerArgs, pMessageId As String) As EmailData
_logger.Info("Moving Mail with MessageId [{0}] to Rejected folder", pMessageId)
_logger.Debug("Fetching Email Data")
@@ -177,7 +195,19 @@ Namespace ZUGFeRD
Return oEmailData
End Function
Private Function FileOpenwithError(pFilePath As String) As Boolean
Try
Using fs As FileStream = File.Open(pFilePath, FileMode.Open, FileAccess.Read, FileShare.None)
Return True ' Datei kann geöffnet werden
End Using
Catch ex As IOException
_logger.Info("file [{0}] could not be opened! Error IOException: [{1}]", pFilePath, ex.Message)
Return False ' Datei ist gesperrt oder existiert nicht
Catch ex As Exception
_logger.Info("file [{0}] could not be opened! Error: [{1}]", pFilePath, ex.Message)
Return False ' Andere Fehler
End Try
End Function
Public Function GetEmailPathWithSubjectAsName(RejectedEmailDirectory As String, UncleanedSubject As String) As String
Dim oCleanSubject = String.Join("", UncleanedSubject.Split(Path.GetInvalidPathChars()))
Dim oAttachmentDirectory = RejectedEmailDirectory

View File

@@ -131,7 +131,7 @@ Public Class ImportZUGFeRDFiles
' Group files by messageId
Dim oGrouped As Dictionary(Of String, List(Of FileInfo)) = _zugferd.FileGroup.GroupFiles(oFiles)
_logger.Info("Found {0} file groups", oGrouped.Count)
_logger.Info("Found [{0}] file groups", oGrouped.Count)
'Process each file group together
'oGrouped equals one e-invoice