From 4bc5059b1e81989f6203fe06182d0db6d4335bce Mon Sep 17 00:00:00 2001 From: Mikal Stordal Date: Thu, 28 Mar 2024 10:35:14 +0100 Subject: [PATCH] fix: move file lookup from the first loop when finding locations to the second loop when generating sym-links --- Shokofin/Resolvers/ShokoResolveManager.cs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/Shokofin/Resolvers/ShokoResolveManager.cs b/Shokofin/Resolvers/ShokoResolveManager.cs index 5d1d2471..e0bb7d9e 100644 --- a/Shokofin/Resolvers/ShokoResolveManager.cs +++ b/Shokofin/Resolvers/ShokoResolveManager.cs @@ -158,8 +158,10 @@ private void OnLibraryManagerItemRemoved(object? sender, ItemChangeEventArgs e) private async Task> GetImportFolderFiles(int importFolderId, string importFolderSubPath, string mediaFolderPath) { - Logger.LogDebug("Looking for recognised files within media folder… (ImportFolder={FolderId},RelativePath={RelativePath})", importFolderId, importFolderSubPath); + Logger.LogDebug("Looking up recognised files for media folder… (ImportFolder={FolderId},RelativePath={RelativePath})", importFolderId, importFolderSubPath); + var start = DateTime.UtcNow; var allFilesForImportFolder = (await ApiClient.GetFilesForImportFolder(importFolderId, importFolderSubPath)) + .AsParallel() .SelectMany(file => { var location = file.Locations @@ -169,21 +171,26 @@ private void OnLibraryManagerItemRemoved(object? sender, ItemChangeEventArgs e) return Array.Empty<(string sourceLocation, string fileId, string seriesId, string[] episodeIds)>(); var sourceLocation = Path.Join(mediaFolderPath, location.Path[importFolderSubPath.Length..]); - if (!File.Exists(sourceLocation)) - return Array.Empty<(string sourceLocation, string fileId, string seriesId, string[] episodeIds)>(); - return file.CrossReferences .Select(xref => (sourceLocation, fileId: file.Id.ToString(), seriesId: xref.Series.Shoko.ToString(), episodeIds: xref.Episodes.Select(e => e.Shoko.ToString()).ToArray())); }) .Where(tuple => !string.IsNullOrEmpty(tuple.sourceLocation)) .ToList(); - Logger.LogDebug("Found {FileCount} files to use within media folder at {Path} (ImportFolder={FolderId},RelativePath={RelativePath})", allFilesForImportFolder.Count, mediaFolderPath, importFolderId, importFolderSubPath); + var timeSpent = start - DateTime.UtcNow; + Logger.LogDebug( + "Found ≤{FileCount} files to potentially use within media folder at {Path} in {TimeSpan} (ImportFolder={FolderId},RelativePath={RelativePath})", + allFilesForImportFolder.Count, + mediaFolderPath, + timeSpent, + importFolderId, + importFolderSubPath + ); return allFilesForImportFolder; } private async Task GenerateSymbolicLinks(Folder mediaFolder, IReadOnlyList<(string sourceLocation, string fileId, string seriesId, string[] episodeIds)> files) { - Logger.LogInformation("Found {FileCount} recognised files to potentially use within media folder at {Path}", files.Count, mediaFolder.Path); + Logger.LogInformation("Creating structure for ≤{FileCount} files to potentially use within media folder at {Path}", files.Count, mediaFolder.Path); var start = DateTime.UtcNow; var skipped = 0; @@ -196,6 +203,9 @@ await Task.WhenAll(files await semaphore.WaitAsync(); try { + if (!File.Exists(tuple.sourceLocation)) + return; + var (sourceLocation, symbolicLink) = await GenerateLocationForFile(vfsPath, collectionType, tuple.sourceLocation, tuple.fileId, tuple.seriesId, tuple.episodeIds); // Skip any source files we weren't meant to have in the library. if (string.IsNullOrEmpty(sourceLocation))