ItemController.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 =>
  101. {
  102. return string.IsNullOrEmpty(f.SortName) ? f.Name : f.SortName;
  103. });
  104. }
  105. }
  106. return item;
  107. }
  108. /// <summary>
  109. /// Finds child BaseItems for a given Folder
  110. /// </summary>
  111. private Task<BaseItem>[] GetChildren(Folder folder, WIN32_FIND_DATA[] fileSystemChildren, bool allowInternetProviders)
  112. {
  113. Task<BaseItem>[] tasks = new Task<BaseItem>[fileSystemChildren.Length];
  114. for (int i = 0; i < fileSystemChildren.Length; i++)
  115. {
  116. var child = fileSystemChildren[i];
  117. tasks[i] = GetItem(child.Path, folder, child, allowInternetProviders: allowInternetProviders);
  118. }
  119. return tasks;
  120. }
  121. /// <summary>
  122. /// Transforms shortcuts into their actual paths
  123. /// </summary>
  124. private WIN32_FIND_DATA[] FilterChildFileSystemEntries(WIN32_FIND_DATA[] fileSystemChildren, bool flattenShortcuts)
  125. {
  126. WIN32_FIND_DATA[] returnArray = new WIN32_FIND_DATA[fileSystemChildren.Length];
  127. List<WIN32_FIND_DATA> resolvedShortcuts = new List<WIN32_FIND_DATA>();
  128. for (int i = 0; i < fileSystemChildren.Length; i++)
  129. {
  130. WIN32_FIND_DATA file = fileSystemChildren[i];
  131. // If it's a shortcut, resolve it
  132. if (Shortcut.IsShortcut(file.Path))
  133. {
  134. string newPath = Shortcut.ResolveShortcut(file.Path);
  135. WIN32_FIND_DATA newPathData = FileData.GetFileData(newPath);
  136. // Find out if the shortcut is pointing to a directory or file
  137. if (newPathData.IsDirectory)
  138. {
  139. // If we're flattening then get the shortcut's children
  140. if (flattenShortcuts)
  141. {
  142. returnArray[i] = file;
  143. WIN32_FIND_DATA[] newChildren = FileData.GetFileSystemEntries(newPath, "*").ToArray();
  144. resolvedShortcuts.AddRange(FilterChildFileSystemEntries(newChildren, false));
  145. }
  146. else
  147. {
  148. returnArray[i] = newPathData;
  149. }
  150. }
  151. else
  152. {
  153. returnArray[i] = newPathData;
  154. }
  155. }
  156. else
  157. {
  158. returnArray[i] = file;
  159. }
  160. }
  161. if (resolvedShortcuts.Count > 0)
  162. {
  163. resolvedShortcuts.InsertRange(0, returnArray);
  164. return resolvedShortcuts.ToArray();
  165. }
  166. return returnArray;
  167. }
  168. /// <summary>
  169. /// Gets a Person
  170. /// </summary>
  171. public Task<Person> GetPerson(string name)
  172. {
  173. return GetImagesByNameItem<Person>(Kernel.Instance.ApplicationPaths.PeoplePath, name);
  174. }
  175. /// <summary>
  176. /// Gets a Studio
  177. /// </summary>
  178. public Task<Studio> GetStudio(string name)
  179. {
  180. return GetImagesByNameItem<Studio>(Kernel.Instance.ApplicationPaths.StudioPath, name);
  181. }
  182. /// <summary>
  183. /// Gets a Genre
  184. /// </summary>
  185. public Task<Genre> GetGenre(string name)
  186. {
  187. return GetImagesByNameItem<Genre>(Kernel.Instance.ApplicationPaths.GenrePath, name);
  188. }
  189. /// <summary>
  190. /// Gets a Year
  191. /// </summary>
  192. public Task<Year> GetYear(int value)
  193. {
  194. return GetImagesByNameItem<Year>(Kernel.Instance.ApplicationPaths.YearPath, value.ToString());
  195. }
  196. private readonly ConcurrentDictionary<string, object> ImagesByNameItemCache = new ConcurrentDictionary<string, object>(StringComparer.OrdinalIgnoreCase);
  197. /// <summary>
  198. /// Generically retrieves an IBN item
  199. /// </summary>
  200. private Task<T> GetImagesByNameItem<T>(string path, string name)
  201. where T : BaseEntity, new()
  202. {
  203. name = FileData.GetValidFilename(name);
  204. string key = Path.Combine(path, name);
  205. // Look for it in the cache, if it's not there, create it
  206. if (!ImagesByNameItemCache.ContainsKey(key))
  207. {
  208. ImagesByNameItemCache[key] = CreateImagesByNameItem<T>(path, name);
  209. }
  210. return ImagesByNameItemCache[key] as Task<T>;
  211. }
  212. /// <summary>
  213. /// Creates an IBN item based on a given path
  214. /// </summary>
  215. private async Task<T> CreateImagesByNameItem<T>(string path, string name)
  216. where T : BaseEntity, new()
  217. {
  218. T item = new T();
  219. item.Name = name;
  220. item.Id = Kernel.GetMD5(path);
  221. if (!Directory.Exists(path))
  222. {
  223. Directory.CreateDirectory(path);
  224. }
  225. item.DateCreated = Directory.GetCreationTimeUtc(path);
  226. item.DateModified = Directory.GetLastWriteTimeUtc(path);
  227. ItemResolveEventArgs args = new ItemResolveEventArgs();
  228. args.FileInfo = FileData.GetFileData(path);
  229. args.FileSystemChildren = FileData.GetFileSystemEntries(path, "*").ToArray();
  230. await Kernel.Instance.ExecuteMetadataProviders(item, args).ConfigureAwait(false);
  231. return item;
  232. }
  233. }
  234. }