LibraryStructureService.cs 13 KB

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