LibraryStructureService.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. using MediaBrowser.Controller;
  2. using MediaBrowser.Controller.IO;
  3. using MediaBrowser.Controller.Library;
  4. using MediaBrowser.Model.Entities;
  5. using ServiceStack.ServiceHost;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. namespace MediaBrowser.Api.Library
  12. {
  13. /// <summary>
  14. /// Class GetDefaultVirtualFolders
  15. /// </summary>
  16. [Route("/Library/VirtualFolders", "GET")]
  17. [Route("/Users/{UserId}/VirtualFolders", "GET")]
  18. public class GetVirtualFolders : IReturn<List<VirtualFolderInfo>>
  19. {
  20. /// <summary>
  21. /// Gets or sets the user id.
  22. /// </summary>
  23. /// <value>The user id.</value>
  24. public string UserId { get; set; }
  25. }
  26. [Route("/Library/VirtualFolders/{Name}", "POST")]
  27. [Route("/Users/{UserId}/VirtualFolders/{Name}", "POST")]
  28. public class AddVirtualFolder : IReturnVoid
  29. {
  30. /// <summary>
  31. /// Gets or sets the user id.
  32. /// </summary>
  33. /// <value>The user id.</value>
  34. public string UserId { get; set; }
  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. }
  51. [Route("/Library/VirtualFolders/{Name}", "DELETE")]
  52. [Route("/Users/{UserId}/VirtualFolders/{Name}", "DELETE")]
  53. public class RemoveVirtualFolder : IReturnVoid
  54. {
  55. /// <summary>
  56. /// Gets or sets the user id.
  57. /// </summary>
  58. /// <value>The user id.</value>
  59. public string UserId { get; set; }
  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}/Name", "POST")]
  72. [Route("/Users/{UserId}/VirtualFolders/{Name}/Name", "POST")]
  73. public class RenameVirtualFolder : IReturnVoid
  74. {
  75. /// <summary>
  76. /// Gets or sets the user id.
  77. /// </summary>
  78. /// <value>The user id.</value>
  79. public string UserId { get; set; }
  80. /// <summary>
  81. /// Gets or sets the name.
  82. /// </summary>
  83. /// <value>The name.</value>
  84. public string Name { get; set; }
  85. /// <summary>
  86. /// Gets or sets the name.
  87. /// </summary>
  88. /// <value>The name.</value>
  89. public string NewName { get; set; }
  90. /// <summary>
  91. /// Gets or sets a value indicating whether [refresh library].
  92. /// </summary>
  93. /// <value><c>true</c> if [refresh library]; otherwise, <c>false</c>.</value>
  94. public bool RefreshLibrary { get; set; }
  95. }
  96. [Route("/Library/VirtualFolders/{Name}/Paths", "POST")]
  97. [Route("/Users/{UserId}/VirtualFolders/{Name}/Paths", "POST")]
  98. public class AddMediaPath : IReturnVoid
  99. {
  100. /// <summary>
  101. /// Gets or sets the user id.
  102. /// </summary>
  103. /// <value>The user id.</value>
  104. public string UserId { get; set; }
  105. /// <summary>
  106. /// Gets or sets the name.
  107. /// </summary>
  108. /// <value>The name.</value>
  109. public string Name { get; set; }
  110. /// <summary>
  111. /// Gets or sets the name.
  112. /// </summary>
  113. /// <value>The name.</value>
  114. public string Path { get; set; }
  115. /// <summary>
  116. /// Gets or sets a value indicating whether [refresh library].
  117. /// </summary>
  118. /// <value><c>true</c> if [refresh library]; otherwise, <c>false</c>.</value>
  119. public bool RefreshLibrary { get; set; }
  120. }
  121. [Route("/Library/VirtualFolders/{Name}/Paths", "DELETE")]
  122. [Route("/Users/{UserId}/VirtualFolders/{Name}/Paths", "DELETE")]
  123. public class RemoveMediaPath : IReturnVoid
  124. {
  125. /// <summary>
  126. /// Gets or sets the user id.
  127. /// </summary>
  128. /// <value>The user id.</value>
  129. public string UserId { get; set; }
  130. /// <summary>
  131. /// Gets or sets the name.
  132. /// </summary>
  133. /// <value>The name.</value>
  134. public string Name { get; set; }
  135. /// <summary>
  136. /// Gets or sets the name.
  137. /// </summary>
  138. /// <value>The name.</value>
  139. public string Path { get; set; }
  140. /// <summary>
  141. /// Gets or sets a value indicating whether [refresh library].
  142. /// </summary>
  143. /// <value><c>true</c> if [refresh library]; otherwise, <c>false</c>.</value>
  144. public bool RefreshLibrary { get; set; }
  145. }
  146. /// <summary>
  147. /// Class LibraryStructureService
  148. /// </summary>
  149. public class LibraryStructureService : BaseApiService
  150. {
  151. /// <summary>
  152. /// The _app paths
  153. /// </summary>
  154. private readonly IServerApplicationPaths _appPaths;
  155. /// <summary>
  156. /// The _user manager
  157. /// </summary>
  158. private readonly IUserManager _userManager;
  159. /// <summary>
  160. /// The _library manager
  161. /// </summary>
  162. private readonly ILibraryManager _libraryManager;
  163. private readonly IDirectoryWatchers _directoryWatchers;
  164. /// <summary>
  165. /// Initializes a new instance of the <see cref="LibraryStructureService"/> class.
  166. /// </summary>
  167. /// <param name="appPaths">The app paths.</param>
  168. /// <param name="userManager">The user manager.</param>
  169. /// <param name="libraryManager">The library manager.</param>
  170. /// <exception cref="System.ArgumentNullException">appPaths</exception>
  171. public LibraryStructureService(IServerApplicationPaths appPaths, IUserManager userManager, ILibraryManager libraryManager, IDirectoryWatchers directoryWatchers)
  172. {
  173. if (appPaths == null)
  174. {
  175. throw new ArgumentNullException("appPaths");
  176. }
  177. _userManager = userManager;
  178. _appPaths = appPaths;
  179. _libraryManager = libraryManager;
  180. _directoryWatchers = directoryWatchers;
  181. }
  182. /// <summary>
  183. /// Gets the specified request.
  184. /// </summary>
  185. /// <param name="request">The request.</param>
  186. /// <returns>System.Object.</returns>
  187. public object Get(GetVirtualFolders request)
  188. {
  189. if (string.IsNullOrEmpty(request.UserId))
  190. {
  191. var result = _libraryManager.GetDefaultVirtualFolders().OrderBy(i => i.Name).ToList();
  192. return ToOptimizedResult(result);
  193. }
  194. else
  195. {
  196. var user = _userManager.GetUserById(new Guid(request.UserId));
  197. var result = _libraryManager.GetVirtualFolders(user).OrderBy(i => i.Name).ToList();
  198. return ToOptimizedResult(result);
  199. }
  200. }
  201. /// <summary>
  202. /// Posts the specified request.
  203. /// </summary>
  204. /// <param name="request">The request.</param>
  205. public void Post(AddVirtualFolder request)
  206. {
  207. _directoryWatchers.Stop();
  208. try
  209. {
  210. if (string.IsNullOrEmpty(request.UserId))
  211. {
  212. LibraryHelpers.AddVirtualFolder(request.Name, request.CollectionType, null, _appPaths);
  213. }
  214. else
  215. {
  216. var user = _userManager.GetUserById(new Guid(request.UserId));
  217. LibraryHelpers.AddVirtualFolder(request.Name, request.CollectionType, user, _appPaths);
  218. }
  219. // Need to add a delay here or directory watchers may still pick up the changes
  220. var task = Task.Delay(1000);
  221. // Have to block here to allow exceptions to bubble
  222. Task.WaitAll(task);
  223. }
  224. finally
  225. {
  226. _directoryWatchers.Start();
  227. }
  228. if (request.RefreshLibrary)
  229. {
  230. _libraryManager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None);
  231. }
  232. }
  233. /// <summary>
  234. /// Posts the specified request.
  235. /// </summary>
  236. /// <param name="request">The request.</param>
  237. public void Post(RenameVirtualFolder request)
  238. {
  239. _directoryWatchers.Stop();
  240. try
  241. {
  242. if (string.IsNullOrEmpty(request.UserId))
  243. {
  244. LibraryHelpers.RenameVirtualFolder(request.Name, request.NewName, null, _appPaths);
  245. }
  246. else
  247. {
  248. var user = _userManager.GetUserById(new Guid(request.UserId));
  249. LibraryHelpers.RenameVirtualFolder(request.Name, request.NewName, user, _appPaths);
  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. }
  256. finally
  257. {
  258. _directoryWatchers.Start();
  259. }
  260. if (request.RefreshLibrary)
  261. {
  262. _libraryManager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None);
  263. }
  264. }
  265. /// <summary>
  266. /// Deletes the specified request.
  267. /// </summary>
  268. /// <param name="request">The request.</param>
  269. public void Delete(RemoveVirtualFolder request)
  270. {
  271. _directoryWatchers.Stop();
  272. try
  273. {
  274. if (string.IsNullOrEmpty(request.UserId))
  275. {
  276. LibraryHelpers.RemoveVirtualFolder(request.Name, null, _appPaths);
  277. }
  278. else
  279. {
  280. var user = _userManager.GetUserById(new Guid(request.UserId));
  281. LibraryHelpers.RemoveVirtualFolder(request.Name, user, _appPaths);
  282. }
  283. // Need to add a delay here or directory watchers may still pick up the changes
  284. var task = Task.Delay(1000);
  285. // Have to block here to allow exceptions to bubble
  286. Task.WaitAll(task);
  287. }
  288. finally
  289. {
  290. _directoryWatchers.Start();
  291. }
  292. if (request.RefreshLibrary)
  293. {
  294. _libraryManager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None);
  295. }
  296. }
  297. /// <summary>
  298. /// Posts the specified request.
  299. /// </summary>
  300. /// <param name="request">The request.</param>
  301. public void Post(AddMediaPath request)
  302. {
  303. _directoryWatchers.Stop();
  304. try
  305. {
  306. if (string.IsNullOrEmpty(request.UserId))
  307. {
  308. LibraryHelpers.AddMediaPath(request.Name, request.Path, null, _appPaths);
  309. }
  310. else
  311. {
  312. var user = _userManager.GetUserById(new Guid(request.UserId));
  313. LibraryHelpers.AddMediaPath(request.Name, request.Path, user, _appPaths);
  314. }
  315. // Need to add a delay here or directory watchers may still pick up the changes
  316. var task = Task.Delay(1000);
  317. // Have to block here to allow exceptions to bubble
  318. Task.WaitAll(task);
  319. }
  320. finally
  321. {
  322. _directoryWatchers.Start();
  323. }
  324. if (request.RefreshLibrary)
  325. {
  326. _libraryManager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None);
  327. }
  328. }
  329. /// <summary>
  330. /// Deletes the specified request.
  331. /// </summary>
  332. /// <param name="request">The request.</param>
  333. public void Delete(RemoveMediaPath request)
  334. {
  335. _directoryWatchers.Stop();
  336. try
  337. {
  338. if (string.IsNullOrEmpty(request.UserId))
  339. {
  340. LibraryHelpers.RemoveMediaPath(request.Name, request.Path, null, _appPaths);
  341. }
  342. else
  343. {
  344. var user = _userManager.GetUserById(new Guid(request.UserId));
  345. LibraryHelpers.RemoveMediaPath(request.Name, request.Path, user, _appPaths);
  346. }
  347. // Need to add a delay here or directory watchers may still pick up the changes
  348. var task = Task.Delay(1000);
  349. // Have to block here to allow exceptions to bubble
  350. Task.WaitAll(task);
  351. }
  352. finally
  353. {
  354. _directoryWatchers.Start();
  355. }
  356. if (request.RefreshLibrary)
  357. {
  358. _libraryManager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None);
  359. }
  360. }
  361. }
  362. }