Parser Enhancement: Fallback to Folder name (#129)

* More cases for parsing regex

* Implemented GetFoldersTillRoot for falling back on parsing when we can't get anything from the filename.

* Implemented a fallback strategy. Not tested on large libraries yet.

* Fallback tested and working great.

* Removed a test case that won't pass and added some trims
This commit is contained in:
Joseph Milazzo 2021-03-29 17:37:35 -05:00 committed by GitHub
parent d9246b7351
commit a0deafe75b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 94 additions and 10 deletions

View file

@ -40,6 +40,40 @@ namespace API.Services
reSearchPattern.IsMatch(Path.GetExtension(file)));
}
/// <summary>
/// Returns a list of folders from end of fullPath to rootPath.
///
/// Example) (C:/Manga/, C:/Manga/Love Hina/Specials/Omake/) returns [Omake, Specials, Love Hina]
/// </summary>
/// <param name="rootPath"></param>
/// <param name="fullPath"></param>
/// <returns></returns>
public static IEnumerable<string> GetFoldersTillRoot(string rootPath, string fullPath)
{
var separator = Path.AltDirectorySeparatorChar;
if (fullPath.Contains(Path.DirectorySeparatorChar))
{
fullPath = fullPath.Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
}
if (rootPath.Contains(Path.DirectorySeparatorChar))
{
rootPath = rootPath.Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
}
var path = fullPath.EndsWith(separator) ? fullPath.Substring(0, fullPath.Length - 1) : fullPath;
var root = rootPath.EndsWith(separator) ? rootPath.Substring(0, rootPath.Length - 1) : rootPath;
var paths = new List<string>();
while (Path.GetDirectoryName(path) != Path.GetDirectoryName(root))
{
var folder = new DirectoryInfo(path).Name;
paths.Add(folder);
path = path.Replace(separator + folder, string.Empty);
}
return paths;
}
public bool Exists(string directory)
{
var di = new DirectoryInfo(directory);