Skip to content

Commit

Permalink
#128 Folder name normalization.
Browse files Browse the repository at this point in the history
  • Loading branch information
MonkAlex committed Mar 30, 2019
1 parent d3ae8e7 commit d930c6b
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 4 deletions.
29 changes: 25 additions & 4 deletions MangaReader.Core/Services/DirectoryHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using MangaReader.Core.Exception;
using MangaReader.Core.Manga;
Expand All @@ -9,6 +11,9 @@ namespace MangaReader.Core.Services
{
public static class DirectoryHelpers
{
private static readonly string NormalizationPattern = string.Format(@"([{0}]*\.+$)|([{0}]+)", Regex.Escape(string.Concat(new string(Path.GetInvalidPathChars()), "?", "/", "*", "\"")));
private static readonly string[] DosReservedNames = { "CON", "PRN", "AUX", "NUL", "COM0", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "LPT0", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9" };

public static bool Equals(string one, string two)
{
return string.Compare(new DirectoryInfo(one).FullName.TrimEnd('\\'),
Expand Down Expand Up @@ -78,7 +83,7 @@ public static string MakeValidPath(string name)
if (Environment.OSVersion.Platform == PlatformID.Unix ||
Environment.OSVersion.Platform == PlatformID.MacOSX)
return name;

const string replacement = ".";
var matchesCount = Regex.Matches(name, @":\\").Count;
string correctName;
Expand All @@ -90,10 +95,26 @@ public static string MakeValidPath(string name)
else
correctName = name.Replace(":", replacement);

var invalidChars = Regex.Escape(string.Concat(new string(Path.GetInvalidPathChars()), "?", "/", "*", "\""));
var invalidRegStr = string.Format(@"([{0}]*\.+$)|([{0}]+)", invalidChars);
var replace = Regex.Replace(correctName, NormalizationPattern, replacement);
foreach (var reservedName in DosReservedNames)
{
var builder = new List<string>();
foreach (var folder in replace.Split(Path.DirectorySeparatorChar))
{
var changedName = folder;
if (string.Equals(folder, reservedName, StringComparison.InvariantCultureIgnoreCase))
changedName = replacement + reservedName;

var value = reservedName + '.';
if (folder.StartsWith(value, StringComparison.InvariantCultureIgnoreCase))
changedName = replacement + value + folder.Remove(0, value.Length);

builder.Add(changedName);
}

return Regex.Replace(correctName, invalidRegStr, replacement);
replace = string.Join<string>(Path.DirectorySeparatorChar.ToString(), builder);
}
return replace.TrimEnd(' ', '.');
}


Expand Down
49 changes: 49 additions & 0 deletions Tests/Tests.API/FolderNameNormalization.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using MangaReader.Core.Services;
using NUnit.Framework;

namespace Tests.API
{
[TestFixture]
class FolderNameNormalization
{
[Test, Sequential]
public void CheckNotAllowedNames([Values(
"test"
,@"C:\manga\manga:name"
,@"usr\home\manga:name"
,@"start < > : "" / \ | ? * end"
,"\x15\x3D" // less than ASCII space
,"\x21\x3D" // HEX of !, valid
,"\x3F\x3D" // HEX of ?, not valid
,@"C:\manga\ trailing space "
,@"C:\manga\...trailing period..."
,@"C:\manga\..." // Bad manga name get the root folder, bug =_=
,@"C:\manga\CON"
,@"C:\manga\CON.txt"
,@"CON"
,@"C:\manga\con.txt\context"
,@"home\NUL.liza"
,@"home\ NUL.liza"
)] string name, [Values(
"test"
,@"C:\manga\manga.name"
,@"usr\home\manga.name"
,@"start . . . . . \ . . . end"
,".="
,"!="
,".="
,@"C:\manga\ trailing space"
,@"C:\manga\...trailing period"
,@"C:\manga\"
,@"C:\manga\.CON"
,@"C:\manga\.CON.txt"
,@".CON"
,@"C:\manga\.CON.txt\context"
,@"home\.NUL.liza"
,@"home\ NUL.liza"
)] string expected)
{
Assert.AreEqual(expected, DirectoryHelpers.MakeValidPath(name));
}
}
}
1 change: 1 addition & 0 deletions Tests/Tests.API/Tests.API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
</Choose>
<ItemGroup>
<Compile Include="ApplicationControl.cs" />
<Compile Include="FolderNameNormalization.cs" />
<Compile Include="VersionInfo.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down

0 comments on commit d930c6b

Please sign in to comment.