fixup! Fixed: Updating series path from different OS paths

This commit is contained in:
Mark McDowall 2024-07-13 21:24:29 -07:00
parent 7fd5302177
commit ff12c36204
3 changed files with 41 additions and 28 deletions

View File

@ -166,7 +166,6 @@ namespace NzbDrone.Common.Test
[TestCase(@"\\server\share\test", @"\\server\share")]
public void path_should_return_parent_name_windows(string path, string parentPath)
{
WindowsOnly();
path.GetParentName().Should().Be(parentPath);
}
@ -175,7 +174,6 @@ namespace NzbDrone.Common.Test
[TestCase(@"/test/tv", "test")]
public void path_should_return_parent_name_mono(string path, string parentPath)
{
PosixOnly();
path.GetParentName().Should().Be(parentPath);
}
@ -190,6 +188,7 @@ namespace NzbDrone.Common.Test
path.GetDirectoryName().Should().Be(parentPath);
}
[TestCase(@"/", "/")]
[TestCase(@"/test", "test")]
[TestCase(@"/test/tv", "tv")]
public void path_should_return_directory_name_mono(string path, string parentPath)

View File

@ -10,7 +10,7 @@ namespace NzbDrone.Common.Disk
private readonly string _path;
private readonly OsPathKind _kind;
private static readonly Regex UncPathRegex = new Regex(@"^\\\\(?:\?\\UNC\\)?[^\\]+\\[^\\]+(?:\\|$)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
private static readonly Regex UncPathRegex = new Regex(@"(?<unc>^\\\\(?:\?\\UNC\\)?[^\\]+\\[^\\]+)(?:\\|$)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
public OsPath(string path)
{
@ -22,7 +22,7 @@ namespace NzbDrone.Common.Disk
else
{
_kind = DetectPathKind(path);
_path = TrimTrailingSlashes(FixSlashes(path, _kind), _kind);
_path = FixSlashes(path, _kind);
}
}
@ -36,7 +36,7 @@ namespace NzbDrone.Common.Disk
else
{
_kind = kind;
_path = TrimTrailingSlashes(FixSlashes(path, kind), kind);
_path = FixSlashes(path, kind);
}
}
@ -99,7 +99,7 @@ namespace NzbDrone.Common.Disk
return path;
}
private static string TrimTrailingSlashes(string path, OsPathKind kind)
private static string TrimTrailingSlash(string path, OsPathKind kind)
{
switch (kind)
{
@ -156,7 +156,7 @@ namespace NzbDrone.Common.Disk
return Null;
}
if (rootLength > index)
if (rootLength > index + 1)
{
return new OsPath(_path.Substring(0, rootLength));
}
@ -167,6 +167,8 @@ namespace NzbDrone.Common.Disk
public string FullPath => _path;
public string PathWithoutTrailingSlash => TrimTrailingSlash(_path, _kind);
public string FileName
{
get
@ -189,6 +191,30 @@ namespace NzbDrone.Common.Disk
}
}
public string Name
{
// Meant to behave similar to DirectoryInfo.Name
get
{
var index = GetFileNameIndex();
if (index == -1)
{
return PathWithoutTrailingSlash;
}
var rootLength = GetRootLength();
if (rootLength > index + 1)
{
return _path.Substring(0, rootLength);
}
return TrimTrailingSlash(_path.Substring(index).TrimStart('/', '\\'), _kind);
}
}
public bool IsValid => _path.IsPathValid(PathValidationType.CurrentOs);
private int GetFileNameIndex()
@ -242,7 +268,7 @@ namespace NzbDrone.Common.Disk
// \\?\UNC\server\share\ or \\server\share
if (uncMatch.Success)
{
return uncMatch.Length;
return uncMatch.Groups["unc"].Length;
}
// \\?\C:\
@ -345,8 +371,8 @@ namespace NzbDrone.Common.Disk
return true;
}
var left = _path;
var right = other._path;
var left = PathWithoutTrailingSlash;
var right = other.PathWithoutTrailingSlash;
if (Kind == OsPathKind.Windows || other.Kind == OsPathKind.Windows)
{

View File

@ -99,35 +99,23 @@ namespace NzbDrone.Common.Extensions
return null;
}
var path = new OsPath(cleanPath).Directory.AsDirectory();
var path = new OsPath(cleanPath).Directory;
return path == OsPath.Null ? null : path.FullPath;
return path == OsPath.Null ? null : path.PathWithoutTrailingSlash;
}
public static string GetParentName(this string childPath)
{
var cleanPath = childPath.GetCleanPath();
var path = new OsPath(childPath).Directory;
if (cleanPath.IsNullOrWhiteSpace())
{
return null;
}
return Directory.GetParent(cleanPath)?.Name;
return path == OsPath.Null ? null : path.Name;
}
public static string GetDirectoryName(this string childPath)
{
var cleanPath = childPath.GetCleanPath();
var path = new OsPath(childPath);
if (cleanPath.IsNullOrWhiteSpace())
{
return null;
}
var directoryInfo = new DirectoryInfo(cleanPath);
return directoryInfo.Name;
return path == OsPath.Null ? null : path.Name;
}
public static string GetCleanPath(this string path)