| | | 1 | | #nullable disable |
| | | 2 | | |
| | | 3 | | #pragma warning disable CS1591 |
| | | 4 | | |
| | | 5 | | using System; |
| | | 6 | | using System.Collections.Generic; |
| | | 7 | | using System.IO; |
| | | 8 | | using System.Linq; |
| | | 9 | | using Emby.Naming.Audio; |
| | | 10 | | using Emby.Naming.AudioBook; |
| | | 11 | | using Emby.Naming.Common; |
| | | 12 | | using Emby.Naming.Video; |
| | | 13 | | using Jellyfin.Data.Enums; |
| | | 14 | | using MediaBrowser.Controller.Entities; |
| | | 15 | | using MediaBrowser.Controller.Library; |
| | | 16 | | using MediaBrowser.Controller.Providers; |
| | | 17 | | using MediaBrowser.Controller.Resolvers; |
| | | 18 | | using MediaBrowser.Model.IO; |
| | | 19 | | |
| | | 20 | | namespace 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 | | |
| | 46 | 29 | | public AudioResolver(NamingOptions namingOptions) |
| | | 30 | | { |
| | 46 | 31 | | _namingOptions = namingOptions; |
| | 46 | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Gets the priority. |
| | | 36 | | /// </summary> |
| | | 37 | | /// <value>The priority.</value> |
| | 21 | 38 | | 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 | | { |
| | 60 | 46 | | var result = ResolveMultipleInternal(parent, files, collectionType); |
| | | 47 | | |
| | 60 | 48 | | if (result is not null) |
| | | 49 | | { |
| | 0 | 50 | | foreach (var item in result.Items) |
| | | 51 | | { |
| | 0 | 52 | | SetInitialItemValues((MediaBrowser.Controller.Entities.Audio.Audio)item, null); |
| | | 53 | | } |
| | | 54 | | } |
| | | 55 | | |
| | 60 | 56 | | return result; |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | private MultiItemResolverResult ResolveMultipleInternal( |
| | | 60 | | Folder parent, |
| | | 61 | | List<FileSystemMetadata> files, |
| | | 62 | | CollectionType? collectionType) |
| | | 63 | | { |
| | 60 | 64 | | if (collectionType == CollectionType.books) |
| | | 65 | | { |
| | 0 | 66 | | return ResolveMultipleAudio(parent, files, true); |
| | | 67 | | } |
| | | 68 | | |
| | 60 | 69 | | 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 | | |
| | 30 | 81 | | var collectionType = args.GetCollectionType(); |
| | | 82 | | |
| | 30 | 83 | | var isBooksCollectionType = collectionType == CollectionType.books; |
| | | 84 | | |
| | 30 | 85 | | if (args.IsDirectory) |
| | | 86 | | { |
| | 18 | 87 | | if (!isBooksCollectionType) |
| | | 88 | | { |
| | 0 | 89 | | return null; |
| | | 90 | | } |
| | | 91 | | |
| | 18 | 92 | | return FindAudioBook(args, false); |
| | | 93 | | } |
| | | 94 | | |
| | 12 | 95 | | if (AudioFileParser.IsAudioFile(args.Path, _namingOptions)) |
| | | 96 | | { |
| | 2 | 97 | | var extension = Path.GetExtension(args.Path.AsSpan()); |
| | | 98 | | |
| | 2 | 99 | | if (extension.Equals(".cue", StringComparison.OrdinalIgnoreCase)) |
| | | 100 | | { |
| | | 101 | | // if audio file exists of same name, return null |
| | 0 | 102 | | return null; |
| | | 103 | | } |
| | | 104 | | |
| | 2 | 105 | | var isMixedCollectionType = collectionType is null; |
| | | 106 | | |
| | | 107 | | // For conflicting extensions, give priority to videos |
| | 2 | 108 | | if (isMixedCollectionType && VideoResolver.IsVideoFile(args.Path, _namingOptions)) |
| | | 109 | | { |
| | 0 | 110 | | return null; |
| | | 111 | | } |
| | | 112 | | |
| | 2 | 113 | | MediaBrowser.Controller.Entities.Audio.Audio item = null; |
| | | 114 | | |
| | 2 | 115 | | var isMusicCollectionType = collectionType == CollectionType.music; |
| | | 116 | | |
| | | 117 | | // Use regular audio type for mixed libraries, owned items and music |
| | 2 | 118 | | if (isMixedCollectionType || |
| | 2 | 119 | | args.Parent is null || |
| | 2 | 120 | | isMusicCollectionType) |
| | | 121 | | { |
| | 2 | 122 | | item = new MediaBrowser.Controller.Entities.Audio.Audio(); |
| | | 123 | | } |
| | 0 | 124 | | else if (isBooksCollectionType) |
| | | 125 | | { |
| | 0 | 126 | | item = new AudioBook(); |
| | | 127 | | } |
| | | 128 | | |
| | 2 | 129 | | if (item is not null) |
| | | 130 | | { |
| | 2 | 131 | | item.IsShortcut = extension.Equals(".strm", StringComparison.OrdinalIgnoreCase); |
| | | 132 | | |
| | 2 | 133 | | item.IsInMixedFolder = true; |
| | | 134 | | } |
| | | 135 | | |
| | 2 | 136 | | return item; |
| | | 137 | | } |
| | | 138 | | |
| | 10 | 139 | | return null; |
| | | 140 | | } |
| | | 141 | | |
| | | 142 | | private AudioBook FindAudioBook(ItemResolveArgs args, bool parseName) |
| | | 143 | | { |
| | | 144 | | // TODO: Allow GetMultiDiscMovie in here |
| | 18 | 145 | | var result = ResolveMultipleAudio(args.Parent, args.GetActualFileSystemChildren(), parseName); |
| | | 146 | | |
| | 18 | 147 | | if (result is null || result.Items.Count != 1 || result.Items[0] is not AudioBook item) |
| | | 148 | | { |
| | 12 | 149 | | return null; |
| | | 150 | | } |
| | | 151 | | |
| | | 152 | | // If we were supporting this we'd be checking filesFromOtherItems |
| | 6 | 153 | | item.IsInMixedFolder = false; |
| | 6 | 154 | | item.Name = Path.GetFileName(item.ContainingFolderPath); |
| | 6 | 155 | | return item; |
| | | 156 | | } |
| | | 157 | | |
| | | 158 | | private MultiItemResolverResult ResolveMultipleAudio(Folder parent, IEnumerable<FileSystemMetadata> fileSystemEn |
| | | 159 | | { |
| | 18 | 160 | | var files = new List<FileSystemMetadata>(); |
| | 18 | 161 | | var leftOver = new List<FileSystemMetadata>(); |
| | | 162 | | |
| | | 163 | | // Loop through each child file/folder and see if we find a video |
| | 94 | 164 | | foreach (var child in fileSystemEntries) |
| | | 165 | | { |
| | 29 | 166 | | if (child.IsDirectory) |
| | | 167 | | { |
| | 2 | 168 | | leftOver.Add(child); |
| | | 169 | | } |
| | | 170 | | else |
| | | 171 | | { |
| | 27 | 172 | | files.Add(child); |
| | | 173 | | } |
| | | 174 | | } |
| | | 175 | | |
| | 18 | 176 | | var resolver = new AudioBookListResolver(_namingOptions); |
| | 18 | 177 | | var resolverResult = resolver.Resolve(files).ToList(); |
| | | 178 | | |
| | 18 | 179 | | var result = new MultiItemResolverResult |
| | 18 | 180 | | { |
| | 18 | 181 | | ExtraFiles = leftOver, |
| | 18 | 182 | | Items = new List<BaseItem>() |
| | 18 | 183 | | }; |
| | | 184 | | |
| | 18 | 185 | | var isInMixedFolder = resolverResult.Count > 1 || (parent is not null && parent.IsTopParent); |
| | | 186 | | |
| | 66 | 187 | | foreach (var resolvedItem in resolverResult) |
| | | 188 | | { |
| | 15 | 189 | | 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 |
| | 10 | 196 | | if (resolvedItem.Files.Count == 0 || resolvedItem.Extras.Count > 0 || resolvedItem.AlternateVersions.Cou |
| | | 197 | | { |
| | | 198 | | continue; |
| | | 199 | | } |
| | | 200 | | |
| | 6 | 201 | | var firstMedia = resolvedItem.Files[0]; |
| | | 202 | | |
| | 6 | 203 | | var libraryItem = new AudioBook |
| | 6 | 204 | | { |
| | 6 | 205 | | Path = firstMedia.Path, |
| | 6 | 206 | | IsInMixedFolder = isInMixedFolder, |
| | 6 | 207 | | ProductionYear = resolvedItem.Year, |
| | 6 | 208 | | Name = parseName ? |
| | 6 | 209 | | resolvedItem.Name : |
| | 6 | 210 | | Path.GetFileNameWithoutExtension(firstMedia.Path), |
| | 6 | 211 | | // AdditionalParts = resolvedItem.Files.Skip(1).Select(i => i.Path).ToArray(), |
| | 6 | 212 | | // LocalAlternateVersions = resolvedItem.AlternateVersions.Select(i => i.Path).ToArray() |
| | 6 | 213 | | }; |
| | | 214 | | |
| | 6 | 215 | | result.Items.Add(libraryItem); |
| | | 216 | | } |
| | | 217 | | |
| | 18 | 218 | | result.ExtraFiles.AddRange(files.Where(i => !ContainsFile(resolverResult, i))); |
| | | 219 | | |
| | 18 | 220 | | return result; |
| | | 221 | | } |
| | | 222 | | |
| | | 223 | | private static bool ContainsFile(IEnumerable<AudioBookInfo> result, FileSystemMetadata file) |
| | | 224 | | { |
| | 27 | 225 | | return result.Any(i => ContainsFile(i, file)); |
| | | 226 | | } |
| | | 227 | | |
| | | 228 | | private static bool ContainsFile(AudioBookInfo result, FileSystemMetadata file) |
| | | 229 | | { |
| | 26 | 230 | | return result.Files.Any(i => ContainsFile(i, file)) || |
| | 26 | 231 | | result.AlternateVersions.Any(i => ContainsFile(i, file)) || |
| | 26 | 232 | | result.Extras.Any(i => ContainsFile(i, file)); |
| | | 233 | | } |
| | | 234 | | |
| | | 235 | | private static bool ContainsFile(AudioBookFileInfo result, FileSystemMetadata file) |
| | | 236 | | { |
| | 35 | 237 | | return string.Equals(result.Path, file.FullName, StringComparison.OrdinalIgnoreCase); |
| | | 238 | | } |
| | | 239 | | } |
| | | 240 | | } |