LibraryStructureService.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. using System.Threading.Tasks;
  2. using MediaBrowser.Controller;
  3. using MediaBrowser.Controller.IO;
  4. using MediaBrowser.Controller.Library;
  5. using MediaBrowser.Model.Entities;
  6. using ServiceStack.ServiceHost;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Threading;
  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 async 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. await Task.Delay(1000).ConfigureAwait(false);
  221. }
  222. finally
  223. {
  224. _directoryWatchers.Start();
  225. }
  226. if (request.RefreshLibrary)
  227. {
  228. _libraryManager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None);
  229. }
  230. }
  231. /// <summary>
  232. /// Posts the specified request.
  233. /// </summary>
  234. /// <param name="request">The request.</param>
  235. public async void Post(RenameVirtualFolder request)
  236. {
  237. _directoryWatchers.Stop();
  238. try
  239. {
  240. if (string.IsNullOrEmpty(request.UserId))
  241. {
  242. LibraryHelpers.RenameVirtualFolder(request.Name, request.NewName, null, _appPaths);
  243. }
  244. else
  245. {
  246. var user = _userManager.GetUserById(new Guid(request.UserId));
  247. LibraryHelpers.RenameVirtualFolder(request.Name, request.NewName, user, _appPaths);
  248. }
  249. // Need to add a delay here or directory watchers may still pick up the changes
  250. await Task.Delay(1000).ConfigureAwait(false);
  251. }
  252. finally
  253. {
  254. _directoryWatchers.Start();
  255. }
  256. if (request.RefreshLibrary)
  257. {
  258. _libraryManager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None);
  259. }
  260. }
  261. /// <summary>
  262. /// Deletes the specified request.
  263. /// </summary>
  264. /// <param name="request">The request.</param>
  265. public async void Delete(RemoveVirtualFolder request)
  266. {
  267. _directoryWatchers.Stop();
  268. try
  269. {
  270. if (string.IsNullOrEmpty(request.UserId))
  271. {
  272. LibraryHelpers.RemoveVirtualFolder(request.Name, null, _appPaths);
  273. }
  274. else
  275. {
  276. var user = _userManager.GetUserById(new Guid(request.UserId));
  277. LibraryHelpers.RemoveVirtualFolder(request.Name, user, _appPaths);
  278. }
  279. // Need to add a delay here or directory watchers may still pick up the changes
  280. await Task.Delay(1000).ConfigureAwait(false);
  281. }
  282. finally
  283. {
  284. _directoryWatchers.Start();
  285. }
  286. if (request.RefreshLibrary)
  287. {
  288. _libraryManager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None);
  289. }
  290. }
  291. /// <summary>
  292. /// Posts the specified request.
  293. /// </summary>
  294. /// <param name="request">The request.</param>
  295. public async void Post(AddMediaPath request)
  296. {
  297. _directoryWatchers.Stop();
  298. try
  299. {
  300. if (string.IsNullOrEmpty(request.UserId))
  301. {
  302. LibraryHelpers.AddMediaPath(request.Name, request.Path, null, _appPaths);
  303. }
  304. else
  305. {
  306. var user = _userManager.GetUserById(new Guid(request.UserId));
  307. LibraryHelpers.AddMediaPath(request.Name, request.Path, user, _appPaths);
  308. }
  309. // Need to add a delay here or directory watchers may still pick up the changes
  310. await Task.Delay(1000).ConfigureAwait(false);
  311. }
  312. finally
  313. {
  314. _directoryWatchers.Start();
  315. }
  316. if (request.RefreshLibrary)
  317. {
  318. _libraryManager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None);
  319. }
  320. }
  321. /// <summary>
  322. /// Deletes the specified request.
  323. /// </summary>
  324. /// <param name="request">The request.</param>
  325. public async void Delete(RemoveMediaPath request)
  326. {
  327. _directoryWatchers.Stop();
  328. try
  329. {
  330. if (string.IsNullOrEmpty(request.UserId))
  331. {
  332. LibraryHelpers.RemoveMediaPath(request.Name, request.Path, null, _appPaths);
  333. }
  334. else
  335. {
  336. var user = _userManager.GetUserById(new Guid(request.UserId));
  337. LibraryHelpers.RemoveMediaPath(request.Name, request.Path, user, _appPaths);
  338. }
  339. // Need to add a delay here or directory watchers may still pick up the changes
  340. await Task.Delay(1000).ConfigureAwait(false);
  341. }
  342. finally
  343. {
  344. _directoryWatchers.Start();
  345. }
  346. if (request.RefreshLibrary)
  347. {
  348. _libraryManager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None);
  349. }
  350. }
  351. }
  352. }