[ZUGFeRD] fix versioning of files
This commit is contained in:
@@ -31,6 +31,76 @@ Public Class File
|
||||
_logger = LogConfig.GetLogger()
|
||||
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>
|
||||
''' Removes files in a directory filtered by filename, extension and last write date
|
||||
''' </summary>
|
||||
|
||||
Reference in New Issue
Block a user