LibraryStructureService.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Controller;
  3. using MediaBrowser.Controller.Library;
  4. using MediaBrowser.Model.Entities;
  5. using MediaBrowser.Model.Logging;
  6. using ServiceStack;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. namespace MediaBrowser.Api.Library
  14. {
  15. /// <summary>
  16. /// Class GetDefaultVirtualFolders
  17. /// </summary>
  18. [Route("/Library/VirtualFolders", "GET")]
  19. [Route("/Users/{UserId}/VirtualFolders", "GET")]
  20. public class GetVirtualFolders : IReturn<List<VirtualFolderInfo>>
  21. {
  22. /// <summary>
  23. /// Gets or sets the user id.
  24. /// </summary>
  25. /// <value>The user id.</value>
  26. public string UserId { get; set; }
  27. }
  28. [Route("/Library/VirtualFolders", "POST")]
  29. public class AddVirtualFolder : IReturnVoid
  30. {
  31. /// <summary>
  32. /// Gets or sets the name.
  33. /// </summary>
  34. /// <value>The name.</value>
  35. public string Name { get; set; }
  36. /// <summary>
  37. /// Gets or sets the type of the collection.
  38. /// </summary>
  39. /// <value>The type of the collection.</value>
  40. public string CollectionType { get; set; }
  41. /// <summary>
  42. /// Gets or sets a value indicating whether [refresh library].
  43. /// </summary>
  44. /// <value><c>true</c> if [refresh library]; otherwise, <c>false</c>.</value>
  45. public bool RefreshLibrary { get; set; }
  46. }
  47. [Route("/Library/VirtualFolders", "DELETE")]
  48. public class RemoveVirtualFolder : IReturnVoid
  49. {
  50. /// <summary>
  51. /// Gets or sets the name.
  52. /// </summary>
  53. /// <value>The name.</value>
  54. public string Name { get; set; }
  55. /// <summary>
  56. /// Gets or sets a value indicating whether [refresh library].
  57. /// </summary>
  58. /// <value><c>true</c> if [refresh library]; otherwise, <c>false</c>.</value>
  59. public bool RefreshLibrary { get; set; }
  60. }
  61. [Route("/Library/VirtualFolders/Name", "POST")]
  62. public class RenameVirtualFolder : IReturnVoid
  63. {
  64. /// <summary>
  65. /// Gets or sets the name.
  66. /// </summary>
  67. /// <value>The name.</value>
  68. public string Name { get; set; }
  69. /// <summary>
  70. /// Gets or sets the name.
  71. /// </summary>
  72. /// <value>The name.</value>
  73. public string NewName { get; set; }
  74. /// <summary>
  75. /// Gets or sets a value indicating whether [refresh library].
  76. /// </summary>
  77. /// <value><c>true</c> if [refresh library]; otherwise, <c>false</c>.</value>
  78. public bool RefreshLibrary { get; set; }
  79. }
  80. [Route("/Library/VirtualFolders/Paths", "POST")]
  81. public class AddMediaPath : IReturnVoid
  82. {
  83. /// <summary>
  84. /// Gets or sets the name.
  85. /// </summary>
  86. /// <value>The name.</value>
  87. public string Name { get; set; }
  88. /// <summary>
  89. /// Gets or sets the name.
  90. /// </summary>
  91. /// <value>The name.</value>
  92. public string Path { get; set; }
  93. /// <summary>
  94. /// Gets or sets a value indicating whether [refresh library].
  95. /// </summary>
  96. /// <value><c>true</c> if [refresh library]; otherwise, <c>false</c>.</value>
  97. public bool RefreshLibrary { get; set; }
  98. }
  99. [Route("/Library/VirtualFolders/Paths", "DELETE")]
  100. public class RemoveMediaPath : IReturnVoid
  101. {
  102. /// <summary>
  103. /// Gets or sets the name.
  104. /// </summary>
  105. /// <value>The name.</value>
  106. public string Name { get; set; }
  107. /// <summary>
  108. /// Gets or sets the name.
  109. /// </summary>
  110. /// <value>The name.</value>
  111. public string Path { get; set; }
  112. /// <summary>
  113. /// Gets or sets a value indicating whether [refresh library].
  114. /// </summary>
  115. /// <value><c>true</c> if [refresh library]; otherwise, <c>false</c>.</value>
  116. public bool RefreshLibrary { get; set; }
  117. }
  118. [Route("/Library/Downloaded", "POST")]
  119. public class ReportContentDownloaded : IReturnVoid
  120. {
  121. [ApiMember(Name = "Path", Description = "The path being downloaded to.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  122. public string Path { get; set; }
  123. [ApiMember(Name = "ImageUrl", Description = "Optional thumbnail image url of the content.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
  124. public string ImageUrl { get; set; }
  125. [ApiMember(Name = "Name", Description = "The name of the content.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
  126. public string Name { get; set; }
  127. }
  128. [Route("/Library/Downloading", "POST")]
  129. public class ReportContentDownloading : IReturnVoid
  130. {
  131. [ApiMember(Name = "Path", Description = "The path being downloaded to.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  132. public string Path { get; set; }
  133. [ApiMember(Name = "ImageUrl", Description = "Optional thumbnail image url of the content.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
  134. public string ImageUrl { get; set; }
  135. [ApiMember(Name = "Name", Description = "The name of the content.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
  136. public string Name { get; set; }
  137. }
  138. /// <summary>
  139. /// Class LibraryStructureService
  140. /// </summary>
  141. public class LibraryStructureService : BaseApiService
  142. {
  143. /// <summary>
  144. /// The _app paths
  145. /// </summary>
  146. private readonly IServerApplicationPaths _appPaths;
  147. /// <summary>
  148. /// The _user manager
  149. /// </summary>
  150. private readonly IUserManager _userManager;
  151. /// <summary>
  152. /// The _library manager
  153. /// </summary>
  154. private readonly ILibraryManager _libraryManager;
  155. private readonly ILibraryMonitor _libraryMonitor;
  156. private readonly IFileSystem _fileSystem;
  157. private readonly ILogger _logger;
  158. /// <summary>
  159. /// Initializes a new instance of the <see cref="LibraryStructureService"/> class.
  160. /// </summary>
  161. /// <param name="appPaths">The app paths.</param>
  162. /// <param name="userManager">The user manager.</param>
  163. /// <param name="libraryManager">The library manager.</param>
  164. /// <exception cref="System.ArgumentNullException">appPaths</exception>
  165. public LibraryStructureService(IServerApplicationPaths appPaths, IUserManager userManager, ILibraryManager libraryManager, ILibraryMonitor libraryMonitor, IFileSystem fileSystem, ILogger logger)
  166. {
  167. if (appPaths == null)
  168. {
  169. throw new ArgumentNullException("appPaths");
  170. }
  171. _userManager = userManager;
  172. _appPaths = appPaths;
  173. _libraryManager = libraryManager;
  174. _libraryMonitor = libraryMonitor;
  175. _fileSystem = fileSystem;
  176. _logger = logger;
  177. }
  178. /// <summary>
  179. /// Gets the specified request.
  180. /// </summary>
  181. /// <param name="request">The request.</param>
  182. /// <returns>System.Object.</returns>
  183. public object Get(GetVirtualFolders request)
  184. {
  185. if (string.IsNullOrEmpty(request.UserId))
  186. {
  187. var result = _libraryManager.GetDefaultVirtualFolders().OrderBy(i => i.Name).ToList();
  188. return ToOptimizedSerializedResultUsingCache(result);
  189. }
  190. else
  191. {
  192. var user = _userManager.GetUserById(new Guid(request.UserId));
  193. var result = _libraryManager.GetVirtualFolders(user).OrderBy(i => i.Name).ToList();
  194. return ToOptimizedSerializedResultUsingCache(result);
  195. }
  196. }
  197. /// <summary>
  198. /// Posts the specified request.
  199. /// </summary>
  200. /// <param name="request">The request.</param>
  201. public void Post(AddVirtualFolder request)
  202. {
  203. if (string.IsNullOrWhiteSpace(request.Name))
  204. {
  205. throw new ArgumentNullException("request");
  206. }
  207. var name = _fileSystem.GetValidFilename(request.Name);
  208. var rootFolderPath = _appPaths.DefaultUserViewsPath;
  209. var virtualFolderPath = Path.Combine(rootFolderPath, name);
  210. if (Directory.Exists(virtualFolderPath))
  211. {
  212. throw new ArgumentException("There is already a media collection with the name " + name + ".");
  213. }
  214. _libraryMonitor.Stop();
  215. try
  216. {
  217. Directory.CreateDirectory(virtualFolderPath);
  218. if (!string.IsNullOrEmpty(request.CollectionType))
  219. {
  220. var path = Path.Combine(virtualFolderPath, request.CollectionType + ".collection");
  221. File.Create(path);
  222. }
  223. // Need to add a delay here or directory watchers may still pick up the changes
  224. var task = Task.Delay(1000);
  225. // Have to block here to allow exceptions to bubble
  226. Task.WaitAll(task);
  227. }
  228. finally
  229. {
  230. // No need to start if scanning the library because it will handle it
  231. if (!request.RefreshLibrary)
  232. {
  233. _libraryMonitor.Start();
  234. }
  235. }
  236. if (request.RefreshLibrary)
  237. {
  238. _libraryManager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None);
  239. }
  240. }
  241. /// <summary>
  242. /// Posts the specified request.
  243. /// </summary>
  244. /// <param name="request">The request.</param>
  245. public void Post(RenameVirtualFolder request)
  246. {
  247. if (string.IsNullOrWhiteSpace(request.Name))
  248. {
  249. throw new ArgumentNullException("request");
  250. }
  251. if (string.IsNullOrWhiteSpace(request.NewName))
  252. {
  253. throw new ArgumentNullException("request");
  254. }
  255. var rootFolderPath = _appPaths.DefaultUserViewsPath;
  256. var currentPath = Path.Combine(rootFolderPath, request.Name);
  257. var newPath = Path.Combine(rootFolderPath, request.NewName);
  258. if (!Directory.Exists(currentPath))
  259. {
  260. throw new DirectoryNotFoundException("The media collection does not exist");
  261. }
  262. if (!string.Equals(currentPath, newPath, StringComparison.OrdinalIgnoreCase) && Directory.Exists(newPath))
  263. {
  264. throw new ArgumentException("There is already a media collection with the name " + newPath + ".");
  265. }
  266. _libraryMonitor.Stop();
  267. try
  268. {
  269. // Only make a two-phase move when changing capitalization
  270. if (string.Equals(currentPath, newPath, StringComparison.OrdinalIgnoreCase))
  271. {
  272. //Create an unique name
  273. var temporaryName = Guid.NewGuid().ToString();
  274. var temporaryPath = Path.Combine(rootFolderPath, temporaryName);
  275. Directory.Move(currentPath, temporaryPath);
  276. currentPath = temporaryPath;
  277. }
  278. Directory.Move(currentPath, newPath);
  279. // Need to add a delay here or directory watchers may still pick up the changes
  280. var task = Task.Delay(1000);
  281. // Have to block here to allow exceptions to bubble
  282. Task.WaitAll(task);
  283. }
  284. finally
  285. {
  286. // No need to start if scanning the library because it will handle it
  287. if (!request.RefreshLibrary)
  288. {
  289. _libraryMonitor.Start();
  290. }
  291. }
  292. if (request.RefreshLibrary)
  293. {
  294. _libraryManager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None);
  295. }
  296. }
  297. /// <summary>
  298. /// Deletes the specified request.
  299. /// </summary>
  300. /// <param name="request">The request.</param>
  301. public void Delete(RemoveVirtualFolder request)
  302. {
  303. if (string.IsNullOrWhiteSpace(request.Name))
  304. {
  305. throw new ArgumentNullException("request");
  306. }
  307. var rootFolderPath = _appPaths.DefaultUserViewsPath;
  308. var path = Path.Combine(rootFolderPath, request.Name);
  309. if (!Directory.Exists(path))
  310. {
  311. throw new DirectoryNotFoundException("The media folder does not exist");
  312. }
  313. _libraryMonitor.Stop();
  314. try
  315. {
  316. Directory.Delete(path, true);
  317. // Need to add a delay here or directory watchers may still pick up the changes
  318. var delayTask = Task.Delay(1000);
  319. // Have to block here to allow exceptions to bubble
  320. Task.WaitAll(delayTask);
  321. }
  322. finally
  323. {
  324. // No need to start if scanning the library because it will handle it
  325. if (!request.RefreshLibrary)
  326. {
  327. _libraryMonitor.Start();
  328. }
  329. }
  330. if (request.RefreshLibrary)
  331. {
  332. _libraryManager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None);
  333. }
  334. }
  335. /// <summary>
  336. /// Posts the specified request.
  337. /// </summary>
  338. /// <param name="request">The request.</param>
  339. public void Post(AddMediaPath request)
  340. {
  341. if (string.IsNullOrWhiteSpace(request.Name))
  342. {
  343. throw new ArgumentNullException("request");
  344. }
  345. _libraryMonitor.Stop();
  346. try
  347. {
  348. LibraryHelpers.AddMediaPath(_fileSystem, request.Name, request.Path, _appPaths);
  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. // No need to start if scanning the library because it will handle it
  357. if (!request.RefreshLibrary)
  358. {
  359. _libraryMonitor.Start();
  360. }
  361. }
  362. if (request.RefreshLibrary)
  363. {
  364. _libraryManager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None);
  365. }
  366. }
  367. /// <summary>
  368. /// Deletes the specified request.
  369. /// </summary>
  370. /// <param name="request">The request.</param>
  371. public void Delete(RemoveMediaPath request)
  372. {
  373. if (string.IsNullOrWhiteSpace(request.Name))
  374. {
  375. throw new ArgumentNullException("request");
  376. }
  377. _libraryMonitor.Stop();
  378. try
  379. {
  380. LibraryHelpers.RemoveMediaPath(_fileSystem, request.Name, request.Path, _appPaths);
  381. // Need to add a delay here or directory watchers may still pick up the changes
  382. var task = Task.Delay(1000);
  383. // Have to block here to allow exceptions to bubble
  384. Task.WaitAll(task);
  385. }
  386. finally
  387. {
  388. // No need to start if scanning the library because it will handle it
  389. if (!request.RefreshLibrary)
  390. {
  391. _libraryMonitor.Start();
  392. }
  393. }
  394. if (request.RefreshLibrary)
  395. {
  396. _libraryManager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None);
  397. }
  398. }
  399. }
  400. }