2
0

AudioResolver.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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.Fourth; }
  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. var extension = Path.GetExtension(args.Path);
  89. if (string.Equals(extension, ".cue", StringComparison.OrdinalIgnoreCase))
  90. {
  91. // if audio file exists of same name, return null
  92. return null;
  93. }
  94. var isMixedCollectionType = string.IsNullOrEmpty(collectionType);
  95. // For conflicting extensions, give priority to videos
  96. if (isMixedCollectionType && LibraryManager.IsVideoFile(args.Path, libraryOptions))
  97. {
  98. return null;
  99. }
  100. MediaBrowser.Controller.Entities.Audio.Audio item = null;
  101. var isMusicCollectionType = string.Equals(collectionType, CollectionType.Music, StringComparison.OrdinalIgnoreCase);
  102. // Use regular audio type for mixed libraries, owned items and music
  103. if (isMixedCollectionType ||
  104. args.Parent == null ||
  105. isMusicCollectionType)
  106. {
  107. item = new MediaBrowser.Controller.Entities.Audio.Audio();
  108. }
  109. else if (isBooksCollectionType)
  110. {
  111. item = new AudioBook();
  112. }
  113. if (item != null)
  114. {
  115. item.IsShortcut = string.Equals(extension, ".strm", StringComparison.OrdinalIgnoreCase);
  116. item.IsInMixedFolder = true;
  117. }
  118. return item;
  119. }
  120. return null;
  121. }
  122. private T FindAudio<T>(ItemResolveArgs args, string path, Folder parent, List<FileSystemMetadata> fileSystemEntries, IDirectoryService directoryService, string collectionType, bool parseName)
  123. where T : MediaBrowser.Controller.Entities.Audio.Audio, new()
  124. {
  125. var multiDiscFolders = new List<FileSystemMetadata>();
  126. var libraryOptions = args.GetLibraryOptions();
  127. var filesFromOtherItems = new List<FileSystemMetadata>();
  128. // TODO: Allow GetMultiDiscMovie in here
  129. var supportsMultiVersion = false;
  130. var result = ResolveMultipleAudio<T>(parent, fileSystemEntries, directoryService, supportsMultiVersion, collectionType, parseName) ??
  131. new MultiItemResolverResult();
  132. if (result.Items.Count == 1)
  133. {
  134. var videoPath = result.Items[0].Path;
  135. // If we were supporting this we'd be checking filesFromOtherItems
  136. var hasOtherItems = false;
  137. if (!hasOtherItems)
  138. {
  139. var item = (T)result.Items[0];
  140. item.IsInMixedFolder = false;
  141. item.Name = Path.GetFileName(item.ContainingFolderPath);
  142. return item;
  143. }
  144. }
  145. if (result.Items.Count == 0 && multiDiscFolders.Count > 0)
  146. {
  147. //return GetMultiDiscAudio<T>(multiDiscFolders, directoryService);
  148. }
  149. return null;
  150. }
  151. private MultiItemResolverResult ResolveMultipleAudio<T>(Folder parent, IEnumerable<FileSystemMetadata> fileSystemEntries, IDirectoryService directoryService, bool suppportMultiEditions, string collectionType, bool parseName)
  152. where T : MediaBrowser.Controller.Entities.Audio.Audio, new()
  153. {
  154. var files = new List<FileSystemMetadata>();
  155. var items = new List<BaseItem>();
  156. var leftOver = new List<FileSystemMetadata>();
  157. // Loop through each child file/folder and see if we find a video
  158. foreach (var child in fileSystemEntries)
  159. {
  160. if (child.IsDirectory)
  161. {
  162. leftOver.Add(child);
  163. }
  164. else if (IsIgnored(child.Name))
  165. {
  166. }
  167. else
  168. {
  169. files.Add(child);
  170. }
  171. }
  172. var namingOptions = ((LibraryManager)LibraryManager).GetNamingOptions();
  173. var resolver = new AudioBookListResolver(namingOptions);
  174. var resolverResult = resolver.Resolve(files).ToList();
  175. var result = new MultiItemResolverResult
  176. {
  177. ExtraFiles = leftOver,
  178. Items = items
  179. };
  180. var isInMixedFolder = resolverResult.Count > 1 || (parent != null && parent.IsTopParent);
  181. foreach (var resolvedItem in resolverResult)
  182. {
  183. if (resolvedItem.Files.Count > 1)
  184. {
  185. // For now, until we sort out naming for multi-part books
  186. continue;
  187. }
  188. var firstMedia = resolvedItem.Files.First();
  189. var libraryItem = new T
  190. {
  191. Path = firstMedia.Path,
  192. IsInMixedFolder = isInMixedFolder,
  193. ProductionYear = resolvedItem.Year,
  194. Name = parseName ?
  195. resolvedItem.Name :
  196. Path.GetFileNameWithoutExtension(firstMedia.Path),
  197. //AdditionalParts = resolvedItem.Files.Skip(1).Select(i => i.Path).ToArray(),
  198. //LocalAlternateVersions = resolvedItem.AlternateVersions.Select(i => i.Path).ToArray()
  199. };
  200. result.Items.Add(libraryItem);
  201. }
  202. result.ExtraFiles.AddRange(files.Where(i => !ContainsFile(resolverResult, i)));
  203. return result;
  204. }
  205. private bool ContainsFile(List<AudioBookInfo> result, FileSystemMetadata file)
  206. {
  207. return result.Any(i => ContainsFile(i, file));
  208. }
  209. private bool ContainsFile(AudioBookInfo result, FileSystemMetadata file)
  210. {
  211. return result.Files.Any(i => ContainsFile(i, file)) ||
  212. result.AlternateVersions.Any(i => ContainsFile(i, file)) ||
  213. result.Extras.Any(i => ContainsFile(i, file));
  214. }
  215. private bool ContainsFile(AudioBookFileInfo result, FileSystemMetadata file)
  216. {
  217. return string.Equals(result.Path, file.FullName, StringComparison.OrdinalIgnoreCase);
  218. }
  219. private bool IsIgnored(string filename)
  220. {
  221. return false;
  222. }
  223. }
  224. }