LibraryStructureService.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. using MediaBrowser.Controller;
  2. using MediaBrowser.Controller.Library;
  3. using MediaBrowser.Controller.Net;
  4. using MediaBrowser.Model.Entities;
  5. using ServiceStack;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using CommonIO;
  13. using MediaBrowser.Controller.Configuration;
  14. using MediaBrowser.Controller.Entities;
  15. using MediaBrowser.Model.Configuration;
  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("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().OrderBy(i => i.Name).ToList();
  181. return ToOptimizedSerializedResultUsingCache(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 void 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. _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("request");
  210. }
  211. if (string.IsNullOrWhiteSpace(request.NewName))
  212. {
  213. throw new ArgumentNullException("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 DirectoryNotFoundException("The media collection does not exist");
  221. }
  222. if (!string.Equals(currentPath, newPath, StringComparison.OrdinalIgnoreCase) && _fileSystem.DirectoryExists(newPath))
  223. {
  224. throw new ArgumentException("There is already a media collection with the name " + newPath + ".");
  225. }
  226. _libraryMonitor.Stop();
  227. try
  228. {
  229. // Only make a two-phase move when changing capitalization
  230. if (string.Equals(currentPath, newPath, StringComparison.OrdinalIgnoreCase))
  231. {
  232. //Create an unique name
  233. var temporaryName = Guid.NewGuid().ToString();
  234. var temporaryPath = Path.Combine(rootFolderPath, temporaryName);
  235. _fileSystem.MoveDirectory(currentPath, temporaryPath);
  236. currentPath = temporaryPath;
  237. }
  238. _fileSystem.MoveDirectory(currentPath, newPath);
  239. }
  240. finally
  241. {
  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 Progress<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 void Delete(RemoveVirtualFolder request)
  265. {
  266. _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("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 Progress<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("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("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 Progress<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. }