LibraryStructureService.cs 19 KB

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