[ZUGFeRD] fix versioning of files
This commit is contained in:
parent
42c06273c5
commit
9a1b716e92
@ -31,6 +31,76 @@ Public Class File
|
|||||||
_logger = LogConfig.GetLogger()
|
_logger = LogConfig.GetLogger()
|
||||||
End Sub
|
End Sub
|
||||||
|
|
||||||
|
''' <summary>
|
||||||
|
''' Adds fileversions to given filename `Destination` if that file already exists.
|
||||||
|
''' </summary>
|
||||||
|
''' <param name="Destination"></param>
|
||||||
|
''' <returns></returns>
|
||||||
|
Public Function GetVersionedFilename(Destination As String) As String
|
||||||
|
Try
|
||||||
|
Dim oFileName As String = Destination
|
||||||
|
Dim oFinalFileName = oFileName
|
||||||
|
|
||||||
|
Dim oDestinationDir = Path.GetDirectoryName(oFileName)
|
||||||
|
Dim oExtension = Path.GetExtension(oFileName)
|
||||||
|
|
||||||
|
Dim oVersionSeparator As Char = "~"c
|
||||||
|
Dim oFileVersion As Integer = Nothing
|
||||||
|
|
||||||
|
' Split Filename without extension at version separator to:
|
||||||
|
' - Check if file is already versioned
|
||||||
|
' - Get the file version of an already versioned file
|
||||||
|
'
|
||||||
|
' Example:
|
||||||
|
' test1.pdf --> test1 --> ['test1'] --> no fileversion
|
||||||
|
' test1~2.pdf --> test1~2 --> ['test1', '2'] --> version 2
|
||||||
|
' test1~12345~2.pdf --> test1~12345~2 --> ['test1', '12345', '2'] --> still version 2
|
||||||
|
Dim oFileNameWithoutExtension = Path.GetFileNameWithoutExtension(oFileName)
|
||||||
|
Dim oSplitFilename = oFileNameWithoutExtension.Split(oVersionSeparator).ToList()
|
||||||
|
|
||||||
|
' if file is already versioned, extract file version
|
||||||
|
' else just use the filename and set version to 1
|
||||||
|
If oSplitFilename.Count > 1 Then
|
||||||
|
Dim oVersion As Integer = 1
|
||||||
|
Try
|
||||||
|
oVersion = Integer.Parse(oSplitFilename.Last())
|
||||||
|
Catch ex As Exception
|
||||||
|
' oFilenameWithoutExtension does NOT change
|
||||||
|
oFileNameWithoutExtension = oFileNameWithoutExtension
|
||||||
|
Finally
|
||||||
|
oFileVersion = oVersion
|
||||||
|
End Try
|
||||||
|
Else
|
||||||
|
oFileVersion = 1
|
||||||
|
oFileNameWithoutExtension = String.Join(String.Empty, oSplitFilename.Take(oSplitFilename.Count - 1))
|
||||||
|
End If
|
||||||
|
|
||||||
|
' while file exists, increment version
|
||||||
|
Do
|
||||||
|
oFinalFileName = Path.Combine(oDestinationDir, GetFilenameWithVersion(oFileNameWithoutExtension, oVersionSeparator, oFileVersion, oExtension))
|
||||||
|
_logger.Debug("Intermediate Filename is {0}", oFinalFileName)
|
||||||
|
_logger.Debug("File version: {0}", oFileVersion)
|
||||||
|
oFileVersion += 1
|
||||||
|
Loop While (IO.File.Exists(oFinalFileName))
|
||||||
|
|
||||||
|
_logger.Debug("Final Filename is {0}", oFinalFileName)
|
||||||
|
|
||||||
|
Return oFinalFileName
|
||||||
|
Catch ex As Exception
|
||||||
|
_logger.Warn("Filename {0} could not be versioned. Original filename will be returned!", Destination)
|
||||||
|
_logger.Error(ex)
|
||||||
|
Return Destination
|
||||||
|
End Try
|
||||||
|
End Function
|
||||||
|
|
||||||
|
Private Function GetFilenameWithVersion(FileNameWithoutExtension As String, VersionSeparator As Char, FileVersion As Integer, Extension As String) As String
|
||||||
|
If FileVersion <= 1 Then
|
||||||
|
Return $"{FileNameWithoutExtension}{Extension}"
|
||||||
|
Else
|
||||||
|
Return $"{FileNameWithoutExtension}{VersionSeparator}{FileVersion}{Extension}"
|
||||||
|
End If
|
||||||
|
End Function
|
||||||
|
|
||||||
''' <summary>
|
''' <summary>
|
||||||
''' Removes files in a directory filtered by filename, extension and last write date
|
''' Removes files in a directory filtered by filename, extension and last write date
|
||||||
''' </summary>
|
''' </summary>
|
||||||
|
|||||||
@ -6,6 +6,7 @@ Imports System.Reflection
|
|||||||
Imports System.Security.Cryptography
|
Imports System.Security.Cryptography
|
||||||
Imports System.Text.RegularExpressions
|
Imports System.Text.RegularExpressions
|
||||||
Imports System.Xml
|
Imports System.Xml
|
||||||
|
Imports DigitalData.Modules.Filesystem
|
||||||
Imports DigitalData.Modules.Database
|
Imports DigitalData.Modules.Database
|
||||||
Imports DigitalData.Modules.Interfaces
|
Imports DigitalData.Modules.Interfaces
|
||||||
Imports DigitalData.Modules.Interfaces.Exceptions
|
Imports DigitalData.Modules.Interfaces.Exceptions
|
||||||
@ -77,7 +78,7 @@ Public Class ImportZUGFeRDFiles
|
|||||||
Dim oAttachmentFile = MessageId & ".eml"
|
Dim oAttachmentFile = MessageId & ".eml"
|
||||||
Dim oAttachmentPath = Path.Combine(oAttachmentDirectory, oAttachmentFile)
|
Dim oAttachmentPath = Path.Combine(oAttachmentDirectory, oAttachmentFile)
|
||||||
|
|
||||||
If File.Exists(oAttachmentPath) Then
|
If IO.File.Exists(oAttachmentPath) Then
|
||||||
Return oAttachmentPath
|
Return oAttachmentPath
|
||||||
Else
|
Else
|
||||||
Return String.Empty
|
Return String.Empty
|
||||||
@ -97,27 +98,11 @@ Public Class ImportZUGFeRDFiles
|
|||||||
Dim oEmailData = GetEmailDataForMessageId(MessageId)
|
Dim oEmailData = GetEmailDataForMessageId(MessageId)
|
||||||
Dim oSource = GetOriginalEmailPath(Args.OriginalEmailDirectory, MessageId)
|
Dim oSource = GetOriginalEmailPath(Args.OriginalEmailDirectory, MessageId)
|
||||||
Dim oDestination = GetEmailPathWithSubjectAsName(Args.RejectedEmailDirectory, oEmailData.Subject)
|
Dim oDestination = GetEmailPathWithSubjectAsName(Args.RejectedEmailDirectory, oEmailData.Subject)
|
||||||
|
Dim oFinalFileName = _filesystem.GetVersionedFilename(oDestination)
|
||||||
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
|
|
||||||
|
|
||||||
Try
|
Try
|
||||||
File.Move(oSource, oFileName) 'oDestination)
|
IO.File.Move(oSource, oFinalFileName)
|
||||||
oEmailData.Attachment = oFileName 'oDestination
|
oEmailData.Attachment = oFinalFileName
|
||||||
Catch ex As Exception
|
Catch ex As Exception
|
||||||
_logger.Warn("File {0} could not be moved! Original Filename will be used!", oSource)
|
_logger.Warn("File {0} could not be moved! Original Filename will be used!", oSource)
|
||||||
_logger.Error(ex)
|
_logger.Error(ex)
|
||||||
@ -630,27 +615,12 @@ Public Class ImportZUGFeRDFiles
|
|||||||
End If
|
End If
|
||||||
End If
|
End If
|
||||||
|
|
||||||
Dim oVersion As Integer = 0
|
Dim oFileName = _filesystem.GetVersionedFilename(Path.Combine(oFinalMoveDirectory, oFile.Name))
|
||||||
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
|
|
||||||
|
|
||||||
_filesystem.MoveTo(oFile.FullName, oFileName, oFinalMoveDirectory)
|
_filesystem.MoveTo(oFile.FullName, oFileName, oFinalMoveDirectory)
|
||||||
|
|
||||||
_logger.Info("Finished processing file {0}", oFile.Name)
|
_logger.Info("Finished processing file {0}", oFile.Name)
|
||||||
_logger.Info("File moved to {0}", oFileName)
|
_logger.Info("File moved to {0}", oFileName)
|
||||||
|
|
||||||
Catch ex As Exception
|
Catch ex As Exception
|
||||||
_logger.Warn("Could not move file {0}", oFile.FullName)
|
_logger.Warn("Could not move file {0}", oFile.FullName)
|
||||||
_logger.Error(ex)
|
_logger.Error(ex)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user