LibraryStructureService.cs 13 KB

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