LibraryStructureService.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. private readonly IFileSystem _fileSystem;
  165. /// <summary>
  166. /// Initializes a new instance of the <see cref="LibraryStructureService"/> class.
  167. /// </summary>
  168. /// <param name="appPaths">The app paths.</param>
  169. /// <param name="userManager">The user manager.</param>
  170. /// <param name="libraryManager">The library manager.</param>
  171. /// <exception cref="System.ArgumentNullException">appPaths</exception>
  172. public LibraryStructureService(IServerApplicationPaths appPaths, IUserManager userManager, ILibraryManager libraryManager, IDirectoryWatchers directoryWatchers, IFileSystem fileSystem)
  173. {
  174. if (appPaths == null)
  175. {
  176. throw new ArgumentNullException("appPaths");
  177. }
  178. _userManager = userManager;
  179. _appPaths = appPaths;
  180. _libraryManager = libraryManager;
  181. _directoryWatchers = directoryWatchers;
  182. _fileSystem = fileSystem;
  183. }
  184. /// <summary>
  185. /// Gets the specified request.
  186. /// </summary>
  187. /// <param name="request">The request.</param>
  188. /// <returns>System.Object.</returns>
  189. public object Get(GetVirtualFolders request)
  190. {
  191. if (string.IsNullOrEmpty(request.UserId))
  192. {
  193. var result = _libraryManager.GetDefaultVirtualFolders().OrderBy(i => i.Name).ToList();
  194. return ToOptimizedResult(result);
  195. }
  196. else
  197. {
  198. var user = _userManager.GetUserById(new Guid(request.UserId));
  199. var result = _libraryManager.GetVirtualFolders(user).OrderBy(i => i.Name).ToList();
  200. return ToOptimizedResult(result);
  201. }
  202. }
  203. /// <summary>
  204. /// Posts the specified request.
  205. /// </summary>
  206. /// <param name="request">The request.</param>
  207. public void Post(AddVirtualFolder request)
  208. {
  209. _directoryWatchers.Stop();
  210. try
  211. {
  212. if (string.IsNullOrEmpty(request.UserId))
  213. {
  214. LibraryHelpers.AddVirtualFolder(_fileSystem, request.Name, request.CollectionType, null, _appPaths);
  215. }
  216. else
  217. {
  218. var user = _userManager.GetUserById(new Guid(request.UserId));
  219. LibraryHelpers.AddVirtualFolder(_fileSystem, request.Name, request.CollectionType, user, _appPaths);
  220. }
  221. // Need to add a delay here or directory watchers may still pick up the changes
  222. var task = Task.Delay(1000);
  223. // Have to block here to allow exceptions to bubble
  224. Task.WaitAll(task);
  225. }
  226. finally
  227. {
  228. _directoryWatchers.Start();
  229. }
  230. if (request.RefreshLibrary)
  231. {
  232. _libraryManager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None);
  233. }
  234. }
  235. /// <summary>
  236. /// Posts the specified request.
  237. /// </summary>
  238. /// <param name="request">The request.</param>
  239. public void Post(RenameVirtualFolder request)
  240. {
  241. _directoryWatchers.Stop();
  242. try
  243. {
  244. if (string.IsNullOrEmpty(request.UserId))
  245. {
  246. LibraryHelpers.RenameVirtualFolder(request.Name, request.NewName, null, _appPaths);
  247. }
  248. else
  249. {
  250. var user = _userManager.GetUserById(new Guid(request.UserId));
  251. LibraryHelpers.RenameVirtualFolder(request.Name, request.NewName, user, _appPaths);
  252. }
  253. // Need to add a delay here or directory watchers may still pick up the changes
  254. var task = Task.Delay(1000);
  255. // Have to block here to allow exceptions to bubble
  256. Task.WaitAll(task);
  257. }
  258. finally
  259. {
  260. _directoryWatchers.Start();
  261. }
  262. if (request.RefreshLibrary)
  263. {
  264. _libraryManager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None);
  265. }
  266. }
  267. /// <summary>
  268. /// Deletes the specified request.
  269. /// </summary>
  270. /// <param name="request">The request.</param>
  271. public void Delete(RemoveVirtualFolder request)
  272. {
  273. _directoryWatchers.Stop();
  274. try
  275. {
  276. if (string.IsNullOrEmpty(request.UserId))
  277. {
  278. LibraryHelpers.RemoveVirtualFolder(request.Name, null, _appPaths);
  279. }
  280. else
  281. {
  282. var user = _userManager.GetUserById(new Guid(request.UserId));
  283. LibraryHelpers.RemoveVirtualFolder(request.Name, user, _appPaths);
  284. }
  285. // Need to add a delay here or directory watchers may still pick up the changes
  286. var task = Task.Delay(1000);
  287. // Have to block here to allow exceptions to bubble
  288. Task.WaitAll(task);
  289. }
  290. finally
  291. {
  292. _directoryWatchers.Start();
  293. }
  294. if (request.RefreshLibrary)
  295. {
  296. _libraryManager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None);
  297. }
  298. }
  299. /// <summary>
  300. /// Posts the specified request.
  301. /// </summary>
  302. /// <param name="request">The request.</param>
  303. public void Post(AddMediaPath request)
  304. {
  305. _directoryWatchers.Stop();
  306. try
  307. {
  308. if (string.IsNullOrEmpty(request.UserId))
  309. {
  310. LibraryHelpers.AddMediaPath(_fileSystem, request.Name, request.Path, null, _appPaths);
  311. }
  312. else
  313. {
  314. var user = _userManager.GetUserById(new Guid(request.UserId));
  315. LibraryHelpers.AddMediaPath(_fileSystem, request.Name, request.Path, user, _appPaths);
  316. }
  317. // Need to add a delay here or directory watchers may still pick up the changes
  318. var task = Task.Delay(1000);
  319. // Have to block here to allow exceptions to bubble
  320. Task.WaitAll(task);
  321. }
  322. finally
  323. {
  324. _directoryWatchers.Start();
  325. }
  326. if (request.RefreshLibrary)
  327. {
  328. _libraryManager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None);
  329. }
  330. }
  331. /// <summary>
  332. /// Deletes the specified request.
  333. /// </summary>
  334. /// <param name="request">The request.</param>
  335. public void Delete(RemoveMediaPath request)
  336. {
  337. _directoryWatchers.Stop();
  338. try
  339. {
  340. if (string.IsNullOrEmpty(request.UserId))
  341. {
  342. LibraryHelpers.RemoveMediaPath(_fileSystem, request.Name, request.Path, null, _appPaths);
  343. }
  344. else
  345. {
  346. var user = _userManager.GetUserById(new Guid(request.UserId));
  347. LibraryHelpers.RemoveMediaPath(_fileSystem, request.Name, request.Path, user, _appPaths);
  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. }
  354. finally
  355. {
  356. _directoryWatchers.Start();
  357. }
  358. if (request.RefreshLibrary)
  359. {
  360. _libraryManager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None);
  361. }
  362. }
  363. }
  364. }