ItemController.cs 7.8 KB

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