Skip to content

Commit

Permalink
fix: move file lookup
Browse files Browse the repository at this point in the history
from the first loop when finding locations
to the second loop when generating sym-links
  • Loading branch information
revam committed Mar 28, 2024
1 parent a323527 commit 4bc5059
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions Shokofin/Resolvers/ShokoResolveManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,10 @@ private void OnLibraryManagerItemRemoved(object? sender, ItemChangeEventArgs e)

private async Task<IReadOnlyList<(string sourceLocation, string fileId, string seriesId, string[] episodeIds)>> 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
Expand All @@ -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;
Expand All @@ -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))
Expand Down

0 comments on commit 4bc5059

Please sign in to comment.