LibraryStructureService.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using MediaBrowser.Common.Progress;
  9. using MediaBrowser.Controller;
  10. using MediaBrowser.Controller.Configuration;
  11. using MediaBrowser.Controller.Entities;
  12. using MediaBrowser.Controller.Library;
  13. using MediaBrowser.Controller.Net;
  14. using MediaBrowser.Model.Configuration;
  15. using MediaBrowser.Model.Entities;
  16. using MediaBrowser.Model.Services;
  17. using Microsoft.Extensions.Logging;
  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. /// <summary>
  161. /// Initializes a new instance of the <see cref="LibraryStructureService" /> class.
  162. /// </summary>
  163. public LibraryStructureService(
  164. ILogger<LibraryStructureService> logger,
  165. IServerConfigurationManager serverConfigurationManager,
  166. IHttpResultFactory httpResultFactory,
  167. ILibraryManager libraryManager,
  168. ILibraryMonitor libraryMonitor)
  169. : base(logger, serverConfigurationManager, httpResultFactory)
  170. {
  171. _appPaths = serverConfigurationManager.ApplicationPaths;
  172. _libraryManager = libraryManager;
  173. _libraryMonitor = libraryMonitor;
  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 ToOptimizedResult(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 Task 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. return _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(nameof(request));
  212. }
  213. if (string.IsNullOrWhiteSpace(request.NewName))
  214. {
  215. throw new ArgumentNullException(nameof(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 (!Directory.Exists(currentPath))
  221. {
  222. throw new FileNotFoundException("The media collection does not exist");
  223. }
  224. if (!string.Equals(currentPath, newPath, StringComparison.OrdinalIgnoreCase) && Directory.Exists(newPath))
  225. {
  226. throw new ArgumentException("Media library already exists at " + newPath + ".");
  227. }
  228. _libraryMonitor.Stop();
  229. try
  230. {
  231. // Changing capitalization. Handle windows case insensitivity
  232. if (string.Equals(currentPath, newPath, StringComparison.OrdinalIgnoreCase))
  233. {
  234. var tempPath = Path.Combine(rootFolderPath, Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture));
  235. Directory.Move(currentPath, tempPath);
  236. currentPath = tempPath;
  237. }
  238. Directory.Move(currentPath, newPath);
  239. }
  240. finally
  241. {
  242. CollectionFolder.OnCollectionFolderChange();
  243. Task.Run(() =>
  244. {
  245. // No need to start if scanning the library because it will handle it
  246. if (request.RefreshLibrary)
  247. {
  248. _libraryManager.ValidateMediaLibrary(new SimpleProgress<double>(), CancellationToken.None);
  249. }
  250. else
  251. {
  252. // Need to add a delay here or directory watchers may still pick up the changes
  253. var task = Task.Delay(1000);
  254. // Have to block here to allow exceptions to bubble
  255. Task.WaitAll(task);
  256. _libraryMonitor.Start();
  257. }
  258. });
  259. }
  260. }
  261. /// <summary>
  262. /// Deletes the specified request.
  263. /// </summary>
  264. /// <param name="request">The request.</param>
  265. public Task Delete(RemoveVirtualFolder request)
  266. {
  267. return _libraryManager.RemoveVirtualFolder(request.Name, request.RefreshLibrary);
  268. }
  269. /// <summary>
  270. /// Posts the specified request.
  271. /// </summary>
  272. /// <param name="request">The request.</param>
  273. public void Post(AddMediaPath request)
  274. {
  275. if (string.IsNullOrWhiteSpace(request.Name))
  276. {
  277. throw new ArgumentNullException(nameof(request));
  278. }
  279. _libraryMonitor.Stop();
  280. try
  281. {
  282. var mediaPath = request.PathInfo ?? new MediaPathInfo
  283. {
  284. Path = request.Path
  285. };
  286. _libraryManager.AddMediaPath(request.Name, mediaPath);
  287. }
  288. finally
  289. {
  290. Task.Run(() =>
  291. {
  292. // No need to start if scanning the library because it will handle it
  293. if (request.RefreshLibrary)
  294. {
  295. _libraryManager.ValidateMediaLibrary(new SimpleProgress<double>(), CancellationToken.None);
  296. }
  297. else
  298. {
  299. // Need to add a delay here or directory watchers may still pick up the changes
  300. var task = Task.Delay(1000);
  301. // Have to block here to allow exceptions to bubble
  302. Task.WaitAll(task);
  303. _libraryMonitor.Start();
  304. }
  305. });
  306. }
  307. }
  308. /// <summary>
  309. /// Posts the specified request.
  310. /// </summary>
  311. /// <param name="request">The request.</param>
  312. public void Post(UpdateMediaPath request)
  313. {
  314. if (string.IsNullOrWhiteSpace(request.Name))
  315. {
  316. throw new ArgumentNullException(nameof(request));
  317. }
  318. _libraryManager.UpdateMediaPath(request.Name, request.PathInfo);
  319. }
  320. /// <summary>
  321. /// Deletes the specified request.
  322. /// </summary>
  323. /// <param name="request">The request.</param>
  324. public void Delete(RemoveMediaPath request)
  325. {
  326. if (string.IsNullOrWhiteSpace(request.Name))
  327. {
  328. throw new ArgumentNullException(nameof(request));
  329. }
  330. _libraryMonitor.Stop();
  331. try
  332. {
  333. _libraryManager.RemoveMediaPath(request.Name, request.Path);
  334. }
  335. finally
  336. {
  337. Task.Run(() =>
  338. {
  339. // No need to start if scanning the library because it will handle it
  340. if (request.RefreshLibrary)
  341. {
  342. _libraryManager.ValidateMediaLibrary(new SimpleProgress<double>(), CancellationToken.None);
  343. }
  344. else
  345. {
  346. // Need to add a delay here or directory watchers may still pick up the changes
  347. var task = Task.Delay(1000);
  348. // Have to block here to allow exceptions to bubble
  349. Task.WaitAll(task);
  350. _libraryMonitor.Start();
  351. }
  352. });
  353. }
  354. }
  355. }
  356. }