From 4a2eb04a02698d7ba2e9d48016f838c4bcf19103 Mon Sep 17 00:00:00 2001 From: Mikal Stordal Date: Sun, 27 Oct 2024 23:14:56 +0100 Subject: [PATCH] fix: better media folder mapping logic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../MediaFolderConfigurationService.cs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/Shokofin/Configuration/MediaFolderConfigurationService.cs b/Shokofin/Configuration/MediaFolderConfigurationService.cs index c9a83bc7..51854e92 100644 --- a/Shokofin/Configuration/MediaFolderConfigurationService.cs +++ b/Shokofin/Configuration/MediaFolderConfigurationService.cs @@ -394,9 +394,10 @@ private async Task 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); @@ -416,9 +417,19 @@ private async Task 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 {