2
0

LibraryStructureService.cs 19 KB

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