Bug fix #167 : folders with leading or trailing whitespace will break file saving. Including paths created from templates

This commit is contained in:
Robert McRackan 2021-12-07 09:24:36 -05:00
parent c267332027
commit de45d008c7
2 changed files with 22 additions and 1 deletions

View File

@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Version>6.6.0.1</Version>
<Version>6.6.1.1</Version>
</PropertyGroup>
<ItemGroup>

View File

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using Dinah.Core;
using Polly;
using Polly.Retry;
@ -65,6 +66,8 @@ namespace FileManager
var fullfilename = fileStem.Truncate(MAX_FILENAME_LENGTH - extension.Length) + extension;
fullfilename = removeInvalidWhitespace(fullfilename);
var i = 0;
while (File.Exists(fullfilename))
{
@ -136,6 +139,24 @@ namespace FileManager
return path[0] + remainder;
}
private static string removeInvalidWhitespace_pattern { get; } = $@"\s*\{Path.DirectorySeparatorChar}\s*";
private static Regex removeInvalidWhitespace_regex { get; } = new(removeInvalidWhitespace_pattern, RegexOptions.Compiled | RegexOptions.IgnorePatternWhitespace);
/// <summary>no part of the path may begin or end in whitespace</summary>
private static string removeInvalidWhitespace(string fullfilename)
{
// no whitespace at beginning or end
// replace whitespace around path slashes
// regex (with space added for clarity)
// \s* \\ \s* => \
fullfilename = fullfilename.Trim();
fullfilename = removeInvalidWhitespace_regex.Replace(fullfilename, @"\");
fullfilename = removeDoubleSlashes(fullfilename);
return fullfilename;
}
/// <summary>
/// Move file.
/// <br/>- Ensure valid file name path: remove invalid chars, ensure uniqueness, enforce max file length