AudioResolver.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using Emby.Naming.AudioBook;
  7. using MediaBrowser.Controller.Entities;
  8. using MediaBrowser.Controller.Library;
  9. using MediaBrowser.Controller.Providers;
  10. using MediaBrowser.Controller.Resolvers;
  11. using MediaBrowser.Model.Entities;
  12. using MediaBrowser.Model.IO;
  13. namespace Emby.Server.Implementations.Library.Resolvers.Audio
  14. {
  15. /// <summary>
  16. /// Class AudioResolver.
  17. /// </summary>
  18. public class AudioResolver : ItemResolver<MediaBrowser.Controller.Entities.Audio.Audio>, IMultiItemResolver
  19. {
  20. private readonly ILibraryManager LibraryManager;
  21. public AudioResolver(ILibraryManager libraryManager)
  22. {
  23. LibraryManager = libraryManager;
  24. }
  25. /// <summary>
  26. /// Gets the priority.
  27. /// </summary>
  28. /// <value>The priority.</value>
  29. public override ResolverPriority Priority => ResolverPriority.Fifth;
  30. public MultiItemResolverResult ResolveMultiple(
  31. Folder parent,
  32. List<FileSystemMetadata> files,
  33. string collectionType,
  34. IDirectoryService directoryService)
  35. {
  36. var result = ResolveMultipleInternal(parent, files, collectionType, directoryService);
  37. if (result != null)
  38. {
  39. foreach (var item in result.Items)
  40. {
  41. SetInitialItemValues((MediaBrowser.Controller.Entities.Audio.Audio)item, null);
  42. }
  43. }
  44. return result;
  45. }
  46. private MultiItemResolverResult ResolveMultipleInternal(
  47. Folder parent,
  48. List<FileSystemMetadata> files,
  49. string collectionType,
  50. IDirectoryService directoryService)
  51. {
  52. if (string.Equals(collectionType, CollectionType.Books, StringComparison.OrdinalIgnoreCase))
  53. {
  54. return ResolveMultipleAudio<AudioBook>(parent, files, directoryService, false, collectionType, true);
  55. }
  56. return null;
  57. }
  58. /// <summary>
  59. /// Resolves the specified args.
  60. /// </summary>
  61. /// <param name="args">The args.</param>
  62. /// <returns>Entities.Audio.Audio.</returns>
  63. protected override MediaBrowser.Controller.Entities.Audio.Audio Resolve(ItemResolveArgs args)
  64. {
  65. // Return audio if the path is a file and has a matching extension
  66. var collectionType = args.GetCollectionType();
  67. var isBooksCollectionType = string.Equals(collectionType, CollectionType.Books, StringComparison.OrdinalIgnoreCase);
  68. if (args.IsDirectory)
  69. {
  70. if (!isBooksCollectionType)
  71. {
  72. return null;
  73. }
  74. var files = args.FileSystemChildren
  75. .Where(i => !LibraryManager.IgnoreFile(i, args.Parent))
  76. .ToList();
  77. return FindAudio<AudioBook>(args, args.Path, args.Parent, files, args.DirectoryService, collectionType, false);
  78. }
  79. if (LibraryManager.IsAudioFile(args.Path))
  80. {
  81. var extension = Path.GetExtension(args.Path);
  82. if (string.Equals(extension, ".cue", StringComparison.OrdinalIgnoreCase))
  83. {
  84. // if audio file exists of same name, return null
  85. return null;
  86. }
  87. var isMixedCollectionType = string.IsNullOrEmpty(collectionType);
  88. // For conflicting extensions, give priority to videos
  89. if (isMixedCollectionType && LibraryManager.IsVideoFile(args.Path))
  90. {
  91. return null;
  92. }
  93. MediaBrowser.Controller.Entities.Audio.Audio item = null;
  94. var isMusicCollectionType = string.Equals(collectionType, CollectionType.Music, StringComparison.OrdinalIgnoreCase);
  95. // Use regular audio type for mixed libraries, owned items and music
  96. if (isMixedCollectionType ||
  97. args.Parent == null ||
  98. isMusicCollectionType)
  99. {
  100. item = new MediaBrowser.Controller.Entities.Audio.Audio();
  101. }
  102. else if (isBooksCollectionType)
  103. {
  104. item = new AudioBook();
  105. }
  106. if (item != null)
  107. {
  108. item.IsShortcut = string.Equals(extension, ".strm", StringComparison.OrdinalIgnoreCase);
  109. item.IsInMixedFolder = true;
  110. }
  111. return item;
  112. }
  113. return null;
  114. }
  115. private T FindAudio<T>(ItemResolveArgs args, string path, Folder parent, List<FileSystemMetadata> fileSystemEntries, IDirectoryService directoryService, string collectionType, bool parseName)
  116. where T : MediaBrowser.Controller.Entities.Audio.Audio, new()
  117. {
  118. // TODO: Allow GetMultiDiscMovie in here
  119. const bool supportsMultiVersion = false;
  120. var result = ResolveMultipleAudio<T>(parent, fileSystemEntries, directoryService, supportsMultiVersion, collectionType, parseName) ??
  121. new MultiItemResolverResult();
  122. if (result.Items.Count == 1)
  123. {
  124. // If we were supporting this we'd be checking filesFromOtherItems
  125. var item = (T)result.Items[0];
  126. item.IsInMixedFolder = false;
  127. item.Name = Path.GetFileName(item.ContainingFolderPath);
  128. return item;
  129. }
  130. return null;
  131. }
  132. private MultiItemResolverResult ResolveMultipleAudio<T>(Folder parent, IEnumerable<FileSystemMetadata> fileSystemEntries, IDirectoryService directoryService, bool suppportMultiEditions, string collectionType, bool parseName)
  133. where T : MediaBrowser.Controller.Entities.Audio.Audio, new()
  134. {
  135. var files = new List<FileSystemMetadata>();
  136. var items = new List<BaseItem>();
  137. var leftOver = new List<FileSystemMetadata>();
  138. // Loop through each child file/folder and see if we find a video
  139. foreach (var child in fileSystemEntries)
  140. {
  141. if (child.IsDirectory)
  142. {
  143. leftOver.Add(child);
  144. }
  145. else if (!IsIgnored(child.Name))
  146. {
  147. files.Add(child);
  148. }
  149. }
  150. var namingOptions = ((LibraryManager)LibraryManager).GetNamingOptions();
  151. var resolver = new AudioBookListResolver(namingOptions);
  152. var resolverResult = resolver.Resolve(files).ToList();
  153. var result = new MultiItemResolverResult
  154. {
  155. ExtraFiles = leftOver,
  156. Items = items
  157. };
  158. var isInMixedFolder = resolverResult.Count > 1 || (parent != null && parent.IsTopParent);
  159. foreach (var resolvedItem in resolverResult)
  160. {
  161. if (resolvedItem.Files.Count > 1)
  162. {
  163. // For now, until we sort out naming for multi-part books
  164. continue;
  165. }
  166. var firstMedia = resolvedItem.Files[0];
  167. var libraryItem = new T
  168. {
  169. Path = firstMedia.Path,
  170. IsInMixedFolder = isInMixedFolder,
  171. ProductionYear = resolvedItem.Year,
  172. Name = parseName ?
  173. resolvedItem.Name :
  174. Path.GetFileNameWithoutExtension(firstMedia.Path),
  175. // AdditionalParts = resolvedItem.Files.Skip(1).Select(i => i.Path).ToArray(),
  176. // LocalAlternateVersions = resolvedItem.AlternateVersions.Select(i => i.Path).ToArray()
  177. };
  178. result.Items.Add(libraryItem);
  179. }
  180. result.ExtraFiles.AddRange(files.Where(i => !ContainsFile(resolverResult, i)));
  181. return result;
  182. }
  183. private bool ContainsFile(List<AudioBookInfo> result, FileSystemMetadata file)
  184. {
  185. return result.Any(i => ContainsFile(i, file));
  186. }
  187. private bool ContainsFile(AudioBookInfo result, FileSystemMetadata file)
  188. {
  189. return result.Files.Any(i => ContainsFile(i, file)) ||
  190. result.AlternateVersions.Any(i => ContainsFile(i, file)) ||
  191. result.Extras.Any(i => ContainsFile(i, file));
  192. }
  193. private static bool ContainsFile(AudioBookFileInfo result, FileSystemMetadata file)
  194. {
  195. return string.Equals(result.Path, file.FullName, StringComparison.OrdinalIgnoreCase);
  196. }
  197. private static bool IsIgnored(string filename)
  198. {
  199. return false;
  200. }
  201. }
  202. }