mirror of
https://github.com/Kareadita/Kavita.git
synced 2025-07-09 03:04:19 -04:00
Simplify parent lookup with Directory.GetParent (#1455)
* Simplify parent lookup with Directory.GetParent * Address comments
This commit is contained in:
parent
66a998425b
commit
329970f01b
@ -963,5 +963,36 @@ namespace API.Tests.Services
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region GetParentDirectory
|
||||
|
||||
[Theory]
|
||||
[InlineData(@"C:/file.txt", "C:/")]
|
||||
[InlineData(@"C:/folder/file.txt", "C:/folder")]
|
||||
[InlineData(@"C:/folder/subfolder/file.txt", "C:/folder/subfolder")]
|
||||
public void GetParentDirectoryName_ShouldFindParentOfFiles(string path, string expected)
|
||||
{
|
||||
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
|
||||
{
|
||||
{ path, new MockFileData(string.Empty)}
|
||||
});
|
||||
|
||||
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), fileSystem);
|
||||
Assert.Equal(expected, ds.GetParentDirectoryName(path));
|
||||
}
|
||||
[Theory]
|
||||
[InlineData(@"C:/folder", "C:/")]
|
||||
[InlineData(@"C:/folder/subfolder", "C:/folder")]
|
||||
[InlineData(@"C:/folder/subfolder/another", "C:/folder/subfolder")]
|
||||
public void GetParentDirectoryName_ShouldFindParentOfDirectories(string path, string expected)
|
||||
{
|
||||
var fileSystem = new MockFileSystem();
|
||||
fileSystem.AddDirectory(path);
|
||||
|
||||
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), fileSystem);
|
||||
Assert.Equal(expected, ds.GetParentDirectoryName(path));
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.IO;
|
||||
@ -554,31 +554,18 @@ namespace API.Services
|
||||
/// <summary>
|
||||
/// Returns the parent directories name for a file or folder. Empty string is path is not valid.
|
||||
/// </summary>
|
||||
/// <remarks>This does touch I/O with an Attribute lookup</remarks>
|
||||
/// <param name="fileOrFolder"></param>
|
||||
/// <returns></returns>
|
||||
public string GetParentDirectoryName(string fileOrFolder)
|
||||
{
|
||||
// TODO: Write Unit tests
|
||||
try
|
||||
{
|
||||
var attr = File.GetAttributes(fileOrFolder);
|
||||
var isDirectory = attr.HasFlag(FileAttributes.Directory);
|
||||
if (isDirectory)
|
||||
{
|
||||
return Parser.Parser.NormalizePath(FileSystem.DirectoryInfo
|
||||
.FromDirectoryName(fileOrFolder).Parent
|
||||
.FullName);
|
||||
}
|
||||
|
||||
return Parser.Parser.NormalizePath(FileSystem.FileInfo
|
||||
.FromFileName(fileOrFolder).Directory.Parent
|
||||
.FullName);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
try
|
||||
{
|
||||
return Parser.Parser.NormalizePath(Directory.GetParent(fileOrFolder).FullName);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
Loading…
x
Reference in New Issue
Block a user