mirror of
https://github.com/Kareadita/Kavita.git
synced 2025-07-09 03:04:19 -04:00
Added a new method to search for files against multiple filters, rather than grabbing all files then filtering.
This commit is contained in:
parent
8c80ed090d
commit
49b4ee0022
@ -5,6 +5,7 @@ using System.Diagnostics;
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Security;
|
using System.Security;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using API.DTOs;
|
using API.DTOs;
|
||||||
@ -16,12 +17,33 @@ namespace API.Services
|
|||||||
public class DirectoryService : IDirectoryService
|
public class DirectoryService : IDirectoryService
|
||||||
{
|
{
|
||||||
private readonly ILogger<DirectoryService> _logger;
|
private readonly ILogger<DirectoryService> _logger;
|
||||||
|
private static readonly string MangaFileExtensions = @"\.cbz|\.cbr|\.png|\.jpeg|\.jpg|\.zip|\.rar";
|
||||||
|
|
||||||
public DirectoryService(ILogger<DirectoryService> logger)
|
public DirectoryService(ILogger<DirectoryService> logger)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Given a set of regex search criteria, get files in the given path.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="path">Directory to search</param>
|
||||||
|
/// <param name="searchPatternExpression">Regex version of search pattern (ie \.mp3|\.mp4)</param>
|
||||||
|
/// <param name="searchOption">SearchOption to use, defaults to TopDirectoryOnly</param>
|
||||||
|
/// <returns>List of file paths</returns>
|
||||||
|
public static IEnumerable<string> GetFiles(string path,
|
||||||
|
string searchPatternExpression = "",
|
||||||
|
SearchOption searchOption = SearchOption.TopDirectoryOnly)
|
||||||
|
{
|
||||||
|
Regex reSearchPattern = new Regex(searchPatternExpression, RegexOptions.IgnoreCase);
|
||||||
|
return Directory.EnumerateFiles(path, "*", searchOption)
|
||||||
|
.Where(file =>
|
||||||
|
reSearchPattern.IsMatch(Path.GetExtension(file)));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Lists out top-level folders for a given directory. Filters out System and Hidden folders.
|
/// Lists out top-level folders for a given directory. Filters out System and Hidden folders.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -48,10 +70,9 @@ namespace API.Services
|
|||||||
TraverseTreeParallelForEach(folderPath, (f) =>
|
TraverseTreeParallelForEach(folderPath, (f) =>
|
||||||
{
|
{
|
||||||
// Exceptions are no-ops.
|
// Exceptions are no-ops.
|
||||||
try {
|
try
|
||||||
// Do nothing with the data except read it.
|
{
|
||||||
//byte[] data = File.ReadAllBytes(f);
|
ProcessManga(folderPath, f);
|
||||||
ProcessManga(f);
|
|
||||||
}
|
}
|
||||||
catch (FileNotFoundException) {}
|
catch (FileNotFoundException) {}
|
||||||
catch (IOException) {}
|
catch (IOException) {}
|
||||||
@ -61,15 +82,18 @@ namespace API.Services
|
|||||||
Console.WriteLine(f);
|
Console.WriteLine(f);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
catch (ArgumentException) {
|
catch (ArgumentException ex) {
|
||||||
|
_logger.LogError(ex, "There was an issue scanning the directory");
|
||||||
_logger.LogError($"The directory '{folderPath}' does not exist");
|
_logger.LogError($"The directory '{folderPath}' does not exist");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void ProcessManga(string filename)
|
private static void ProcessManga(string folderPath, string filename)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"Found {filename}");
|
Console.WriteLine($"Found {filename}");
|
||||||
|
var series = Parser.Parser.ParseSeries(filename);
|
||||||
|
Console.WriteLine($"Series: {series}");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void TraverseTreeParallelForEach(string root, Action<string> action)
|
public static void TraverseTreeParallelForEach(string root, Action<string> action)
|
||||||
@ -109,7 +133,10 @@ namespace API.Services
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
files = Directory.GetFiles(currentDir);
|
//files = Directory.GetFiles(currentDir, "*.")
|
||||||
|
files = DirectoryService.GetFiles(currentDir, MangaFileExtensions)
|
||||||
|
.ToArray();
|
||||||
|
//files = Directory.GetFiles(currentDir);
|
||||||
}
|
}
|
||||||
catch (UnauthorizedAccessException e) {
|
catch (UnauthorizedAccessException e) {
|
||||||
Console.WriteLine(e.Message);
|
Console.WriteLine(e.Message);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user