diff --git a/Filesystem/File.vb b/Filesystem/File.vb index 2f4365a6..814a3424 100644 --- a/Filesystem/File.vb +++ b/Filesystem/File.vb @@ -31,6 +31,76 @@ Public Class File _logger = LogConfig.GetLogger() End Sub + ''' + ''' Adds fileversions to given filename `Destination` if that file already exists. + ''' + ''' + ''' + 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 + ''' ''' Removes files in a directory filtered by filename, extension and last write date ''' diff --git a/Jobs/EDMI/ZUGFeRD/ImportZUGFeRDFiles.vb b/Jobs/EDMI/ZUGFeRD/ImportZUGFeRDFiles.vb index 47feb79f..abdf4a63 100644 --- a/Jobs/EDMI/ZUGFeRD/ImportZUGFeRDFiles.vb +++ b/Jobs/EDMI/ZUGFeRD/ImportZUGFeRDFiles.vb @@ -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)