< Summary - Jellyfin

Information
Class: Emby.Server.Implementations.Library.Resolvers.Audio.AudioResolver
Assembly: Emby.Server.Implementations
File(s): /srv/git/jellyfin/Emby.Server.Implementations/Library/Resolvers/Audio/AudioResolver.cs
Line coverage
90%
Covered lines: 73
Uncovered lines: 8
Coverable lines: 81
Total lines: 240
Line coverage: 90.1%
Branch coverage
75%
Covered branches: 42
Total branches: 56
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_Priority()100%11100%
ResolveMultiple(...)25%5.02460%
ResolveMultipleInternal(...)50%2.15266.66%
Resolve(...)65%24.922076.92%
FindAudioBook(...)100%66100%
ResolveMultipleAudio(...)85%2020100%
ContainsFile(...)100%11100%
ContainsFile(...)100%44100%
ContainsFile(...)100%11100%

File(s)

/srv/git/jellyfin/Emby.Server.Implementations/Library/Resolvers/Audio/AudioResolver.cs

#LineLine coverage
 1#nullable disable
 2
 3#pragma warning disable CS1591
 4
 5using System;
 6using System.Collections.Generic;
 7using System.IO;
 8using System.Linq;
 9using Emby.Naming.Audio;
 10using Emby.Naming.AudioBook;
 11using Emby.Naming.Common;
 12using Emby.Naming.Video;
 13using Jellyfin.Data.Enums;
 14using MediaBrowser.Controller.Entities;
 15using MediaBrowser.Controller.Library;
 16using MediaBrowser.Controller.Providers;
 17using MediaBrowser.Controller.Resolvers;
 18using MediaBrowser.Model.IO;
 19
 20namespace Emby.Server.Implementations.Library.Resolvers.Audio
 21{
 22    /// <summary>
 23    /// Class AudioResolver.
 24    /// </summary>
 25    public class AudioResolver : ItemResolver<MediaBrowser.Controller.Entities.Audio.Audio>, IMultiItemResolver
 26    {
 27        private readonly NamingOptions _namingOptions;
 28
 4729        public AudioResolver(NamingOptions namingOptions)
 30        {
 4731            _namingOptions = namingOptions;
 4732        }
 33
 34        /// <summary>
 35        /// Gets the priority.
 36        /// </summary>
 37        /// <value>The priority.</value>
 2938        public override ResolverPriority Priority => ResolverPriority.Fifth;
 39
 40        public MultiItemResolverResult ResolveMultiple(
 41            Folder parent,
 42            List<FileSystemMetadata> files,
 43            CollectionType? collectionType,
 44            IDirectoryService directoryService)
 45        {
 6146            var result = ResolveMultipleInternal(parent, files, collectionType);
 47
 6148            if (result is not null)
 49            {
 050                foreach (var item in result.Items)
 51                {
 052                    SetInitialItemValues((MediaBrowser.Controller.Entities.Audio.Audio)item, null);
 53                }
 54            }
 55
 6156            return result;
 57        }
 58
 59        private MultiItemResolverResult ResolveMultipleInternal(
 60            Folder parent,
 61            List<FileSystemMetadata> files,
 62            CollectionType? collectionType)
 63        {
 6164            if (collectionType == CollectionType.books)
 65            {
 066                return ResolveMultipleAudio(parent, files, true);
 67            }
 68
 6169            return null;
 70        }
 71
 72        /// <summary>
 73        /// Resolves the specified args.
 74        /// </summary>
 75        /// <param name="args">The args.</param>
 76        /// <returns>Entities.Audio.Audio.</returns>
 77        protected override MediaBrowser.Controller.Entities.Audio.Audio Resolve(ItemResolveArgs args)
 78        {
 79            // Return audio if the path is a file and has a matching extension
 80
 2381            var collectionType = args.GetCollectionType();
 82
 2383            var isBooksCollectionType = collectionType == CollectionType.books;
 84
 2385            if (args.IsDirectory)
 86            {
 1887                if (!isBooksCollectionType)
 88                {
 089                    return null;
 90                }
 91
 1892                return FindAudioBook(args, false);
 93            }
 94
 595            if (AudioFileParser.IsAudioFile(args.Path, _namingOptions))
 96            {
 297                var extension = Path.GetExtension(args.Path.AsSpan());
 98
 299                if (extension.Equals(".cue", StringComparison.OrdinalIgnoreCase))
 100                {
 101                    // if audio file exists of same name, return null
 0102                    return null;
 103                }
 104
 2105                var isMixedCollectionType = collectionType is null;
 106
 107                // For conflicting extensions, give priority to videos
 2108                if (isMixedCollectionType && VideoResolver.IsVideoFile(args.Path, _namingOptions))
 109                {
 0110                    return null;
 111                }
 112
 2113                MediaBrowser.Controller.Entities.Audio.Audio item = null;
 114
 2115                var isMusicCollectionType = collectionType == CollectionType.music;
 116
 117                // Use regular audio type for mixed libraries, owned items and music
 2118                if (isMixedCollectionType ||
 2119                    args.Parent is null ||
 2120                    isMusicCollectionType)
 121                {
 2122                    item = new MediaBrowser.Controller.Entities.Audio.Audio();
 123                }
 0124                else if (isBooksCollectionType)
 125                {
 0126                    item = new AudioBook();
 127                }
 128
 2129                if (item is not null)
 130                {
 2131                    item.IsShortcut = extension.Equals(".strm", StringComparison.OrdinalIgnoreCase);
 132
 2133                    item.IsInMixedFolder = true;
 134                }
 135
 2136                return item;
 137            }
 138
 3139            return null;
 140        }
 141
 142        private AudioBook FindAudioBook(ItemResolveArgs args, bool parseName)
 143        {
 144            // TODO: Allow GetMultiDiscMovie in here
 18145            var result = ResolveMultipleAudio(args.Parent, args.GetActualFileSystemChildren(), parseName);
 146
 18147            if (result is null || result.Items.Count != 1 || result.Items[0] is not AudioBook item)
 148            {
 12149                return null;
 150            }
 151
 152            // If we were supporting this we'd be checking filesFromOtherItems
 6153            item.IsInMixedFolder = false;
 6154            item.Name = Path.GetFileName(item.ContainingFolderPath);
 6155            return item;
 156        }
 157
 158        private MultiItemResolverResult ResolveMultipleAudio(Folder parent, IEnumerable<FileSystemMetadata> fileSystemEn
 159        {
 18160            var files = new List<FileSystemMetadata>();
 18161            var leftOver = new List<FileSystemMetadata>();
 162
 163            // Loop through each child file/folder and see if we find a video
 94164            foreach (var child in fileSystemEntries)
 165            {
 29166                if (child.IsDirectory)
 167                {
 2168                    leftOver.Add(child);
 169                }
 170                else
 171                {
 27172                    files.Add(child);
 173                }
 174            }
 175
 18176            var resolver = new AudioBookListResolver(_namingOptions);
 18177            var resolverResult = resolver.Resolve(files).ToList();
 178
 18179            var result = new MultiItemResolverResult
 18180            {
 18181                ExtraFiles = leftOver,
 18182                Items = new List<BaseItem>()
 18183            };
 184
 18185            var isInMixedFolder = resolverResult.Count > 1 || (parent is not null && parent.IsTopParent);
 186
 66187            foreach (var resolvedItem in resolverResult)
 188            {
 15189                if (resolvedItem.Files.Count > 1)
 190                {
 191                    // For now, until we sort out naming for multi-part books
 192                    continue;
 193                }
 194
 195                // Until multi-part books are handled letting files stack hides them from browsing in the client
 10196                if (resolvedItem.Files.Count == 0 || resolvedItem.Extras.Count > 0 || resolvedItem.AlternateVersions.Cou
 197                {
 198                    continue;
 199                }
 200
 6201                var firstMedia = resolvedItem.Files[0];
 202
 6203                var libraryItem = new AudioBook
 6204                {
 6205                    Path = firstMedia.Path,
 6206                    IsInMixedFolder = isInMixedFolder,
 6207                    ProductionYear = resolvedItem.Year,
 6208                    Name = parseName ?
 6209                        resolvedItem.Name :
 6210                        Path.GetFileNameWithoutExtension(firstMedia.Path),
 6211                    // AdditionalParts = resolvedItem.Files.Skip(1).Select(i => i.Path).ToArray(),
 6212                    // LocalAlternateVersions = resolvedItem.AlternateVersions.Select(i => i.Path).ToArray()
 6213                };
 214
 6215                result.Items.Add(libraryItem);
 216            }
 217
 18218            result.ExtraFiles.AddRange(files.Where(i => !ContainsFile(resolverResult, i)));
 219
 18220            return result;
 221        }
 222
 223        private static bool ContainsFile(IEnumerable<AudioBookInfo> result, FileSystemMetadata file)
 224        {
 27225            return result.Any(i => ContainsFile(i, file));
 226        }
 227
 228        private static bool ContainsFile(AudioBookInfo result, FileSystemMetadata file)
 229        {
 26230            return result.Files.Any(i => ContainsFile(i, file)) ||
 26231                result.AlternateVersions.Any(i => ContainsFile(i, file)) ||
 26232                result.Extras.Any(i => ContainsFile(i, file));
 233        }
 234
 235        private static bool ContainsFile(AudioBookFileInfo result, FileSystemMetadata file)
 236        {
 35237            return string.Equals(result.Path, file.FullName, StringComparison.OrdinalIgnoreCase);
 238        }
 239    }
 240}