diff --git a/Application/FileConverter/ConversionJobs/ConversionJob.cs b/Application/FileConverter/ConversionJobs/ConversionJob.cs index 31d7fdc..5968f77 100644 --- a/Application/FileConverter/ConversionJobs/ConversionJob.cs +++ b/Application/FileConverter/ConversionJobs/ConversionJob.cs @@ -12,6 +12,7 @@ namespace FileConverter.ConversionJobs private float progress = 0f; private ConversionState state = ConversionState.Unknown; private string errorMessage = string.Empty; + private string initialInputPath = string.Empty; public ConversionJob() { @@ -114,8 +115,10 @@ namespace FileConverter.ConversionJobs throw new Exception("The conversion preset must be valid."); } + this.initialInputPath = inputFilePath; this.InputFilePath = inputFilePath; this.OutputFilePath = this.ConversionPreset.GenerateOutputFilePath(inputFilePath); + if (!PathHelpers.IsPathValid(this.OutputFilePath)) { this.ConvertionFailed("Invalid output path generated by output file path template."); @@ -123,7 +126,20 @@ namespace FileConverter.ConversionJobs return; } - // Check if all output folders exists. + if (this.OutputFilePath == this.InputFilePath) + { + // If the input post conversion action is to move or delete the input file, change its name in order to keep the output name intact. + if (this.ConversionPreset.InputPostConversionAction == InputPostConversionAction.MoveInArchiveFolder || + this.ConversionPreset.InputPostConversionAction == InputPostConversionAction.Delete) + { + string inputExtension = System.IO.Path.GetExtension(this.InputFilePath); + string pathWithoutExtension = this.InputFilePath.Substring(0, this.InputFilePath.Length - inputExtension.Length); + this.InputFilePath = PathHelpers.GenerateUniquePath(pathWithoutExtension + "_TEMP" + inputExtension); + System.IO.File.Move(inputFilePath, this.InputFilePath); + } + } + + // Create output folder that doesn't exist. { StringBuilder path = new StringBuilder(this.OutputFilePath.Length); foreach (string directory in PathHelpers.GetDirectories(this.OutputFilePath)) @@ -147,16 +163,10 @@ namespace FileConverter.ConversionJobs } } + // Make the output path valid. try { - string basePath = System.IO.Path.GetDirectoryName(this.OutputFilePath) + "\\" + System.IO.Path.GetFileNameWithoutExtension(this.OutputFilePath); - string baseExtension = System.IO.Path.GetExtension(this.OutputFilePath); - int index = 2; - while (System.IO.File.Exists(this.OutputFilePath)) - { - this.OutputFilePath = string.Format("{0} ({1}){2}", basePath, index, baseExtension); - index++; - } + this.OutputFilePath = PathHelpers.GenerateUniquePath(this.OutputFilePath); } catch (Exception exception) { @@ -193,10 +203,7 @@ namespace FileConverter.ConversionJobs if (this.State != ConversionState.Failed) { - // Convertion succeed ! - this.Progress = 1f; - this.State = ConversionState.Done; - Diagnostics.Log("\nDone!"); + this.OnConversionSucceed(); } } @@ -208,6 +215,43 @@ namespace FileConverter.ConversionJobs { } + protected virtual void OnConversionSucceed() + { + Diagnostics.Log("Conversion Succeed!"); + + // Apply the input post conversion action. + switch (this.ConversionPreset.InputPostConversionAction) + { + case InputPostConversionAction.None: + break; + + case InputPostConversionAction.MoveInArchiveFolder: + string basePath = System.IO.Path.GetDirectoryName(this.initialInputPath); + string inputFilename = System.IO.Path.GetFileName(this.initialInputPath); + string archivePath = basePath + "\\" + this.ConversionPreset.ConversionArchiveFolderName; + if (!System.IO.Directory.Exists(archivePath)) + { + System.IO.Directory.CreateDirectory(archivePath); + } + + string newPath = PathHelpers.GenerateUniquePath(archivePath + "\\" + inputFilename); + System.IO.File.Move(this.InputFilePath, newPath); + Diagnostics.Log("Input file moved in archive folder: '{0}'", newPath); + break; + + case InputPostConversionAction.Delete: + System.IO.File.Delete(this.InputFilePath); + Diagnostics.Log("Input file deleted: '{0}'", this.initialInputPath); + break; + } + + Diagnostics.Log(string.Empty); + + this.Progress = 1f; + this.State = ConversionState.Done; + Diagnostics.Log("Conversion Done!"); + } + protected void ConvertionFailed(string exitingMessage) { this.State = ConversionState.Failed; diff --git a/Application/FileConverter/ConversionPreset/ConversionPreset.cs b/Application/FileConverter/ConversionPreset/ConversionPreset.cs index 4729ad4..7cc866b 100644 --- a/Application/FileConverter/ConversionPreset/ConversionPreset.cs +++ b/Application/FileConverter/ConversionPreset/ConversionPreset.cs @@ -147,6 +147,15 @@ namespace FileConverter } } + [XmlIgnore] + public string ConversionArchiveFolderName + { + get + { + return "Conversion Archives"; + } + } + [XmlIgnore] public IConversionSettings Settings { diff --git a/Application/FileConverter/PathHelpers.cs b/Application/FileConverter/PathHelpers.cs index 7de417a..e09c6aa 100644 --- a/Application/FileConverter/PathHelpers.cs +++ b/Application/FileConverter/PathHelpers.cs @@ -44,5 +44,19 @@ namespace FileConverter yield return matchGroup.Captures[index].Value; } } + + public static string GenerateUniquePath(string path) + { + string baseExtension = System.IO.Path.GetExtension(path); + string basePath = path.Substring(0, path.Length - baseExtension.Length); + int index = 2; + while (System.IO.File.Exists(path)) + { + path = string.Format("{0} ({1}){2}", basePath, index, baseExtension); + index++; + } + + return path; + } } }