diff options
Diffstat (limited to 'Emby.Server.Implementations/Library')
5 files changed, 166 insertions, 35 deletions
diff --git a/Emby.Server.Implementations/Library/DotIgnoreIgnoreRule.cs b/Emby.Server.Implementations/Library/DotIgnoreIgnoreRule.cs index 023c1e8915..0d63049372 100644 --- a/Emby.Server.Implementations/Library/DotIgnoreIgnoreRule.cs +++ b/Emby.Server.Implementations/Library/DotIgnoreIgnoreRule.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; using System.Text.RegularExpressions; using BitFaster.Caching.Lru; using MediaBrowser.Controller.Entities; @@ -16,10 +17,14 @@ namespace Emby.Server.Implementations.Library; public class DotIgnoreIgnoreRule : IResolverIgnoreRule { private static readonly bool IsWindows = OperatingSystem.IsWindows(); + private static readonly StringComparison PathComparison = IsWindows ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal; private readonly FastConcurrentLru<string, IgnoreFileCacheEntry> _directoryCache; private readonly FastConcurrentLru<string, ParsedIgnoreCacheEntry> _rulesCache; + private Func<IEnumerable<string>>? _libraryRootsProvider; + private volatile string[]? _libraryRoots; + /// <summary> /// Initializes a new instance of the <see cref="DotIgnoreIgnoreRule"/> class. /// </summary> @@ -40,11 +45,23 @@ public class DotIgnoreIgnoreRule : IResolverIgnoreRule public bool ShouldIgnore(FileSystemMetadata fileInfo, BaseItem? parent) => IsIgnoredInternal(fileInfo, parent); /// <summary> - /// Clears the directory lookup cache. The parsed rules cache is not cleared + /// Sets the provider of library root paths. The search for a .ignore file stops at the + /// outermost library root containing the path instead of walking up to the filesystem root. + /// </summary> + /// <param name="libraryRootsProvider">The library root paths provider.</param> + public void SetLibraryRootsProvider(Func<IEnumerable<string>> libraryRootsProvider) + { + _libraryRootsProvider = libraryRootsProvider; + ClearDirectoryCache(); + } + + /// <summary> + /// Clears the directory lookup cache and the library roots. The parsed rules cache is not cleared /// as it validates file modification time on each access. /// </summary> public void ClearDirectoryCache() { + _libraryRoots = null; _directoryCache.Clear(); } @@ -164,7 +181,9 @@ public class DotIgnoreIgnoreRule : IResolverIgnoreRule return null; } - // Walk up the directory tree to find .ignore file using DirectoryInfo.Parent + // Walk up the directory tree to find .ignore file using DirectoryInfo.Parent, + // never leaving the library: probing above it can hit autofs or network mounts (#17364) + var stopDirectory = GetOutermostLibraryRoot(startDir.FullName); var checkedDirs = new List<string> { directory }; for (var current = startDir; current is not null; current = current.Parent) @@ -203,6 +222,11 @@ public class DotIgnoreIgnoreRule : IResolverIgnoreRule { checkedDirs.Add(currentPath); } + + if (stopDirectory is not null && IsSamePath(currentPath, stopDirectory)) + { + break; + } } // No .ignore file found - cache null result for all directories @@ -215,6 +239,58 @@ public class DotIgnoreIgnoreRule : IResolverIgnoreRule return null; } + private string? GetOutermostLibraryRoot(string path) + { + string? outermost = null; + foreach (var root in GetLibraryRoots()) + { + if ((outermost is null || root.Length < outermost.Length) + && IsSameOrChildPath(path, root)) + { + outermost = root; + } + } + + return outermost; + } + + private string[] GetLibraryRoots() + { + var roots = _libraryRoots; + if (roots is not null) + { + return roots; + } + + var provider = _libraryRootsProvider; + if (provider is null) + { + return []; + } + + roots = provider() + .Where(r => !string.IsNullOrEmpty(r)) + .Select(r => Path.TrimEndingDirectorySeparator(Path.GetFullPath(r))) + .ToArray(); + _libraryRoots = roots; + return roots; + } + + private static bool IsSameOrChildPath(string path, string root) + { + if (IsSamePath(path, root)) + { + return true; + } + + // A filesystem root such as "/" keeps its trailing separator after trimming + return path.StartsWith(root, PathComparison) + && (Path.EndsInDirectorySeparator(root) || path[root.Length] == Path.DirectorySeparatorChar); + } + + private static bool IsSamePath(string path1, string path2) + => Path.TrimEndingDirectorySeparator(path1.AsSpan()).Equals(Path.TrimEndingDirectorySeparator(path2.AsSpan()), PathComparison); + private ParsedIgnoreCacheEntry? GetParsedRules(FileInfo ignoreFile) { if (!ignoreFile.Exists) diff --git a/Emby.Server.Implementations/Library/LibraryManager.cs b/Emby.Server.Implementations/Library/LibraryManager.cs index 16616dd328..359b0f1d0c 100644 --- a/Emby.Server.Implementations/Library/LibraryManager.cs +++ b/Emby.Server.Implementations/Library/LibraryManager.cs @@ -184,6 +184,7 @@ namespace Emby.Server.Implementations.Library _peopleRepository = peopleRepository; _pathManager = pathManager; _dotIgnoreIgnoreRule = dotIgnoreIgnoreRule; + _dotIgnoreIgnoreRule.SetLibraryRootsProvider(GetLibraryRootPaths); _localization = localization; _directoryService = directoryService; _extraResolver = new ExtraResolver(loggerFactory.CreateLogger<ExtraResolver>(), namingOptions, directoryService); @@ -1627,23 +1628,7 @@ namespace Emby.Server.Implementations.Library { Name = Path.GetFileName(dir), - Locations = _fileSystem.GetFilePaths(dir, false) - .Where(i => Path.GetExtension(i.AsSpan()).Equals(ShortcutFileExtension, StringComparison.OrdinalIgnoreCase)) - .Select(i => - { - try - { - return _appHost.ExpandVirtualPath(_fileSystem.ResolveShortcut(i)); - } - catch (Exception ex) - { - _logger.LogError(ex, "Error resolving shortcut file {File}", i); - return null; - } - }) - .Where(i => i is not null) - .Order() - .ToArray(), + Locations = GetVirtualFolderLocations(dir).Order().ToArray(), CollectionType = GetCollectionType(dir) }; @@ -1671,6 +1656,40 @@ namespace Emby.Server.Implementations.Library return info; } + private IEnumerable<string> GetVirtualFolderLocations(string dir) + { + return _fileSystem.GetFilePaths(dir, false) + .Where(i => Path.GetExtension(i.AsSpan()).Equals(ShortcutFileExtension, StringComparison.OrdinalIgnoreCase)) + .Select(i => + { + try + { + return _appHost.ExpandVirtualPath(_fileSystem.ResolveShortcut(i)); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error resolving shortcut file {File}", i); + return null; + } + }) + .OfType<string>(); + } + + // Reads the .mblink files directly; going through RootFolder.Children would re-enter the resolver. + private IEnumerable<string> GetLibraryRootPaths() + { + // root/default only exists once the user root folder or a library has been created + var userViewsPath = _configurationManager.ApplicationPaths.DefaultUserViewsPath; + if (!_fileSystem.DirectoryExists(userViewsPath)) + { + return []; + } + + return _fileSystem.GetDirectoryPaths(userViewsPath) + .SelectMany(GetVirtualFolderLocations) + .ToList(); + } + private CollectionTypeOptions? GetCollectionType(string path) { var files = _fileSystem.GetFilePaths(path, [".collection"], true, false); @@ -3750,6 +3769,7 @@ namespace Emby.Server.Implementations.Library public async Task<ItemImageInfo> ConvertImageToLocal(BaseItem item, ItemImageInfo image, int imageIndex, bool removeOnFailure) { + HttpRequestException? lastHttpException = null; foreach (var url in image.Path.Split('|')) { try @@ -3781,6 +3801,7 @@ namespace Emby.Server.Implementations.Library && (ex.StatusCode.Value == HttpStatusCode.NotFound || ex.StatusCode.Value == HttpStatusCode.Forbidden)) { _logger.LogDebug(ex, "Error downloading image {Url}", url); + lastHttpException = ex; continue; } @@ -3795,13 +3816,17 @@ namespace Emby.Server.Implementations.Library await item.UpdateToRepositoryAsync(ItemUpdateType.ImageUpdate, CancellationToken.None).ConfigureAwait(false); } - throw new InvalidOperationException(string.Format( - CultureInfo.InvariantCulture, - "Unable to convert any {0} image url in \"{1}\" to a local file for {2} ({3})", - image.Type, - image.Path, - item.Name, - item.Id)); + // Keep the rejection as the inner exception: callers such as the Live TV guide need the + // status code to tell a missing image from a provider that is refusing us. + throw new InvalidOperationException( + string.Format( + CultureInfo.InvariantCulture, + "Unable to convert any {0} image url in \"{1}\" to a local file for {2} ({3})", + image.Type, + image.Path, + item.Name, + item.Id), + lastHttpException); } public async Task AddVirtualFolder(string name, CollectionTypeOptions? collectionType, LibraryOptions options, bool refreshLibrary) @@ -4200,6 +4225,30 @@ namespace Emby.Server.Implementations.Library } /// <inheritdoc /> + public IReadOnlyList<int> GetYears(InternalItemsQuery query) + { + if (query.User is not null) + { + AddUserToQuery(query, query.User); + } + + SetTopParentOrAncestorIds(query); + return _itemRepository.GetYears(query); + } + + /// <inheritdoc /> + public IReadOnlyList<string> GetOfficialRatings(InternalItemsQuery query) + { + if (query.User is not null) + { + AddUserToQuery(query, query.User); + } + + SetTopParentOrAncestorIds(query); + return _itemRepository.GetOfficialRatings(query); + } + + /// <inheritdoc /> public IReadOnlyList<string> GetTagNames(InternalItemsQuery query) { if (query.User is not null) diff --git a/Emby.Server.Implementations/Library/MediaSourceManager.cs b/Emby.Server.Implementations/Library/MediaSourceManager.cs index e9bba05839..4513cefde8 100644 --- a/Emby.Server.Implementations/Library/MediaSourceManager.cs +++ b/Emby.Server.Implementations/Library/MediaSourceManager.cs @@ -481,13 +481,7 @@ namespace Emby.Server.Implementations.Library return []; } - var culture = _localizationManager.FindLanguageInfo(language); - if (culture is not null) - { - return culture.Name.Contains('-', StringComparison.OrdinalIgnoreCase) ? [culture.Name] : culture.ThreeLetterISOLanguageNames; - } - - return [language]; + return _localizationManager.GetMediaStreamLanguageCodes(language); } private void SetDefaultSubtitleStreamIndex(MediaSourceInfo source, UserItemData userData, User user, bool allowRememberingSelection) diff --git a/Emby.Server.Implementations/Library/MediaStreamSelector.cs b/Emby.Server.Implementations/Library/MediaStreamSelector.cs index 631179ffcf..31e36af3b8 100644 --- a/Emby.Server.Implementations/Library/MediaStreamSelector.cs +++ b/Emby.Server.Implementations/Library/MediaStreamSelector.cs @@ -46,6 +46,7 @@ namespace Emby.Server.Implementations.Library .ThenByDescending(x => x.IsDefault) .ThenByDescending(x => !x.IsForced && MatchesPreferredLanguage(x.Language, preferredLanguages)) .ThenByDescending(x => x.IsForced && MatchesPreferredLanguage(x.Language, preferredLanguages)) + .ThenBy(x => GetLanguageRank(x.Language, preferredLanguages)) .ThenByDescending(x => x.IsForced && IsLanguageUndefined(x.Language)) .ThenByDescending(x => x.IsForced) .ToList(); @@ -158,6 +159,13 @@ namespace Emby.Server.Implementations.Library preferredLanguages.Contains(language, StringComparison.OrdinalIgnoreCase); } + // Earlier preferences win among matches: a pt-br preference lists [pt-br, por] and must pick the pt-br track. + private static int GetLanguageRank(string language, IReadOnlyList<string> preferredLanguages) + { + var index = preferredLanguages.FindIndex(x => string.Equals(x, language, StringComparison.OrdinalIgnoreCase)); + return index == -1 ? int.MaxValue : index; + } + private static bool IsLanguageUndefined(string language) { // Check for null, empty, or known placeholders diff --git a/Emby.Server.Implementations/Library/Search/SqlSearchProvider.cs b/Emby.Server.Implementations/Library/Search/SqlSearchProvider.cs index a5ea02df51..dd96f75893 100644 --- a/Emby.Server.Implementations/Library/Search/SqlSearchProvider.cs +++ b/Emby.Server.Implementations/Library/Search/SqlSearchProvider.cs @@ -136,7 +136,7 @@ public class SqlSearchProvider : IInternalSearchProvider dbQuery = ApplyTypeFilter(dbQuery, query.IncludeItemTypes, query.ExcludeItemTypes); dbQuery = ApplyMediaTypeFilter(dbQuery, query.MediaTypes); - dbQuery = ApplyParentFilter(dbQuery, query.ParentId); + dbQuery = ApplyParentFilter(dbContext, dbQuery, query.ParentId); dbQuery = ApplyUserAccessFilter(dbContext, dbQuery, query); dbQuery = ExcludeVersionsOfMatchedPrimaries(dbQuery); @@ -205,6 +205,7 @@ public class SqlSearchProvider : IInternalSearchProvider } private static IQueryable<BaseItemEntity> ApplyParentFilter( + JellyfinDbContext context, IQueryable<BaseItemEntity> query, Guid? parentId) { @@ -214,7 +215,10 @@ public class SqlSearchProvider : IInternalSearchProvider } var pid = parentId.Value; - return query.Where(e => e.ParentId == pid || e.Parents!.Any(p => p.ParentItemId == pid)); + var descendantIds = context.AncestorIds + .Where(a => a.ParentItemId == pid) + .Select(a => a.ItemId); + return query.Where(e => e.ParentId == pid || descendantIds.Contains(e.Id)); } private static IQueryable<BaseItemEntity> ExcludeVersionsOfMatchedPrimaries(IQueryable<BaseItemEntity> query) |
