From 1d66f9fef0cc8089a25cc7b401ee819a156a9057 Mon Sep 17 00:00:00 2001 From: Tichau Date: Tue, 27 Oct 2015 09:06:35 +0100 Subject: [PATCH] Add: handle path validity error check in conversion jobs. --- Application/FileConverter/Application.xaml.cs | 18 ++++------ .../ConversionJobs/ConversionJob.cs | 33 ++++++++++++++++--- .../ConversionJobs/ConversionJob_FFMPEG.cs | 3 ++ 3 files changed, 37 insertions(+), 17 deletions(-) diff --git a/Application/FileConverter/Application.xaml.cs b/Application/FileConverter/Application.xaml.cs index 1118e1e..94ed178 100644 --- a/Application/FileConverter/Application.xaml.cs +++ b/Application/FileConverter/Application.xaml.cs @@ -102,20 +102,14 @@ namespace FileConverter if (args.Length <= 1) { this.debugMode = true; - System.Array.Resize(ref args, 9); + System.Array.Resize(ref args, 5); args[1] = "--conversion-preset"; args[2] = "To Mp3"; - args[3] = @"D:\Projects\FileConverter\TestFiles\Herbie Hancock - Speak Like A Child [RVG Edition].flac"; - args[3] = @"D:\Projects\FileConverter\TestFiles\01 - Le Bruit Du Bang.wma"; - args[4] = @"D:\Projects\FileConverter\TestFiles\test\Toccata.wav"; - args[5] = @"D:\Projects\FileConverter\TestFiles\test\Toccata - Copie (4).wav"; - args[5] = "--verbose"; - args[6] = @"D:\Projects\FileConverter\TestFiles\test\Toccata - Copie (3).wav"; - args[7] = @"D:\Projects\FileConverter\TestFiles\test\Toccata - Copie (2).wav"; - args[8] = @"D:\Projects\FileConverter\TestFiles\test\Toccata - Copie (5).wav"; + args[3] = "--verbose"; + args[4] = @"D:\Test\10 - Get around town (lp version).ogg"; - System.Array.Resize(ref args, 2); - args[1] = "--settings"; + //System.Array.Resize(ref args, 2); + //args[1] = "--settings"; } #endif @@ -227,7 +221,7 @@ namespace FileConverter Diagnostics.Log("Delete file {0}.", conversionJob.OutputFilePath); try { - System.IO.File.Delete(conversionJob.OutputFilePath); + //System.IO.File.Delete(conversionJob.OutputFilePath); } catch { diff --git a/Application/FileConverter/ConversionJobs/ConversionJob.cs b/Application/FileConverter/ConversionJobs/ConversionJob.cs index 471051b..453624a 100644 --- a/Application/FileConverter/ConversionJobs/ConversionJob.cs +++ b/Application/FileConverter/ConversionJobs/ConversionJob.cs @@ -115,17 +115,39 @@ namespace FileConverter.ConversionJobs this.InputFilePath = inputFilePath; this.OutputFilePath = this.ConversionPreset.GenerateOutputFilePath(inputFilePath); - string presetError = this.ConversionPreset.Error; - if (!string.IsNullOrEmpty(presetError)) + if (!PathHelpers.IsPathValid(this.OutputFilePath)) { - this.State = ConversionState.Failed; - this.ErrorMessage = presetError; + this.ConvertionFailed("Invalid output path generated by output file path template."); + Diagnostics.Log(string.Format("Invalid output path generated: {0} from input: {1}.", this.OutputFilePath, this.InputFilePath)); return; } + // TODO: Check is all output folders exists. + + 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++; + } + } + catch (Exception exception) + { + this.ConvertionFailed("Can't generate a valid output filename."); + Diagnostics.Log(exception.Message); + return; + } + this.Initialize(); - this.State = ConversionState.Ready; + if (this.State == ConversionState.Unknown) + { + this.State = ConversionState.Ready; + } } public void StartConvertion() @@ -166,6 +188,7 @@ namespace FileConverter.ConversionJobs protected void ConvertionFailed(string exitingMessage) { this.State = ConversionState.Failed; + this.ErrorMessage = exitingMessage; } protected void NotifyPropertyChanged([CallerMemberName] string propertyName = "") diff --git a/Application/FileConverter/ConversionJobs/ConversionJob_FFMPEG.cs b/Application/FileConverter/ConversionJobs/ConversionJob_FFMPEG.cs index aa3670f..47adce2 100644 --- a/Application/FileConverter/ConversionJobs/ConversionJob_FFMPEG.cs +++ b/Application/FileConverter/ConversionJobs/ConversionJob_FFMPEG.cs @@ -27,6 +27,8 @@ namespace FileConverter.ConversionJobs protected override void Initialize() { + base.Initialize(); + if (this.ConversionPreset == null) { throw new Exception("The conversion preset must be valid."); @@ -38,6 +40,7 @@ namespace FileConverter.ConversionJobs string ffmpegPath = string.Format("{0}\\ffmpeg.exe", applicationDirectory); if (!System.IO.File.Exists(ffmpegPath)) { + this.ConvertionFailed("Can't find ffmpeg executable. You should try to reinstall the application."); Diagnostics.Log("Can't find ffmpeg executable ({0}). Try to reinstall the application.", ffmpegPath); return; }