From 9b0777d54df430dab7e59e03fc92819453ce39ab Mon Sep 17 00:00:00 2001 From: Tichau Date: Fri, 13 Nov 2015 21:45:32 +0100 Subject: [PATCH] Add check if category is compatible with output type. --- .../ConversionJobs/ConversionJob.cs | 9 +++++ Application/FileConverter/PathHelpers.cs | 38 +++++++++++++++++-- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/Application/FileConverter/ConversionJobs/ConversionJob.cs b/Application/FileConverter/ConversionJobs/ConversionJob.cs index 5968f77..f2d0151 100644 --- a/Application/FileConverter/ConversionJobs/ConversionJob.cs +++ b/Application/FileConverter/ConversionJobs/ConversionJob.cs @@ -115,6 +115,15 @@ namespace FileConverter.ConversionJobs throw new Exception("The conversion preset must be valid."); } + string extension = System.IO.Path.GetExtension(inputFilePath); + extension = extension.Substring(1, extension.Length - 1); + string extensionCategory = PathHelpers.GetExtensionCategory(extension); + if (!PathHelpers.IsOutputTypeCompatibleWithCategory(this.ConversionPreset.OutputType, extensionCategory)) + { + this.ConvertionFailed("The input file type is not compatible with the output file type."); + return; + } + this.initialInputPath = inputFilePath; this.InputFilePath = inputFilePath; this.OutputFilePath = this.ConversionPreset.GenerateOutputFilePath(inputFilePath); diff --git a/Application/FileConverter/PathHelpers.cs b/Application/FileConverter/PathHelpers.cs index 4eff34b..3acec4a 100644 --- a/Application/FileConverter/PathHelpers.cs +++ b/Application/FileConverter/PathHelpers.cs @@ -72,16 +72,48 @@ namespace FileConverter case "ogg": case "wav": case "wma": - return "Audio"; + return InputCategoryNames.Audio; case "avi": case "flv": case "mp4": case "mkv": - return "Video"; + return InputCategoryNames.Video; } - return "Misc"; + return InputCategoryNames.Misc; + } + + public static bool IsOutputTypeCompatibleWithCategory(OutputType outputType, string category) + { + if (category == InputCategoryNames.Misc) + { + // Misc category contains unsorted input extensions, so we consider that they are compatible to be tolerant. + return true; + } + + switch (outputType) + { + case OutputType.Aac: + case OutputType.Flac: + case OutputType.Mp3: + case OutputType.Ogg: + case OutputType.Wav: + return category == InputCategoryNames.Audio || category == InputCategoryNames.Video; + + case OutputType.Mkv: + return category == InputCategoryNames.Video; + + default: + return false; + } + } + + public static class InputCategoryNames + { + public const string Audio = "Audio"; + public const string Video = "Video"; + public const string Misc = "Misc"; } } }