ItemController.cs 9.8 KB

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