From d1bb8184d101b71e2a0d102ea411780dcfd375ef Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Thu, 20 Sep 2018 22:18:07 -0700 Subject: [PATCH] Path correction --- DICUI/MainWindow.xaml.cs | 10 ++++++- DICUI/Utilities/DumpEnvironment.cs | 45 +++++++++++++++++++++++------- 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/DICUI/MainWindow.xaml.cs b/DICUI/MainWindow.xaml.cs index a1fc1c3b..5d3ed6f2 100644 --- a/DICUI/MainWindow.xaml.cs +++ b/DICUI/MainWindow.xaml.cs @@ -389,11 +389,19 @@ namespace DICUI Type = MediaTypeComboBox.SelectedItem as MediaType? }; - // Fix and write back the paths + // Fix the output paths env.FixOutputPaths(); + + // Disable automatic reprocessing of the textboxes until we're done + OutputDirectoryTextBox.TextChanged -= OutputDirectoryTextBoxTextChanged; + OutputFilenameTextBox.TextChanged -= OutputFilenameTextBoxTextChanged; + OutputDirectoryTextBox.Text = env.OutputDirectory; OutputFilenameTextBox.Text = env.OutputFilename; + OutputDirectoryTextBox.TextChanged += OutputDirectoryTextBoxTextChanged; + OutputFilenameTextBox.TextChanged += OutputFilenameTextBoxTextChanged; + return env; } diff --git a/DICUI/Utilities/DumpEnvironment.cs b/DICUI/Utilities/DumpEnvironment.cs index b16b89ba..ebf426d3 100644 --- a/DICUI/Utilities/DumpEnvironment.cs +++ b/DICUI/Utilities/DumpEnvironment.cs @@ -240,20 +240,45 @@ namespace DICUI.Utilities #region Internal for Testing Purposes /// - /// Fix the output paths to remove characters that DiscImageCreator can't handle + /// Fix output paths to strip out any invalid characters /// - /// - /// TODO: Investigate why the `&` replacement is needed - /// internal void FixOutputPaths() { - // Only fix OutputDirectory if it's not blank or null - if (!String.IsNullOrWhiteSpace(OutputDirectory)) - OutputDirectory = OutputDirectory.Replace('.', '_').Replace('&', '_'); + try + { + // Cache if we had a directory separator or not + bool endedWithDirectorySeparator = OutputDirectory.EndsWith(Path.DirectorySeparatorChar.ToString()); - // Only fix OutputFilename if it's not blank or null - if (!String.IsNullOrWhiteSpace(OutputFilename)) - OutputFilename = new StringBuilder(OutputFilename.Replace('&', '_')).Replace('.', '_', 0, OutputFilename.LastIndexOf('.') == -1 ? 0 : OutputFilename.LastIndexOf('.')).ToString(); + // Normalize the path so the below filters work better + string combinedPath = Path.Combine(OutputDirectory, OutputFilename); + OutputDirectory = Path.GetDirectoryName(combinedPath); + OutputFilename = Path.GetFileName(combinedPath); + + // If we have either a blank filename or path, just exit + if (String.IsNullOrWhiteSpace(OutputFilename) || String.IsNullOrWhiteSpace(OutputDirectory)) + return; + + // Take care of extra path characters + OutputDirectory = new StringBuilder(OutputDirectory.Replace('.', '_').Replace('&', '_')) + .Replace(':', '_', 0, OutputDirectory.LastIndexOf(':') == -1 ? 0 : OutputDirectory.LastIndexOf(':')).ToString(); + OutputFilename = new StringBuilder(OutputFilename.Replace('&', '_')) + .Replace('.', '_', 0, OutputFilename.LastIndexOf('.') == -1 ? 0 : OutputFilename.LastIndexOf('.')).ToString(); + + // Sanitize everything else + foreach (char c in Path.GetInvalidPathChars()) + OutputDirectory = OutputDirectory.Replace(c, '_'); + foreach (char c in Path.GetInvalidFileNameChars()) + OutputFilename = OutputFilename.Replace(c, '_'); + + // If we had a directory separator at the end before, add it again + if (endedWithDirectorySeparator) + OutputDirectory += Path.DirectorySeparatorChar; + } + catch + { + // We don't care what the error was + return; + } } ///