[ZUGFeRD] fix versioning of files

This commit is contained in:
Jonathan Jenne
2019-10-07 12:46:31 +02:00
parent 42c06273c5
commit 9a1b716e92
2 changed files with 76 additions and 36 deletions

View File

@@ -6,6 +6,7 @@ Imports System.Reflection
Imports System.Security.Cryptography
Imports System.Text.RegularExpressions
Imports System.Xml
Imports DigitalData.Modules.Filesystem
Imports DigitalData.Modules.Database
Imports DigitalData.Modules.Interfaces
Imports DigitalData.Modules.Interfaces.Exceptions
@@ -77,7 +78,7 @@ Public Class ImportZUGFeRDFiles
Dim oAttachmentFile = MessageId & ".eml"
Dim oAttachmentPath = Path.Combine(oAttachmentDirectory, oAttachmentFile)
If File.Exists(oAttachmentPath) Then
If IO.File.Exists(oAttachmentPath) Then
Return oAttachmentPath
Else
Return String.Empty
@@ -97,27 +98,11 @@ Public Class ImportZUGFeRDFiles
Dim oEmailData = GetEmailDataForMessageId(MessageId)
Dim oSource = GetOriginalEmailPath(Args.OriginalEmailDirectory, MessageId)
Dim oDestination = GetEmailPathWithSubjectAsName(Args.RejectedEmailDirectory, oEmailData.Subject)
Dim oVersion As Integer = 0
Dim oFileName As String = oDestination
Do While File.Exists(oFileName)
If oVersion > 29 Then
Throw New ApplicationException("Max. Move-Retries of 30 exceeded! Move will be aborted!")
End If
oVersion += 1
Dim oDestinationDir = Path.GetDirectoryName(oDestination)
Dim oExtension = Path.GetExtension(oFileName)
Dim oRootName = Path.GetFileNameWithoutExtension(oFileName)
Dim oNewName As String = oRootName & "~" & oVersion & oExtension
oFileName = Path.Combine(oDestinationDir, oNewName)
Loop
Dim oFinalFileName = _filesystem.GetVersionedFilename(oDestination)
Try
File.Move(oSource, oFileName) 'oDestination)
oEmailData.Attachment = oFileName 'oDestination
IO.File.Move(oSource, oFinalFileName)
oEmailData.Attachment = oFinalFileName
Catch ex As Exception
_logger.Warn("File {0} could not be moved! Original Filename will be used!", oSource)
_logger.Error(ex)
@@ -630,27 +615,12 @@ Public Class ImportZUGFeRDFiles
End If
End If
Dim oVersion As Integer = 0
Dim oFileName As String = Path.Combine(oFinalMoveDirectory, oFile.Name)
Do While File.Exists(oFileName)
If oVersion > 29 Then
Throw New ApplicationException("Max. Move-Retries of 30 exceeded! Move will be aborted!")
End If
oVersion += 1
Dim oExtension = Path.GetExtension(oFileName)
Dim oRootName = Path.GetFileNameWithoutExtension(oFile.Name)
Dim oNewName As String = oRootName & "~" & oVersion & oExtension
oFileName = Path.Combine(oFinalMoveDirectory, oNewName)
Loop
Dim oFileName = _filesystem.GetVersionedFilename(Path.Combine(oFinalMoveDirectory, oFile.Name))
_filesystem.MoveTo(oFile.FullName, oFileName, oFinalMoveDirectory)
_logger.Info("Finished processing file {0}", oFile.Name)
_logger.Info("File moved to {0}", oFileName)
Catch ex As Exception
_logger.Warn("Could not move file {0}", oFile.FullName)
_logger.Error(ex)