AudioResolver.cs 9.6 KB

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