AudioResolver.cs 9.6 KB

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