Custom illegal character replacement
This commit is contained in:
parent
184ba84600
commit
2ab466c570
24 changed files with 838 additions and 218 deletions
|
|
@ -12,6 +12,8 @@ namespace FileNamingTemplateTests
|
|||
[TestClass]
|
||||
public class GetFilePath
|
||||
{
|
||||
static ReplacementCharacters Replacements = ReplacementCharacters.Default;
|
||||
|
||||
[TestMethod]
|
||||
public void equiv_GetValidFilename()
|
||||
{
|
||||
|
|
@ -19,81 +21,33 @@ namespace FileNamingTemplateTests
|
|||
sb.Append('0', 300);
|
||||
var longText = sb.ToString();
|
||||
|
||||
var expectedOld = "C:\\foo\\bar\\my_ book 00000000000000000000000000000000000000000 [ID123456].txt";
|
||||
var expectedNew = "C:\\foo\\bar\\my꞉ book 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 [ID123456].txt";
|
||||
var f1 = OLD_GetValidFilename(@"C:\foo\bar", "my: book " + longText, "txt", "ID123456");
|
||||
var f2 = NEW_GetValidFilename_FileNamingTemplate(@"C:\foo\bar", "my: book " + longText, "txt", "ID123456");
|
||||
|
||||
f1.Should().Be(expectedOld);
|
||||
f2.Should().Be(expectedNew);
|
||||
}
|
||||
private static string OLD_GetValidFilename(string dirFullPath, string filename, string extension, string metadataSuffix)
|
||||
{
|
||||
ArgumentValidator.EnsureNotNullOrWhiteSpace(dirFullPath, nameof(dirFullPath));
|
||||
|
||||
filename ??= "";
|
||||
|
||||
// sanitize. omit invalid characters. exception: colon => underscore
|
||||
filename = filename.Replace(":", "_");
|
||||
filename = FileUtility.GetSafeFileName(filename);
|
||||
|
||||
if (filename.Length > 50)
|
||||
filename = filename.Substring(0, 50);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(metadataSuffix))
|
||||
filename += $" [{metadataSuffix}]";
|
||||
|
||||
// extension is null when this method is used for directory names
|
||||
extension = FileUtility.GetStandardizedExtension(extension);
|
||||
|
||||
// ensure uniqueness
|
||||
var fullfilename = Path.Combine(dirFullPath, filename + extension);
|
||||
var i = 0;
|
||||
while (File.Exists(fullfilename))
|
||||
fullfilename = Path.Combine(dirFullPath, filename + $" ({++i})" + extension);
|
||||
|
||||
return fullfilename;
|
||||
}
|
||||
private static string NEW_GetValidFilename_FileNamingTemplate(string dirFullPath, string filename, string extension, string metadataSuffix)
|
||||
{
|
||||
var template = $"<title> [<id>]";
|
||||
|
||||
var fullfilename = Path.Combine(dirFullPath, template + FileUtility.GetStandardizedExtension(extension));
|
||||
|
||||
var fileNamingTemplate = new FileNamingTemplate(fullfilename) { IllegalCharacterReplacements = "_" };
|
||||
var fileNamingTemplate = new FileNamingTemplate(fullfilename);
|
||||
fileNamingTemplate.AddParameterReplacement("title", filename);
|
||||
fileNamingTemplate.AddParameterReplacement("id", metadataSuffix);
|
||||
return fileNamingTemplate.GetFilePath().PathWithoutPrefix;
|
||||
return fileNamingTemplate.GetFilePath(Replacements).PathWithoutPrefix;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void equiv_GetMultipartFileName()
|
||||
{
|
||||
var expected = @"C:\foo\bar\my file - 002 - title.txt";
|
||||
var f1 = OLD_GetMultipartFileName(@"C:\foo\bar\my file.txt", 2, 100, "title");
|
||||
var f2 = NEW_GetMultipartFileName_FileNamingTemplate(@"C:\foo\bar\my file.txt", 2, 100, "title");
|
||||
|
||||
f1.Should().Be(expected);
|
||||
f1.Should().Be(f2);
|
||||
f2.Should().Be(expected);
|
||||
}
|
||||
private static string OLD_GetMultipartFileName(string originalPath, int partsPosition, int partsTotal, string suffix)
|
||||
{
|
||||
// 1-9 => 1-9
|
||||
// 10-99 => 01-99
|
||||
// 100-999 => 001-999
|
||||
var chapterCountLeadingZeros = partsPosition.ToString().PadLeft(partsTotal.ToString().Length, '0');
|
||||
|
||||
string extension = Path.GetExtension(originalPath);
|
||||
|
||||
var filenameBase = $"{Path.GetFileNameWithoutExtension(originalPath)} - {chapterCountLeadingZeros}";
|
||||
if (!string.IsNullOrWhiteSpace(suffix))
|
||||
filenameBase += $" - {suffix}";
|
||||
|
||||
// Replace illegal path characters with spaces
|
||||
var fileName = FileUtility.GetSafeFileName(filenameBase, " ");
|
||||
var path = Path.Combine(Path.GetDirectoryName(originalPath), fileName + extension);
|
||||
return path;
|
||||
}
|
||||
private static string NEW_GetMultipartFileName_FileNamingTemplate(string originalPath, int partsPosition, int partsTotal, string suffix)
|
||||
{
|
||||
// 1-9 => 1-9
|
||||
|
|
@ -103,10 +57,10 @@ namespace FileNamingTemplateTests
|
|||
|
||||
var t = Path.ChangeExtension(originalPath, null) + " - <chapter> - <title>" + Path.GetExtension(originalPath);
|
||||
|
||||
var fileNamingTemplate = new FileNamingTemplate(t) { IllegalCharacterReplacements = " " };
|
||||
var fileNamingTemplate = new FileNamingTemplate(t);
|
||||
fileNamingTemplate.AddParameterReplacement("chapter", chapterCountLeadingZeros);
|
||||
fileNamingTemplate.AddParameterReplacement("title", suffix);
|
||||
return fileNamingTemplate.GetFilePath().PathWithoutPrefix;
|
||||
return fileNamingTemplate.GetFilePath(Replacements).PathWithoutPrefix;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
|
|
@ -114,7 +68,7 @@ namespace FileNamingTemplateTests
|
|||
{
|
||||
var fileNamingTemplate = new FileNamingTemplate(@"\foo\<title>.txt");
|
||||
fileNamingTemplate.AddParameterReplacement("title", @"s\l/a\s/h\e/s");
|
||||
fileNamingTemplate.GetFilePath().PathWithoutPrefix.Should().Be(@"\foo\slashes.txt");
|
||||
fileNamingTemplate.GetFilePath(Replacements).PathWithoutPrefix.Should().Be(@"\foo\sl∕as∕he∕s.txt");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,49 +12,88 @@ namespace FileUtilityTests
|
|||
[TestClass]
|
||||
public class GetSafePath
|
||||
{
|
||||
[TestMethod]
|
||||
public void null_path_throws() => Assert.ThrowsException<ArgumentNullException>(() => FileUtility.GetSafePath(null));
|
||||
static readonly ReplacementCharacters Default = ReplacementCharacters.Default;
|
||||
static readonly ReplacementCharacters LoFiDefault = ReplacementCharacters.LoFiDefault;
|
||||
static readonly ReplacementCharacters Barebones = ReplacementCharacters.Minimum;
|
||||
|
||||
// needs separate method. middle null param not running correctly in TestExplorer when used in DataRow()
|
||||
[TestMethod]
|
||||
[DataRow("http://test.com/a/b/c", @"http꞉\test.com\a\b\c")]
|
||||
public void null_replacement(string inStr, string outStr) => Tests(inStr, null, outStr);
|
||||
public void null_path_throws() => Assert.ThrowsException<ArgumentNullException>(() => FileUtility.GetSafePath(null, Default));
|
||||
|
||||
[TestMethod]
|
||||
// non-empty replacement
|
||||
[DataRow("abc*abc.txt", "abc✱abc.txt")]
|
||||
// standardize slashes
|
||||
[DataRow(@"a/b\c/d", @"a\b\c\d")]
|
||||
// remove illegal chars
|
||||
[DataRow("a*?:z.txt", "a✱?꞉z.txt")]
|
||||
// retain drive letter path colon
|
||||
[DataRow(@"C:\az.txt", @"C:\az.txt")]
|
||||
// replace all other colons
|
||||
[DataRow(@"a\b:c\d.txt", @"a\b꞉c\d.txt")]
|
||||
// remove empty directories
|
||||
[DataRow(@"C:\a\\\b\c\\\d.txt", @"C:\a\b\c\d.txt")]
|
||||
[DataRow(@"C:\""foo\<id>", @"C:\“foo\<id>")]
|
||||
public void DefaultTests(string inStr, string outStr) => Assert.AreEqual(outStr, FileUtility.GetSafePath(inStr, Default).PathWithoutPrefix);
|
||||
|
||||
[TestMethod]
|
||||
// non-empty replacement
|
||||
[DataRow("abc*abc.txt", "abc_abc.txt")]
|
||||
// standardize slashes
|
||||
[DataRow(@"a/b\c/d", @"a\b\c\d")]
|
||||
// remove illegal chars
|
||||
[DataRow("a*?:z.txt", "a__-z.txt")]
|
||||
// retain drive letter path colon
|
||||
[DataRow(@"C:\az.txt", @"C:\az.txt")]
|
||||
// replace all other colons
|
||||
[DataRow(@"a\b:c\d.txt", @"a\b-c\d.txt")]
|
||||
// remove empty directories
|
||||
[DataRow(@"C:\a\\\b\c\\\d.txt", @"C:\a\b\c\d.txt")]
|
||||
[DataRow(@"C:\""foo\<id>", @"C:\'foo\{id}")]
|
||||
public void LoFiDefaultTests(string inStr, string outStr) => Assert.AreEqual(outStr, FileUtility.GetSafePath(inStr, LoFiDefault).PathWithoutPrefix);
|
||||
|
||||
[TestMethod]
|
||||
// empty replacement
|
||||
[DataRow("abc*abc.txt", "", "abc✱abc.txt")]
|
||||
// non-empty replacement
|
||||
[DataRow("abc*abc.txt", "ZZZ", "abc✱abc.txt")]
|
||||
[DataRow("abc*abc.txt", "abc_abc.txt")]
|
||||
// standardize slashes
|
||||
[DataRow(@"a/b\c/d", "Z", @"a\b\c\d")]
|
||||
[DataRow(@"a/b\c/d", @"a\b\c\d")]
|
||||
// remove illegal chars
|
||||
[DataRow("a*?:z.txt", "Z", "a✱?꞉z.txt")]
|
||||
[DataRow("a*?:z.txt", "a___z.txt")]
|
||||
// retain drive letter path colon
|
||||
[DataRow(@"C:\az.txt", "Z", @"C:\az.txt")]
|
||||
[DataRow(@"C:\az.txt", @"C:\az.txt")]
|
||||
// replace all other colons
|
||||
[DataRow(@"a\b:c\d.txt", "ZZZ", @"a\b꞉c\d.txt")]
|
||||
[DataRow(@"a\b:c\d.txt", @"a\b_c\d.txt")]
|
||||
// remove empty directories
|
||||
[DataRow(@"C:\a\\\b\c\\\d.txt", "ZZZ", @"C:\a\b\c\d.txt")]
|
||||
[DataRow(@"C:\""foo\<id>", "ZZZ", @"C:\“foo\<id>")]
|
||||
public void Tests(string inStr, string replacement, string outStr) => Assert.AreEqual(outStr, FileUtility.GetSafePath(inStr, replacement).PathWithoutPrefix);
|
||||
[DataRow(@"C:\a\\\b\c\\\d.txt", @"C:\a\b\c\d.txt")]
|
||||
[DataRow(@"C:\""foo\<id>", @"C:\_foo\_id_")]
|
||||
public void BarebonesDefaultTests(string inStr, string outStr) => Assert.AreEqual(outStr, FileUtility.GetSafePath(inStr, Barebones).PathWithoutPrefix);
|
||||
}
|
||||
|
||||
[TestClass]
|
||||
public class GetSafeFileName
|
||||
{
|
||||
static readonly ReplacementCharacters Default = ReplacementCharacters.Default;
|
||||
static readonly ReplacementCharacters LoFiDefault = ReplacementCharacters.LoFiDefault;
|
||||
static readonly ReplacementCharacters Barebones = ReplacementCharacters.Minimum;
|
||||
|
||||
// needs separate method. middle null param not running correctly in TestExplorer when used in DataRow()
|
||||
[TestMethod]
|
||||
[DataRow("http://test.com/a/b/c", "httptest.comabc")]
|
||||
public void url_null_replacement(string inStr, string outStr) => ReplacementTests(inStr, null, outStr);
|
||||
[DataRow("http://test.com/a/b/c", "http꞉∕∕test.com∕a∕b∕c")]
|
||||
public void url_null_replacement(string inStr, string outStr) => DefaultReplacementTest(inStr, outStr);
|
||||
|
||||
[TestMethod]
|
||||
// empty replacement
|
||||
[DataRow("http://test.com/a/b/c", "", "httptest.comabc")]
|
||||
// single char replace
|
||||
[DataRow("http://test.com/a/b/c", "_", "http___test.com_a_b_c")]
|
||||
// multi char replace
|
||||
[DataRow("http://test.com/a/b/c", "!!!", "http!!!!!!!!!test.com!!!a!!!b!!!c")]
|
||||
public void ReplacementTests(string inStr, string replacement, string outStr) => FileUtility.GetSafeFileName(inStr, replacement).Should().Be(outStr);
|
||||
[DataRow("http://test.com/a/b/c", "http꞉∕∕test.com∕a∕b∕c")]
|
||||
public void DefaultReplacementTest(string inStr, string outStr) => Default.ReplaceInvalidFilenameChars(inStr).Should().Be(outStr);
|
||||
|
||||
[TestMethod]
|
||||
// empty replacement
|
||||
[DataRow("http://test.com/a/b/c", "http-__test.com_a_b_c")]
|
||||
public void LoFiDefaultReplacementTest(string inStr, string outStr) => LoFiDefault.ReplaceInvalidFilenameChars(inStr).Should().Be(outStr);
|
||||
|
||||
[TestMethod]
|
||||
// empty replacement
|
||||
[DataRow("http://test.com/a/b/c", "http___test.com_a_b_c")]
|
||||
public void BarebonesDefaultReplacementTest(string inStr, string outStr) => Barebones.ReplaceInvalidFilenameChars(inStr).Should().Be(outStr);
|
||||
}
|
||||
|
||||
[TestClass]
|
||||
|
|
@ -117,6 +156,8 @@ namespace FileUtilityTests
|
|||
[TestClass]
|
||||
public class GetValidFilename
|
||||
{
|
||||
static ReplacementCharacters Replacements = ReplacementCharacters.Default;
|
||||
|
||||
[TestMethod]
|
||||
// dot-files
|
||||
[DataRow(@"C:\a bc\x y z\.f i l e.txt")]
|
||||
|
|
@ -134,7 +175,7 @@ namespace FileUtilityTests
|
|||
// file end dots
|
||||
[DataRow(@"C:\a bc\x y z\f i l e.txt . . .", @"C:\a bc\x y z\f i l e.txt")]
|
||||
public void Tests(string input, string expected)
|
||||
=> FileUtility.GetValidFilename(input).PathWithoutPrefix.Should().Be(expected);
|
||||
=> FileUtility.GetValidFilename(input, Replacements).PathWithoutPrefix.Should().Be(expected);
|
||||
}
|
||||
|
||||
[TestClass]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue