ItemController.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. using MediaBrowser.Controller.Entities;
  2. using MediaBrowser.Controller.IO;
  3. using System;
  4. using System.Collections.Concurrent;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. namespace MediaBrowser.Controller.Library
  10. {
  11. public class ItemController
  12. {
  13. #region PreBeginResolvePath Event
  14. /// <summary>
  15. /// Fires when a path is about to be resolved, but before child folders and files
  16. /// have been collected from the file system.
  17. /// This gives listeners a chance to cancel the operation and cause the path to be ignored.
  18. /// </summary>
  19. public event EventHandler<PreBeginResolveEventArgs> PreBeginResolvePath;
  20. private bool OnPreBeginResolvePath(PreBeginResolveEventArgs args)
  21. {
  22. if (PreBeginResolvePath != null)
  23. {
  24. PreBeginResolvePath(this, args);
  25. }
  26. return !args.Cancel;
  27. }
  28. #endregion
  29. #region BeginResolvePath Event
  30. /// <summary>
  31. /// Fires when a path is about to be resolved, but after child folders and files
  32. /// have been collected from the file system.
  33. /// This gives listeners a chance to cancel the operation and cause the path to be ignored.
  34. /// </summary>
  35. public event EventHandler<ItemResolveEventArgs> BeginResolvePath;
  36. private bool OnBeginResolvePath(ItemResolveEventArgs args)
  37. {
  38. if (BeginResolvePath != null)
  39. {
  40. BeginResolvePath(this, args);
  41. }
  42. return !args.Cancel;
  43. }
  44. #endregion
  45. private BaseItem ResolveItem(ItemResolveEventArgs args)
  46. {
  47. // Try first priority resolvers
  48. for (int i = 0; i < Kernel.Instance.EntityResolvers.Length; i++)
  49. {
  50. var item = Kernel.Instance.EntityResolvers[i].ResolvePath(args);
  51. if (item != null)
  52. {
  53. return item;
  54. }
  55. }
  56. return null;
  57. }
  58. /// <summary>
  59. /// Resolves a path into a BaseItem
  60. /// </summary>
  61. public async Task<BaseItem> GetItem(string path, Folder parent = null, WIN32_FIND_DATA? fileInfo = null, bool allowInternetProviders = true)
  62. {
  63. var args = new ItemResolveEventArgs
  64. {
  65. FileInfo = fileInfo ?? FileData.GetFileData(path),
  66. Parent = parent,
  67. Cancel = false,
  68. Path = path
  69. };
  70. if (!OnPreBeginResolvePath(args))
  71. {
  72. return null;
  73. }
  74. WIN32_FIND_DATA[] fileSystemChildren;
  75. // Gather child folder and files
  76. if (args.IsDirectory)
  77. {
  78. fileSystemChildren = FileData.GetFileSystemEntries(path, "*").ToArray();
  79. bool isVirtualFolder = parent != null && parent.IsRoot;
  80. fileSystemChildren = FilterChildFileSystemEntries(fileSystemChildren, isVirtualFolder);
  81. }
  82. else
  83. {
  84. fileSystemChildren = new WIN32_FIND_DATA[] { };
  85. }
  86. args.FileSystemChildren = fileSystemChildren;
  87. // Fire BeginResolvePath to see if anyone wants to cancel this operation
  88. if (!OnBeginResolvePath(args))
  89. {
  90. return null;
  91. }
  92. BaseItem item = ResolveItem(args);
  93. if (item != null)
  94. {
  95. await Kernel.Instance.ExecuteMetadataProviders(item, args, allowInternetProviders: allowInternetProviders).ConfigureAwait(false);
  96. if (item.IsFolder)
  97. {
  98. // If it's a folder look for child entities
  99. (item as Folder).Children = (await Task.WhenAll(GetChildren(item as Folder, fileSystemChildren, allowInternetProviders)).ConfigureAwait(false))
  100. .Where(i => i != null).OrderBy(f => (string.IsNullOrEmpty(f.SortName) ? f.Name : f.SortName));
  101. }
  102. }
  103. return item;
  104. }
  105. /// <summary>
  106. /// Finds child BaseItems for a given Folder
  107. /// </summary>
  108. private Task<BaseItem>[] GetChildren(Folder folder, WIN32_FIND_DATA[] fileSystemChildren, bool allowInternetProviders)
  109. {
  110. var tasks = new Task<BaseItem>[fileSystemChildren.Length];
  111. for (int i = 0; i < fileSystemChildren.Length; i++)
  112. {
  113. var child = fileSystemChildren[i];
  114. tasks[i] = GetItem(child.Path, folder, child, allowInternetProviders: allowInternetProviders);
  115. }
  116. return tasks;
  117. }
  118. /// <summary>
  119. /// Transforms shortcuts into their actual paths
  120. /// </summary>
  121. private WIN32_FIND_DATA[] FilterChildFileSystemEntries(WIN32_FIND_DATA[] fileSystemChildren, bool flattenShortcuts)
  122. {
  123. var returnArray = new WIN32_FIND_DATA[fileSystemChildren.Length];
  124. var resolvedShortcuts = new List<WIN32_FIND_DATA>();
  125. for (int i = 0; i < fileSystemChildren.Length; i++)
  126. {
  127. WIN32_FIND_DATA file = fileSystemChildren[i];
  128. // If it's a shortcut, resolve it
  129. if (Shortcut.IsShortcut(file.Path))
  130. {
  131. string newPath = Shortcut.ResolveShortcut(file.Path);
  132. WIN32_FIND_DATA newPathData = FileData.GetFileData(newPath);
  133. // Find out if the shortcut is pointing to a directory or file
  134. if (newPathData.IsDirectory)
  135. {
  136. // If we're flattening then get the shortcut's children
  137. if (flattenShortcuts)
  138. {
  139. returnArray[i] = file;
  140. WIN32_FIND_DATA[] newChildren = FileData.GetFileSystemEntries(newPath, "*").ToArray();
  141. resolvedShortcuts.AddRange(FilterChildFileSystemEntries(newChildren, false));
  142. }
  143. else
  144. {
  145. returnArray[i] = newPathData;
  146. }
  147. }
  148. else
  149. {
  150. returnArray[i] = newPathData;
  151. }
  152. }
  153. else
  154. {
  155. returnArray[i] = file;
  156. }
  157. }
  158. if (resolvedShortcuts.Count > 0)
  159. {
  160. resolvedShortcuts.InsertRange(0, returnArray);
  161. return resolvedShortcuts.ToArray();
  162. }
  163. return returnArray;
  164. }
  165. /// <summary>
  166. /// Gets a Person
  167. /// </summary>
  168. public Task<Person> GetPerson(string name)
  169. {
  170. return GetImagesByNameItem<Person>(Kernel.Instance.ApplicationPaths.PeoplePath, name);
  171. }
  172. /// <summary>
  173. /// Gets a Studio
  174. /// </summary>
  175. public Task<Studio> GetStudio(string name)
  176. {
  177. return GetImagesByNameItem<Studio>(Kernel.Instance.ApplicationPaths.StudioPath, name);
  178. }
  179. /// <summary>
  180. /// Gets a Genre
  181. /// </summary>
  182. public Task<Genre> GetGenre(string name)
  183. {
  184. return GetImagesByNameItem<Genre>(Kernel.Instance.ApplicationPaths.GenrePath, name);
  185. }
  186. /// <summary>
  187. /// Gets a Year
  188. /// </summary>
  189. public Task<Year> GetYear(int value)
  190. {
  191. return GetImagesByNameItem<Year>(Kernel.Instance.ApplicationPaths.YearPath, value.ToString());
  192. }
  193. private readonly ConcurrentDictionary<string, object> ImagesByNameItemCache = new ConcurrentDictionary<string, object>(StringComparer.OrdinalIgnoreCase);
  194. /// <summary>
  195. /// Generically retrieves an IBN item
  196. /// </summary>
  197. private Task<T> GetImagesByNameItem<T>(string path, string name)
  198. where T : BaseEntity, new()
  199. {
  200. name = FileData.GetValidFilename(name);
  201. string key = Path.Combine(path, name);
  202. // Look for it in the cache, if it's not there, create it
  203. if (!ImagesByNameItemCache.ContainsKey(key))
  204. {
  205. ImagesByNameItemCache[key] = CreateImagesByNameItem<T>(path, name);
  206. }
  207. return ImagesByNameItemCache[key] as Task<T>;
  208. }
  209. /// <summary>
  210. /// Creates an IBN item based on a given path
  211. /// </summary>
  212. private async Task<T> CreateImagesByNameItem<T>(string path, string name)
  213. where T : BaseEntity, new()
  214. {
  215. var item = new T { };
  216. item.Name = name;
  217. item.Id = Kernel.GetMD5(path);
  218. if (!Directory.Exists(path))
  219. {
  220. Directory.CreateDirectory(path);
  221. }
  222. item.DateCreated = Directory.GetCreationTimeUtc(path);
  223. item.DateModified = Directory.GetLastWriteTimeUtc(path);
  224. var args = new ItemResolveEventArgs { };
  225. args.FileInfo = FileData.GetFileData(path);
  226. args.FileSystemChildren = FileData.GetFileSystemEntries(path, "*").ToArray();
  227. await Kernel.Instance.ExecuteMetadataProviders(item, args).ConfigureAwait(false);
  228. return item;
  229. }
  230. }
  231. }