LibraryStructureService.cs 14 KB

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