|  |  | 1 |  | #nullable disable | 
|  |  | 2 |  |  | 
|  |  | 3 |  | using System; | 
|  |  | 4 |  | using System.Collections.Generic; | 
|  |  | 5 |  | using System.IO; | 
|  |  | 6 |  | using System.Linq; | 
|  |  | 7 |  | using System.Threading; | 
|  |  | 8 |  | using System.Threading.Tasks; | 
|  |  | 9 |  | using Emby.Naming.Audio; | 
|  |  | 10 |  | using Emby.Naming.Common; | 
|  |  | 11 |  | using Jellyfin.Data.Enums; | 
|  |  | 12 |  | using MediaBrowser.Controller.Entities.Audio; | 
|  |  | 13 |  | using MediaBrowser.Controller.Library; | 
|  |  | 14 |  | using MediaBrowser.Controller.Providers; | 
|  |  | 15 |  | using MediaBrowser.Controller.Resolvers; | 
|  |  | 16 |  | using MediaBrowser.Model.IO; | 
|  |  | 17 |  | using Microsoft.Extensions.Logging; | 
|  |  | 18 |  |  | 
|  |  | 19 |  | namespace Emby.Server.Implementations.Library.Resolvers.Audio | 
|  |  | 20 |  | { | 
|  |  | 21 |  |     /// <summary> | 
|  |  | 22 |  |     /// The music album resolver. | 
|  |  | 23 |  |     /// </summary> | 
|  |  | 24 |  |     public class MusicAlbumResolver : ItemResolver<MusicAlbum> | 
|  |  | 25 |  |     { | 
|  |  | 26 |  |         private readonly ILogger<MusicAlbumResolver> _logger; | 
|  |  | 27 |  |         private readonly NamingOptions _namingOptions; | 
|  |  | 28 |  |         private readonly IDirectoryService _directoryService; | 
|  |  | 29 |  |  | 
|  |  | 30 |  |         /// <summary> | 
|  |  | 31 |  |         /// Initializes a new instance of the <see cref="MusicAlbumResolver"/> class. | 
|  |  | 32 |  |         /// </summary> | 
|  |  | 33 |  |         /// <param name="logger">The logger.</param> | 
|  |  | 34 |  |         /// <param name="namingOptions">The naming options.</param> | 
|  |  | 35 |  |         /// <param name="directoryService">The directory service.</param> | 
|  | 21 | 36 |  |         public MusicAlbumResolver(ILogger<MusicAlbumResolver> logger, NamingOptions namingOptions, IDirectoryService dir | 
|  |  | 37 |  |         { | 
|  | 21 | 38 |  |             _logger = logger; | 
|  | 21 | 39 |  |             _namingOptions = namingOptions; | 
|  | 21 | 40 |  |             _directoryService = directoryService; | 
|  | 21 | 41 |  |         } | 
|  |  | 42 |  |  | 
|  |  | 43 |  |         /// <summary> | 
|  |  | 44 |  |         /// Gets the priority. | 
|  |  | 45 |  |         /// </summary> | 
|  |  | 46 |  |         /// <value>The priority.</value> | 
|  | 21 | 47 |  |         public override ResolverPriority Priority => ResolverPriority.Third; | 
|  |  | 48 |  |  | 
|  |  | 49 |  |         /// <summary> | 
|  |  | 50 |  |         /// Resolves the specified args. | 
|  |  | 51 |  |         /// </summary> | 
|  |  | 52 |  |         /// <param name="args">The args.</param> | 
|  |  | 53 |  |         /// <returns>MusicAlbum.</returns> | 
|  |  | 54 |  |         protected override MusicAlbum Resolve(ItemResolveArgs args) | 
|  |  | 55 |  |         { | 
|  | 10 | 56 |  |             var collectionType = args.GetCollectionType(); | 
|  | 10 | 57 |  |             var isMusicMediaFolder = collectionType == CollectionType.music; | 
|  |  | 58 |  |  | 
|  |  | 59 |  |             // If there's a collection type and it's not music, don't allow it. | 
|  | 10 | 60 |  |             if (!isMusicMediaFolder) | 
|  |  | 61 |  |             { | 
|  | 10 | 62 |  |                 return null; | 
|  |  | 63 |  |             } | 
|  |  | 64 |  |  | 
|  | 0 | 65 |  |             if (!args.IsDirectory) | 
|  |  | 66 |  |             { | 
|  | 0 | 67 |  |                 return null; | 
|  |  | 68 |  |             } | 
|  |  | 69 |  |  | 
|  |  | 70 |  |             // Avoid mis-identifying top folders | 
|  | 0 | 71 |  |             if (args.HasParent<MusicAlbum>()) | 
|  |  | 72 |  |             { | 
|  | 0 | 73 |  |                 return null; | 
|  |  | 74 |  |             } | 
|  |  | 75 |  |  | 
|  | 0 | 76 |  |             if (args.Parent.IsRoot) | 
|  |  | 77 |  |             { | 
|  | 0 | 78 |  |                 return null; | 
|  |  | 79 |  |             } | 
|  |  | 80 |  |  | 
|  | 0 | 81 |  |             return IsMusicAlbum(args) ? new MusicAlbum() : null; | 
|  |  | 82 |  |         } | 
|  |  | 83 |  |  | 
|  |  | 84 |  |         /// <summary> | 
|  |  | 85 |  |         /// Determine if the supplied file data points to a music album. | 
|  |  | 86 |  |         /// </summary> | 
|  |  | 87 |  |         /// <param name="path">The path to check.</param> | 
|  |  | 88 |  |         /// <param name="directoryService">The directory service.</param> | 
|  |  | 89 |  |         /// <returns><c>true</c> if the provided path points to a music album; otherwise, <c>false</c>.</returns> | 
|  |  | 90 |  |         public bool IsMusicAlbum(string path, IDirectoryService directoryService) | 
|  |  | 91 |  |         { | 
|  | 0 | 92 |  |             return ContainsMusic(directoryService.GetFileSystemEntries(path), true, directoryService); | 
|  |  | 93 |  |         } | 
|  |  | 94 |  |  | 
|  |  | 95 |  |         /// <summary> | 
|  |  | 96 |  |         /// Determine if the supplied resolve args should be considered a music album. | 
|  |  | 97 |  |         /// </summary> | 
|  |  | 98 |  |         /// <param name="args">The args.</param> | 
|  |  | 99 |  |         /// <returns><c>true</c> if [is music album] [the specified args]; otherwise, <c>false</c>.</returns> | 
|  |  | 100 |  |         private bool IsMusicAlbum(ItemResolveArgs args) | 
|  |  | 101 |  |         { | 
|  | 0 | 102 |  |             if (args.IsDirectory) | 
|  |  | 103 |  |             { | 
|  |  | 104 |  |                 // If args is a artist subfolder it's not a music album | 
|  | 0 | 105 |  |                 foreach (var subfolder in _namingOptions.ArtistSubfolders) | 
|  |  | 106 |  |                 { | 
|  | 0 | 107 |  |                     if (Path.GetDirectoryName(args.Path.AsSpan()).Equals(subfolder, StringComparison.OrdinalIgnoreCase)) | 
|  |  | 108 |  |                     { | 
|  | 0 | 109 |  |                         _logger.LogDebug("Found release folder: {Path}", args.Path); | 
|  | 0 | 110 |  |                         return false; | 
|  |  | 111 |  |                     } | 
|  |  | 112 |  |                 } | 
|  |  | 113 |  |  | 
|  |  | 114 |  |                 // If args contains music it's a music album | 
|  | 0 | 115 |  |                 if (ContainsMusic(args.FileSystemChildren, true, _directoryService)) | 
|  |  | 116 |  |                 { | 
|  | 0 | 117 |  |                     return true; | 
|  |  | 118 |  |                 } | 
|  |  | 119 |  |             } | 
|  |  | 120 |  |  | 
|  | 0 | 121 |  |             return false; | 
|  |  | 122 |  |         } | 
|  |  | 123 |  |  | 
|  |  | 124 |  |         /// <summary> | 
|  |  | 125 |  |         /// Determine if the supplied list contains what we should consider music. | 
|  |  | 126 |  |         /// </summary> | 
|  |  | 127 |  |         /// <returns><c>true</c> if the provided path list contains music; otherwise, <c>false</c>.</returns> | 
|  |  | 128 |  |         private bool ContainsMusic( | 
|  |  | 129 |  |             ICollection<FileSystemMetadata> list, | 
|  |  | 130 |  |             bool allowSubfolders, | 
|  |  | 131 |  |             IDirectoryService directoryService) | 
|  |  | 132 |  |         { | 
|  |  | 133 |  |             // Check for audio files before digging down into directories | 
|  | 0 | 134 |  |             var foundAudioFile = list.Any(fileSystemInfo => !fileSystemInfo.IsDirectory && AudioFileParser.IsAudioFile(f | 
|  | 0 | 135 |  |             if (foundAudioFile) | 
|  |  | 136 |  |             { | 
|  |  | 137 |  |                 // At least one audio file exists | 
|  | 0 | 138 |  |                 return true; | 
|  |  | 139 |  |             } | 
|  |  | 140 |  |  | 
|  | 0 | 141 |  |             if (!allowSubfolders) | 
|  |  | 142 |  |             { | 
|  |  | 143 |  |                 // Not music since no audio file exists and we're not looking into subfolders | 
|  | 0 | 144 |  |                 return false; | 
|  |  | 145 |  |             } | 
|  |  | 146 |  |  | 
|  | 0 | 147 |  |             var discSubfolderCount = 0; | 
|  |  | 148 |  |  | 
|  | 0 | 149 |  |             var parser = new AlbumParser(_namingOptions); | 
|  |  | 150 |  |  | 
|  | 0 | 151 |  |             var directories = list.Where(fileSystemInfo => fileSystemInfo.IsDirectory); | 
|  |  | 152 |  |  | 
|  | 0 | 153 |  |             var result = Parallel.ForEach(directories, (fileSystemInfo, state) => | 
|  | 0 | 154 |  |             { | 
|  | 0 | 155 |  |                 var path = fileSystemInfo.FullName; | 
|  | 0 | 156 |  |                 var hasMusic = ContainsMusic(directoryService.GetFileSystemEntries(path), false, directoryService); | 
|  | 0 | 157 |  |  | 
|  | 0 | 158 |  |                 if (hasMusic) | 
|  | 0 | 159 |  |                 { | 
|  | 0 | 160 |  |                     if (parser.IsMultiPart(path)) | 
|  | 0 | 161 |  |                     { | 
|  | 0 | 162 |  |                         _logger.LogDebug("Found multi-disc folder: {Path}", path); | 
|  | 0 | 163 |  |                         Interlocked.Increment(ref discSubfolderCount); | 
|  | 0 | 164 |  |                     } | 
|  | 0 | 165 |  |                     else | 
|  | 0 | 166 |  |                     { | 
|  | 0 | 167 |  |                         // If there are folders underneath with music that are not multidisc, then this can't be a multi | 
|  | 0 | 168 |  |                         state.Stop(); | 
|  | 0 | 169 |  |                     } | 
|  | 0 | 170 |  |                 } | 
|  | 0 | 171 |  |             }); | 
|  |  | 172 |  |  | 
|  | 0 | 173 |  |             if (!result.IsCompleted) | 
|  |  | 174 |  |             { | 
|  | 0 | 175 |  |                 return false; | 
|  |  | 176 |  |             } | 
|  |  | 177 |  |  | 
|  | 0 | 178 |  |             return discSubfolderCount > 0; | 
|  |  | 179 |  |         } | 
|  |  | 180 |  |     } | 
|  |  | 181 |  | } |