Skip to content

Commit

Permalink
fix: better media folder mapping logic
Browse files Browse the repository at this point in the history
Check all ≥101 (up from ≥100) locations for a match before deciding on a mapping between the media folder and import folder, instead of picking the first found match. This will help in cases where some files have multiple locations with the same file name but different relative paths.

Fixes #68
  • Loading branch information
revam committed Oct 27, 2024
1 parent 5bd8f17 commit 4a2eb04
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions Shokofin/Configuration/MediaFolderConfigurationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,10 @@ private async Task<MediaFolderConfiguration> CreateConfigurationForPath(Guid lib
mediaFolderConfig.ImportFolderRelativePath = string.Empty;
}
else {
var foundLocations = new List<(int, string)>();
var samplePaths = FileSystem.GetFilePaths(mediaFolder.Path, true)
.Where(path => NamingOptions.VideoFileExtensions.Contains(Path.GetExtension(path)))
.Take(100)
.Take(101) // 101 as a tie breaker
.ToList();

Logger.LogDebug("Asking remote server if it knows any of the {Count} sampled files in {Path}. (Library={LibraryId})", samplePaths.Count > 100 ? 100 : samplePaths.Count, mediaFolder.Path, libraryId);
Expand All @@ -416,9 +417,19 @@ private async Task<MediaFolderConfiguration> CreateConfigurationForPath(Guid lib
continue;

var fileLocation = fileLocations[0];
mediaFolderConfig.ImportFolderId = fileLocation.ImportFolderId;
mediaFolderConfig.ImportFolderRelativePath = fileLocation.RelativePath[..^partialPath.Length];
break;
foundLocations.Add((fileLocation.ImportFolderId, fileLocation.RelativePath[..^partialPath.Length]));
}

if (foundLocations.Count > 0) {
var groupedLocations = foundLocations
.GroupBy(x => x)
.ToDictionary(x => x.Key, x => x.Count());
foreach (var ((importFolderId, relativePath), count) in groupedLocations) {
Logger.LogDebug("Found {Count} hits for import folder {Id} at relative path {RelativePath}. (Library={LibraryId})", count, importFolderId, relativePath, libraryId);
}
(mediaFolderConfig.ImportFolderId, mediaFolderConfig.ImportFolderRelativePath) = groupedLocations
.MaxBy(x => x.Value)!
.Key;
}

try {
Expand Down

0 comments on commit 4a2eb04

Please sign in to comment.