AudioResolver.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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
  31. {
  32. get { return ResolverPriority.Last; }
  33. }
  34. public MultiItemResolverResult ResolveMultiple(Folder parent,
  35. List<FileSystemMetadata> files,
  36. string collectionType,
  37. IDirectoryService directoryService)
  38. {
  39. var result = ResolveMultipleInternal(parent, files, collectionType, directoryService);
  40. if (result != null)
  41. {
  42. foreach (var item in result.Items)
  43. {
  44. SetInitialItemValues((MediaBrowser.Controller.Entities.Audio.Audio)item, null);
  45. }
  46. }
  47. return result;
  48. }
  49. private MultiItemResolverResult ResolveMultipleInternal(Folder parent,
  50. List<FileSystemMetadata> files,
  51. string collectionType,
  52. IDirectoryService directoryService)
  53. {
  54. if (string.Equals(collectionType, CollectionType.Books, StringComparison.OrdinalIgnoreCase))
  55. {
  56. return ResolveMultipleAudio<AudioBook>(parent, files, directoryService, false, collectionType, true);
  57. }
  58. return null;
  59. }
  60. /// <summary>
  61. /// Resolves the specified args.
  62. /// </summary>
  63. /// <param name="args">The args.</param>
  64. /// <returns>Entities.Audio.Audio.</returns>
  65. protected override MediaBrowser.Controller.Entities.Audio.Audio Resolve(ItemResolveArgs args)
  66. {
  67. // Return audio if the path is a file and has a matching extension
  68. var libraryOptions = args.GetLibraryOptions();
  69. var collectionType = args.GetCollectionType();
  70. var isBooksCollectionType = string.Equals(collectionType, CollectionType.Books, StringComparison.OrdinalIgnoreCase);
  71. if (args.IsDirectory)
  72. {
  73. if (!isBooksCollectionType)
  74. {
  75. return null;
  76. }
  77. var files = args.FileSystemChildren
  78. .Where(i => !LibraryManager.IgnoreFile(i, args.Parent))
  79. .ToList();
  80. if (isBooksCollectionType)
  81. {
  82. return FindAudio<AudioBook>(args, args.Path, args.Parent, files, args.DirectoryService, collectionType, false);
  83. }
  84. return null;
  85. }
  86. if (LibraryManager.IsAudioFile(args.Path, libraryOptions))
  87. {
  88. if (string.Equals(Path.GetExtension(args.Path), ".cue", StringComparison.OrdinalIgnoreCase))
  89. {
  90. // if audio file exists of same name, return null
  91. return null;
  92. }
  93. var isMixedCollectionType = string.IsNullOrWhiteSpace(collectionType);
  94. // For conflicting extensions, give priority to videos
  95. if (isMixedCollectionType && LibraryManager.IsVideoFile(args.Path, libraryOptions))
  96. {
  97. return null;
  98. }
  99. MediaBrowser.Controller.Entities.Audio.Audio item = null;
  100. var isMusicCollectionType = string.Equals(collectionType, CollectionType.Music, StringComparison.OrdinalIgnoreCase);
  101. // Use regular audio type for mixed libraries, owned items and music
  102. if (isMixedCollectionType ||
  103. args.Parent == null ||
  104. isMusicCollectionType)
  105. {
  106. item = new MediaBrowser.Controller.Entities.Audio.Audio();
  107. }
  108. else if (isBooksCollectionType)
  109. {
  110. item = new AudioBook();
  111. }
  112. if (item != null)
  113. {
  114. item.IsInMixedFolder = true;
  115. }
  116. return item;
  117. }
  118. return null;
  119. }
  120. private T FindAudio<T>(ItemResolveArgs args, string path, Folder parent, List<FileSystemMetadata> fileSystemEntries, IDirectoryService directoryService, string collectionType, bool parseName)
  121. where T : MediaBrowser.Controller.Entities.Audio.Audio, new()
  122. {
  123. var multiDiscFolders = new List<FileSystemMetadata>();
  124. var libraryOptions = args.GetLibraryOptions();
  125. var filesFromOtherItems = new List<FileSystemMetadata>();
  126. // TODO: Allow GetMultiDiscMovie in here
  127. var supportsMultiVersion = false;
  128. var result = ResolveMultipleAudio<T>(parent, fileSystemEntries, directoryService, supportsMultiVersion, collectionType, parseName) ??
  129. new MultiItemResolverResult();
  130. if (result.Items.Count == 1)
  131. {
  132. var videoPath = result.Items[0].Path;
  133. // If we were supporting this we'd be checking filesFromOtherItems
  134. var hasOtherItems = false;
  135. if (!hasOtherItems)
  136. {
  137. var item = (T)result.Items[0];
  138. item.IsInMixedFolder = false;
  139. item.Name = Path.GetFileName(item.ContainingFolderPath);
  140. return item;
  141. }
  142. }
  143. if (result.Items.Count == 0 && multiDiscFolders.Count > 0)
  144. {
  145. //return GetMultiDiscAudio<T>(multiDiscFolders, directoryService);
  146. }
  147. return null;
  148. }
  149. private MultiItemResolverResult ResolveMultipleAudio<T>(Folder parent, IEnumerable<FileSystemMetadata> fileSystemEntries, IDirectoryService directoryService, bool suppportMultiEditions, string collectionType, bool parseName)
  150. where T : MediaBrowser.Controller.Entities.Audio.Audio, new()
  151. {
  152. var files = new List<FileSystemMetadata>();
  153. var items = new List<BaseItem>();
  154. var leftOver = new List<FileSystemMetadata>();
  155. // Loop through each child file/folder and see if we find a video
  156. foreach (var child in fileSystemEntries)
  157. {
  158. if (child.IsDirectory)
  159. {
  160. leftOver.Add(child);
  161. }
  162. else if (IsIgnored(child.Name))
  163. {
  164. }
  165. else
  166. {
  167. files.Add(child);
  168. }
  169. }
  170. var namingOptions = ((LibraryManager)LibraryManager).GetNamingOptions();
  171. var resolver = new AudioBookListResolver(namingOptions);
  172. var resolverResult = resolver.Resolve(files).ToList();
  173. var result = new MultiItemResolverResult
  174. {
  175. ExtraFiles = leftOver,
  176. Items = items
  177. };
  178. var isInMixedFolder = resolverResult.Count > 1 || (parent != null && parent.IsTopParent);
  179. foreach (var resolvedItem in resolverResult)
  180. {
  181. var firstMedia = resolvedItem.Files.First();
  182. var libraryItem = new T
  183. {
  184. Path = firstMedia.Path,
  185. IsInMixedFolder = isInMixedFolder,
  186. //ProductionYear = resolvedItem.Year,
  187. Name = parseName ?
  188. resolvedItem.Name :
  189. Path.GetFileNameWithoutExtension(firstMedia.Path),
  190. //AdditionalParts = resolvedItem.Files.Skip(1).Select(i => i.Path).ToArray(),
  191. //LocalAlternateVersions = resolvedItem.AlternateVersions.Select(i => i.Path).ToArray()
  192. };
  193. result.Items.Add(libraryItem);
  194. }
  195. result.ExtraFiles.AddRange(files.Where(i => !ContainsFile(resolverResult, i)));
  196. return result;
  197. }
  198. private bool ContainsFile(List<AudioBookInfo> result, FileSystemMetadata file)
  199. {
  200. return result.Any(i => ContainsFile(i, file));
  201. }
  202. private bool ContainsFile(AudioBookInfo result, FileSystemMetadata file)
  203. {
  204. return result.Files.Any(i => ContainsFile(i, file)) ||
  205. result.AlternateVersions.Any(i => ContainsFile(i, file)) ||
  206. result.Extras.Any(i => ContainsFile(i, file));
  207. }
  208. private bool ContainsFile(AudioBookFileInfo result, FileSystemMetadata file)
  209. {
  210. return string.Equals(result.Path, file.FullName, StringComparison.OrdinalIgnoreCase);
  211. }
  212. private bool IsIgnored(string filename)
  213. {
  214. return false;
  215. }
  216. }
  217. }