diff --git a/API/Data/Repositories/SeriesRepository.cs b/API/Data/Repositories/SeriesRepository.cs index e7a465f20..741968d3a 100644 --- a/API/Data/Repositories/SeriesRepository.cs +++ b/API/Data/Repositories/SeriesRepository.cs @@ -140,7 +140,7 @@ public interface ISeriesRepository Task> GetWantToReadForUserAsync(int userId); Task IsSeriesInWantToRead(int userId, int seriesId); Task GetSeriesByFolderPath(string folder, SeriesIncludes includes = SeriesIncludes.None); - Task GetSeriesThatContainsLowestFolderPath(string folder, SeriesIncludes includes = SeriesIncludes.None); + Task GetSeriesThatContainsLowestFolderPath(string path, SeriesIncludes includes = SeriesIncludes.None); Task> GetAllSeriesByNameAsync(IList normalizedNames, int userId, SeriesIncludes includes = SeriesIncludes.None); Task GetFullSeriesByAnyName(string seriesName, string localizedName, int libraryId, MangaFormat format, bool withFullIncludes = true); @@ -1603,9 +1603,24 @@ public async Task> GetRediscover(int userId, int libraryId, .SingleOrDefaultAsync(); } - public async Task GetSeriesThatContainsLowestFolderPath(string folder, SeriesIncludes includes = SeriesIncludes.None) + public async Task GetSeriesThatContainsLowestFolderPath(string path, SeriesIncludes includes = SeriesIncludes.None) { - var normalized = Services.Tasks.Scanner.Parser.Parser.NormalizePath(folder); + // Check if the path ends with a file (has a file extension) + string directoryPath; + if (Path.HasExtension(path)) + { + // Remove the file part and get the directory path + directoryPath = Path.GetDirectoryName(path); + if (string.IsNullOrEmpty(directoryPath)) return null; + } + else + { + // Use the path as is if it doesn't end with a file + directoryPath = path; + } + + // Normalize the directory path + var normalized = Services.Tasks.Scanner.Parser.Parser.NormalizePath(directoryPath); if (string.IsNullOrEmpty(normalized)) return null; normalized = normalized.TrimEnd('/'); diff --git a/API/Services/TaskScheduler.cs b/API/Services/TaskScheduler.cs index fda3d923f..c49273508 100644 --- a/API/Services/TaskScheduler.cs +++ b/API/Services/TaskScheduler.cs @@ -282,20 +282,17 @@ public void ScanFolder(string folderPath, string originalPath, TimeSpan delay) { var normalizedFolder = Tasks.Scanner.Parser.Parser.NormalizePath(folderPath); var normalizedOriginal = Tasks.Scanner.Parser.Parser.NormalizePath(originalPath); + if (HasAlreadyEnqueuedTask(ScannerService.Name, "ScanFolder", [normalizedFolder, normalizedOriginal]) || HasAlreadyEnqueuedTask(ScannerService.Name, "ScanFolder", [normalizedFolder, string.Empty])) { - _logger.LogInformation("Skipped scheduling ScanFolder for {Folder} as a job already queued", - normalizedFolder); + _logger.LogTrace("Skipped scheduling ScanFolder for {Folder} as a job already queued", normalizedFolder); return; } // Not sure where we should put this code, but we can get a bunch of ScanFolders when original has slight variations, like // create a folder, add a new file, etc. All of these can be merged into just 1 request. - - - _logger.LogInformation("Scheduling ScanFolder for {Folder}", normalizedFolder); BackgroundJob.Schedule(() => _scannerService.ScanFolder(normalizedFolder, normalizedOriginal), delay); } diff --git a/API/Services/Tasks/Scanner/LibraryWatcher.cs b/API/Services/Tasks/Scanner/LibraryWatcher.cs index e429a5aed..d2e6437a3 100644 --- a/API/Services/Tasks/Scanner/LibraryWatcher.cs +++ b/API/Services/Tasks/Scanner/LibraryWatcher.cs @@ -278,7 +278,7 @@ public async Task ProcessChange(string filePath, bool isDirectoryChange = false) _logger.LogTrace("Folder path: {FolderPath}", fullPath); if (string.IsNullOrEmpty(fullPath)) { - _logger.LogTrace("[LibraryWatcher] Change from {FilePath} could not find root level folder, ignoring change", filePath); + _logger.LogInformation("[LibraryWatcher] Change from {FilePath} could not find root level folder, ignoring change", filePath); return; } diff --git a/API/Services/Tasks/ScannerService.cs b/API/Services/Tasks/ScannerService.cs index 3046bc9b2..5dab902be 100644 --- a/API/Services/Tasks/ScannerService.cs +++ b/API/Services/Tasks/ScannerService.cs @@ -157,15 +157,14 @@ await _unitOfWork.SeriesRepository.GetSeriesByFolderPath(originalPath, SeriesInc } } - // TODO: Figure out why we have the library type restriction here - if (series != null)// && series.Library.Type is not (LibraryType.Book or LibraryType.LightNovel) + if (series != null) { if (TaskScheduler.HasScanTaskRunningForSeries(series.Id)) { _logger.LogDebug("[ScannerService] Scan folder invoked for {Folder} but a task is already queued for this series. Dropping request", folder); return; } - _logger.LogInformation("[ScannerService] Scan folder invoked for {Folder}, Series matched to folder and ScanSeries enqueued for 1 minute", folder); + _logger.LogDebug("[ScannerService] Scan folder invoked for {Folder}, Series matched to folder and ScanSeries enqueued for 1 minute", folder); BackgroundJob.Schedule(() => ScanSeries(series.Id, true), TimeSpan.FromMinutes(1)); return; } @@ -227,12 +226,14 @@ public async Task ScanSeries(int seriesId, bool bypassFolderOptimizationChecks = return; } + // TODO: We need to refactor this to handle the path changes better var folderPath = series.LowestFolderPath ?? series.FolderPath; if (string.IsNullOrEmpty(folderPath) || !_directoryService.Exists(folderPath)) { // We don't care if it's multiple due to new scan loop enforcing all in one root directory var files = await _unitOfWork.SeriesRepository.GetFilesForSeries(seriesId); - var seriesDirs = _directoryService.FindHighestDirectoriesFromFiles(libraryPaths, files.Select(f => f.FilePath).ToList()); + var seriesDirs = _directoryService.FindHighestDirectoriesFromFiles(libraryPaths, + files.Select(f => f.FilePath).ToList()); if (seriesDirs.Keys.Count == 0) { _logger.LogCritical("Scan Series has files spread outside a main series folder. Defaulting to library folder (this is expensive)"); @@ -259,7 +260,6 @@ public async Task ScanSeries(int seriesId, bool bypassFolderOptimizationChecks = } // If the series path doesn't exist anymore, it was either moved or renamed. We need to essentially delete it - //var parsedSeries = new Dictionary>(); await _eventHub.SendMessageAsync(MessageFactory.NotificationProgress, MessageFactory.LibraryScanProgressEvent(library.Name, ProgressEventType.Started, series.Name, 1)); @@ -268,13 +268,8 @@ await _eventHub.SendMessageAsync(MessageFactory.NotificationProgress, var (scanElapsedTime, parsedSeries) = await ScanFiles(library, [folderPath], false, true); - // // Transform seen series into the parsedSeries (I think we can actually just have processedSeries be used instead - // var parsedSeries = TrackFoundSeriesAndFiles(processedSeries); - _logger.LogInformation("ScanFiles for {Series} took {Time} milliseconds", series.Name, scanElapsedTime); - // We now technically have all scannedSeries, we could invoke each Series to be scanned - // Remove any parsedSeries keys that don't belong to our series. This can occur when users store 2 series in the same folder RemoveParsedInfosNotForSeries(parsedSeries, series); @@ -357,6 +352,7 @@ private static Dictionary> TrackFoundSeriesAndFi private async Task ShouldScanSeries(int seriesId, Library library, IList libraryPaths, Series series, bool bypassFolderChecks = false) { + var seriesFolderPaths = (await _unitOfWork.SeriesRepository.GetFilesForSeries(seriesId)) .Select(f => _directoryService.FileSystem.FileInfo.New(f.FilePath).Directory?.FullName ?? string.Empty) .Where(f => !string.IsNullOrEmpty(f))