LibraryStructureService.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. using MediaBrowser.Controller;
  2. using MediaBrowser.Controller.Library;
  3. using MediaBrowser.Controller.Net;
  4. using MediaBrowser.Model.Entities;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using MediaBrowser.Common.Progress;
  12. using MediaBrowser.Model.IO;
  13. using MediaBrowser.Controller.Configuration;
  14. using MediaBrowser.Controller.Entities;
  15. using MediaBrowser.Controller.IO;
  16. using MediaBrowser.Model.Configuration;
  17. using MediaBrowser.Model.Services;
  18. namespace MediaBrowser.Api.Library
  19. {
  20. /// <summary>
  21. /// Class GetDefaultVirtualFolders
  22. /// </summary>
  23. [Route("/Library/VirtualFolders", "GET")]
  24. public class GetVirtualFolders : IReturn<List<VirtualFolderInfo>>
  25. {
  26. /// <summary>
  27. /// Gets or sets the user id.
  28. /// </summary>
  29. /// <value>The user id.</value>
  30. public string UserId { get; set; }
  31. }
  32. [Route("/Library/VirtualFolders", "POST")]
  33. public class AddVirtualFolder : IReturnVoid
  34. {
  35. /// <summary>
  36. /// Gets or sets the name.
  37. /// </summary>
  38. /// <value>The name.</value>
  39. public string Name { get; set; }
  40. /// <summary>
  41. /// Gets or sets the type of the collection.
  42. /// </summary>
  43. /// <value>The type of the collection.</value>
  44. public string CollectionType { get; set; }
  45. /// <summary>
  46. /// Gets or sets a value indicating whether [refresh library].
  47. /// </summary>
  48. /// <value><c>true</c> if [refresh library]; otherwise, <c>false</c>.</value>
  49. public bool RefreshLibrary { get; set; }
  50. /// <summary>
  51. /// Gets or sets the path.
  52. /// </summary>
  53. /// <value>The path.</value>
  54. public string[] Paths { get; set; }
  55. public LibraryOptions LibraryOptions { get; set; }
  56. }
  57. [Route("/Library/VirtualFolders", "DELETE")]
  58. public class RemoveVirtualFolder : IReturnVoid
  59. {
  60. /// <summary>
  61. /// Gets or sets the name.
  62. /// </summary>
  63. /// <value>The name.</value>
  64. public string Name { get; set; }
  65. /// <summary>
  66. /// Gets or sets a value indicating whether [refresh library].
  67. /// </summary>
  68. /// <value><c>true</c> if [refresh library]; otherwise, <c>false</c>.</value>
  69. public bool RefreshLibrary { get; set; }
  70. }
  71. [Route("/Library/VirtualFolders/Name", "POST")]
  72. public class RenameVirtualFolder : IReturnVoid
  73. {
  74. /// <summary>
  75. /// Gets or sets the name.
  76. /// </summary>
  77. /// <value>The name.</value>
  78. public string Name { get; set; }
  79. /// <summary>
  80. /// Gets or sets the name.
  81. /// </summary>
  82. /// <value>The name.</value>
  83. public string NewName { get; set; }
  84. /// <summary>
  85. /// Gets or sets a value indicating whether [refresh library].
  86. /// </summary>
  87. /// <value><c>true</c> if [refresh library]; otherwise, <c>false</c>.</value>
  88. public bool RefreshLibrary { get; set; }
  89. }
  90. [Route("/Library/VirtualFolders/Paths", "POST")]
  91. public class AddMediaPath : IReturnVoid
  92. {
  93. /// <summary>
  94. /// Gets or sets the name.
  95. /// </summary>
  96. /// <value>The name.</value>
  97. public string Name { get; set; }
  98. /// <summary>
  99. /// Gets or sets the name.
  100. /// </summary>
  101. /// <value>The name.</value>
  102. public string Path { get; set; }
  103. public MediaPathInfo PathInfo { get; set; }
  104. /// <summary>
  105. /// Gets or sets a value indicating whether [refresh library].
  106. /// </summary>
  107. /// <value><c>true</c> if [refresh library]; otherwise, <c>false</c>.</value>
  108. public bool RefreshLibrary { get; set; }
  109. }
  110. [Route("/Library/VirtualFolders/Paths/Update", "POST")]
  111. public class UpdateMediaPath : IReturnVoid
  112. {
  113. /// <summary>
  114. /// Gets or sets the name.
  115. /// </summary>
  116. /// <value>The name.</value>
  117. public string Name { get; set; }
  118. public MediaPathInfo PathInfo { get; set; }
  119. }
  120. [Route("/Library/VirtualFolders/Paths", "DELETE")]
  121. public class RemoveMediaPath : IReturnVoid
  122. {
  123. /// <summary>
  124. /// Gets or sets the name.
  125. /// </summary>
  126. /// <value>The name.</value>
  127. public string Name { get; set; }
  128. /// <summary>
  129. /// Gets or sets the name.
  130. /// </summary>
  131. /// <value>The name.</value>
  132. public string Path { get; set; }
  133. /// <summary>
  134. /// Gets or sets a value indicating whether [refresh library].
  135. /// </summary>
  136. /// <value><c>true</c> if [refresh library]; otherwise, <c>false</c>.</value>
  137. public bool RefreshLibrary { get; set; }
  138. }
  139. [Route("/Library/VirtualFolders/LibraryOptions", "POST")]
  140. public class UpdateLibraryOptions : IReturnVoid
  141. {
  142. public string Id { get; set; }
  143. public LibraryOptions LibraryOptions { get; set; }
  144. }
  145. /// <summary>
  146. /// Class LibraryStructureService
  147. /// </summary>
  148. [Authenticated(Roles = "Admin", AllowBeforeStartupWizard = true)]
  149. public class LibraryStructureService : BaseApiService
  150. {
  151. /// <summary>
  152. /// The _app paths
  153. /// </summary>
  154. private readonly IServerApplicationPaths _appPaths;
  155. /// <summary>
  156. /// The _library manager
  157. /// </summary>
  158. private readonly ILibraryManager _libraryManager;
  159. private readonly ILibraryMonitor _libraryMonitor;
  160. private readonly IFileSystem _fileSystem;
  161. /// <summary>
  162. /// Initializes a new instance of the <see cref="LibraryStructureService" /> class.
  163. /// </summary>
  164. public LibraryStructureService(IServerApplicationPaths appPaths, ILibraryManager libraryManager, ILibraryMonitor libraryMonitor, IFileSystem fileSystem)
  165. {
  166. if (appPaths == null)
  167. {
  168. throw new ArgumentNullException("appPaths");
  169. }
  170. _appPaths = appPaths;
  171. _libraryManager = libraryManager;
  172. _libraryMonitor = libraryMonitor;
  173. _fileSystem = fileSystem;
  174. }
  175. /// <summary>
  176. /// Gets the specified request.
  177. /// </summary>
  178. /// <param name="request">The request.</param>
  179. /// <returns>System.Object.</returns>
  180. public object Get(GetVirtualFolders request)
  181. {
  182. var result = _libraryManager.GetVirtualFolders(true);
  183. return ToOptimizedSerializedResultUsingCache(result);
  184. }
  185. public void Post(UpdateLibraryOptions request)
  186. {
  187. var collectionFolder = (CollectionFolder)_libraryManager.GetItemById(request.Id);
  188. collectionFolder.UpdateLibraryOptions(request.LibraryOptions);
  189. }
  190. /// <summary>
  191. /// Posts the specified request.
  192. /// </summary>
  193. /// <param name="request">The request.</param>
  194. public void Post(AddVirtualFolder request)
  195. {
  196. var libraryOptions = request.LibraryOptions ?? new LibraryOptions();
  197. if (request.Paths != null && request.Paths.Length > 0)
  198. {
  199. libraryOptions.PathInfos = request.Paths.Select(i => new MediaPathInfo { Path = i }).ToArray();
  200. }
  201. _libraryManager.AddVirtualFolder(request.Name, request.CollectionType, libraryOptions, request.RefreshLibrary);
  202. }
  203. /// <summary>
  204. /// Posts the specified request.
  205. /// </summary>
  206. /// <param name="request">The request.</param>
  207. public void Post(RenameVirtualFolder request)
  208. {
  209. if (string.IsNullOrWhiteSpace(request.Name))
  210. {
  211. throw new ArgumentNullException("request");
  212. }
  213. if (string.IsNullOrWhiteSpace(request.NewName))
  214. {
  215. throw new ArgumentNullException("request");
  216. }
  217. var rootFolderPath = _appPaths.DefaultUserViewsPath;
  218. var currentPath = Path.Combine(rootFolderPath, request.Name);
  219. var newPath = Path.Combine(rootFolderPath, request.NewName);
  220. if (!_fileSystem.DirectoryExists(currentPath))
  221. {
  222. throw new FileNotFoundException("The media collection does not exist");
  223. }
  224. if (!string.Equals(currentPath, newPath, StringComparison.OrdinalIgnoreCase) && _fileSystem.DirectoryExists(newPath))
  225. {
  226. throw new ArgumentException("There is already a media collection with the name " + newPath + ".");
  227. }
  228. _libraryMonitor.Stop();
  229. try
  230. {
  231. // Only make a two-phase move when changing capitalization
  232. if (string.Equals(currentPath, newPath, StringComparison.OrdinalIgnoreCase))
  233. {
  234. //Create an unique name
  235. var temporaryName = Guid.NewGuid().ToString();
  236. var temporaryPath = Path.Combine(rootFolderPath, temporaryName);
  237. _fileSystem.MoveDirectory(currentPath, temporaryPath);
  238. currentPath = temporaryPath;
  239. }
  240. _fileSystem.MoveDirectory(currentPath, newPath);
  241. }
  242. finally
  243. {
  244. Task.Run(() =>
  245. {
  246. // No need to start if scanning the library because it will handle it
  247. if (request.RefreshLibrary)
  248. {
  249. _libraryManager.ValidateMediaLibrary(new SimpleProgress<double>(), CancellationToken.None);
  250. }
  251. else
  252. {
  253. // Need to add a delay here or directory watchers may still pick up the changes
  254. var task = Task.Delay(1000);
  255. // Have to block here to allow exceptions to bubble
  256. Task.WaitAll(task);
  257. _libraryMonitor.Start();
  258. }
  259. });
  260. }
  261. }
  262. /// <summary>
  263. /// Deletes the specified request.
  264. /// </summary>
  265. /// <param name="request">The request.</param>
  266. public void Delete(RemoveVirtualFolder request)
  267. {
  268. _libraryManager.RemoveVirtualFolder(request.Name, request.RefreshLibrary);
  269. }
  270. /// <summary>
  271. /// Posts the specified request.
  272. /// </summary>
  273. /// <param name="request">The request.</param>
  274. public void Post(AddMediaPath request)
  275. {
  276. if (string.IsNullOrWhiteSpace(request.Name))
  277. {
  278. throw new ArgumentNullException("request");
  279. }
  280. _libraryMonitor.Stop();
  281. try
  282. {
  283. var mediaPath = request.PathInfo;
  284. if (mediaPath == null)
  285. {
  286. mediaPath = new MediaPathInfo
  287. {
  288. Path = request.Path
  289. };
  290. }
  291. _libraryManager.AddMediaPath(request.Name, mediaPath);
  292. }
  293. finally
  294. {
  295. Task.Run(() =>
  296. {
  297. // No need to start if scanning the library because it will handle it
  298. if (request.RefreshLibrary)
  299. {
  300. _libraryManager.ValidateMediaLibrary(new SimpleProgress<double>(), CancellationToken.None);
  301. }
  302. else
  303. {
  304. // Need to add a delay here or directory watchers may still pick up the changes
  305. var task = Task.Delay(1000);
  306. // Have to block here to allow exceptions to bubble
  307. Task.WaitAll(task);
  308. _libraryMonitor.Start();
  309. }
  310. });
  311. }
  312. }
  313. /// <summary>
  314. /// Posts the specified request.
  315. /// </summary>
  316. /// <param name="request">The request.</param>
  317. public void Post(UpdateMediaPath request)
  318. {
  319. if (string.IsNullOrWhiteSpace(request.Name))
  320. {
  321. throw new ArgumentNullException("request");
  322. }
  323. _libraryManager.UpdateMediaPath(request.Name, request.PathInfo);
  324. }
  325. /// <summary>
  326. /// Deletes the specified request.
  327. /// </summary>
  328. /// <param name="request">The request.</param>
  329. public void Delete(RemoveMediaPath request)
  330. {
  331. if (string.IsNullOrWhiteSpace(request.Name))
  332. {
  333. throw new ArgumentNullException("request");
  334. }
  335. _libraryMonitor.Stop();
  336. try
  337. {
  338. _libraryManager.RemoveMediaPath(request.Name, request.Path);
  339. }
  340. finally
  341. {
  342. Task.Run(() =>
  343. {
  344. // No need to start if scanning the library because it will handle it
  345. if (request.RefreshLibrary)
  346. {
  347. _libraryManager.ValidateMediaLibrary(new SimpleProgress<double>(), CancellationToken.None);
  348. }
  349. else
  350. {
  351. // Need to add a delay here or directory watchers may still pick up the changes
  352. var task = Task.Delay(1000);
  353. // Have to block here to allow exceptions to bubble
  354. Task.WaitAll(task);
  355. _libraryMonitor.Start();
  356. }
  357. });
  358. }
  359. }
  360. }
  361. }