LibraryStructureService.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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/Path", "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 = false, DataType = "string", ParameterType = "query", Verb = "POST")]
  157. public string Path { get; set; }
  158. }
  159. /// <summary>
  160. /// Class LibraryStructureService
  161. /// </summary>
  162. public class LibraryStructureService : BaseApiService
  163. {
  164. /// <summary>
  165. /// The _app paths
  166. /// </summary>
  167. private readonly IServerApplicationPaths _appPaths;
  168. /// <summary>
  169. /// The _user manager
  170. /// </summary>
  171. private readonly IUserManager _userManager;
  172. /// <summary>
  173. /// The _library manager
  174. /// </summary>
  175. private readonly ILibraryManager _libraryManager;
  176. private readonly ILibraryMonitor _libraryMonitor;
  177. private readonly IFileSystem _fileSystem;
  178. private readonly ILogger _logger;
  179. /// <summary>
  180. /// Initializes a new instance of the <see cref="LibraryStructureService"/> class.
  181. /// </summary>
  182. /// <param name="appPaths">The app paths.</param>
  183. /// <param name="userManager">The user manager.</param>
  184. /// <param name="libraryManager">The library manager.</param>
  185. /// <exception cref="System.ArgumentNullException">appPaths</exception>
  186. public LibraryStructureService(IServerApplicationPaths appPaths, IUserManager userManager, ILibraryManager libraryManager, ILibraryMonitor libraryMonitor, IFileSystem fileSystem, ILogger logger)
  187. {
  188. if (appPaths == null)
  189. {
  190. throw new ArgumentNullException("appPaths");
  191. }
  192. _userManager = userManager;
  193. _appPaths = appPaths;
  194. _libraryManager = libraryManager;
  195. _libraryMonitor = libraryMonitor;
  196. _fileSystem = fileSystem;
  197. _logger = logger;
  198. }
  199. /// <summary>
  200. /// Posts the specified request.
  201. /// </summary>
  202. /// <param name="request">The request.</param>
  203. /// <exception cref="System.ArgumentException">Please supply a Path</exception>
  204. public void Post(ReportChangedPath request)
  205. {
  206. if (string.IsNullOrEmpty(request.Path))
  207. {
  208. throw new ArgumentException("Please supply a Path");
  209. }
  210. _libraryMonitor.ReportFileSystemChanged(request.Path);
  211. }
  212. /// <summary>
  213. /// Gets the specified request.
  214. /// </summary>
  215. /// <param name="request">The request.</param>
  216. /// <returns>System.Object.</returns>
  217. public object Get(GetVirtualFolders request)
  218. {
  219. if (string.IsNullOrEmpty(request.UserId))
  220. {
  221. var result = _libraryManager.GetDefaultVirtualFolders().OrderBy(i => i.Name).ToList();
  222. return ToOptimizedResult(result);
  223. }
  224. else
  225. {
  226. var user = _userManager.GetUserById(new Guid(request.UserId));
  227. var result = _libraryManager.GetVirtualFolders(user).OrderBy(i => i.Name).ToList();
  228. return ToOptimizedResult(result);
  229. }
  230. }
  231. /// <summary>
  232. /// Posts the specified request.
  233. /// </summary>
  234. /// <param name="request">The request.</param>
  235. public void Post(AddVirtualFolder request)
  236. {
  237. if (string.IsNullOrWhiteSpace(request.Name))
  238. {
  239. throw new ArgumentNullException("request");
  240. }
  241. var name = _fileSystem.GetValidFilename(request.Name);
  242. string rootFolderPath;
  243. if (string.IsNullOrEmpty(request.UserId))
  244. {
  245. rootFolderPath = _appPaths.DefaultUserViewsPath;
  246. }
  247. else
  248. {
  249. var user = _userManager.GetUserById(new Guid(request.UserId));
  250. rootFolderPath = user.RootFolderPath;
  251. }
  252. var virtualFolderPath = Path.Combine(rootFolderPath, name);
  253. if (Directory.Exists(virtualFolderPath))
  254. {
  255. throw new ArgumentException("There is already a media collection with the name " + name + ".");
  256. }
  257. _libraryMonitor.Stop();
  258. try
  259. {
  260. Directory.CreateDirectory(virtualFolderPath);
  261. if (!string.IsNullOrEmpty(request.CollectionType))
  262. {
  263. var path = Path.Combine(virtualFolderPath, request.CollectionType + ".collection");
  264. File.Create(path);
  265. }
  266. // Need to add a delay here or directory watchers may still pick up the changes
  267. var task = Task.Delay(1000);
  268. // Have to block here to allow exceptions to bubble
  269. Task.WaitAll(task);
  270. }
  271. finally
  272. {
  273. // No need to start if scanning the library because it will handle it
  274. if (!request.RefreshLibrary)
  275. {
  276. _libraryMonitor.Start();
  277. }
  278. }
  279. if (request.RefreshLibrary)
  280. {
  281. _libraryManager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None);
  282. }
  283. }
  284. /// <summary>
  285. /// Posts the specified request.
  286. /// </summary>
  287. /// <param name="request">The request.</param>
  288. public void Post(RenameVirtualFolder request)
  289. {
  290. if (string.IsNullOrWhiteSpace(request.Name))
  291. {
  292. throw new ArgumentNullException("request");
  293. }
  294. if (string.IsNullOrWhiteSpace(request.NewName))
  295. {
  296. throw new ArgumentNullException("request");
  297. }
  298. string rootFolderPath;
  299. if (string.IsNullOrEmpty(request.UserId))
  300. {
  301. rootFolderPath = _appPaths.DefaultUserViewsPath;
  302. }
  303. else
  304. {
  305. var user = _userManager.GetUserById(new Guid(request.UserId));
  306. rootFolderPath = user.RootFolderPath;
  307. }
  308. var currentPath = Path.Combine(rootFolderPath, request.Name);
  309. var newPath = Path.Combine(rootFolderPath, request.NewName);
  310. if (!Directory.Exists(currentPath))
  311. {
  312. throw new DirectoryNotFoundException("The media collection does not exist");
  313. }
  314. if (!string.Equals(currentPath, newPath, StringComparison.OrdinalIgnoreCase) && Directory.Exists(newPath))
  315. {
  316. throw new ArgumentException("There is already a media collection with the name " + newPath + ".");
  317. }
  318. _libraryMonitor.Stop();
  319. try
  320. {
  321. // Only make a two-phase move when changing capitalization
  322. if (string.Equals(currentPath, newPath, StringComparison.OrdinalIgnoreCase))
  323. {
  324. //Create an unique name
  325. var temporaryName = Guid.NewGuid().ToString();
  326. var temporaryPath = Path.Combine(rootFolderPath, temporaryName);
  327. Directory.Move(currentPath, temporaryPath);
  328. currentPath = temporaryPath;
  329. }
  330. Directory.Move(currentPath, newPath);
  331. // Need to add a delay here or directory watchers may still pick up the changes
  332. var task = Task.Delay(1000);
  333. // Have to block here to allow exceptions to bubble
  334. Task.WaitAll(task);
  335. }
  336. finally
  337. {
  338. // No need to start if scanning the library because it will handle it
  339. if (!request.RefreshLibrary)
  340. {
  341. _libraryMonitor.Start();
  342. }
  343. }
  344. if (request.RefreshLibrary)
  345. {
  346. _libraryManager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None);
  347. }
  348. }
  349. /// <summary>
  350. /// Deletes the specified request.
  351. /// </summary>
  352. /// <param name="request">The request.</param>
  353. public void Delete(RemoveVirtualFolder request)
  354. {
  355. if (string.IsNullOrWhiteSpace(request.Name))
  356. {
  357. throw new ArgumentNullException("request");
  358. }
  359. string rootFolderPath;
  360. if (string.IsNullOrEmpty(request.UserId))
  361. {
  362. rootFolderPath = _appPaths.DefaultUserViewsPath;
  363. }
  364. else
  365. {
  366. var user = _userManager.GetUserById(new Guid(request.UserId));
  367. rootFolderPath = user.RootFolderPath;
  368. }
  369. var path = Path.Combine(rootFolderPath, request.Name);
  370. if (!Directory.Exists(path))
  371. {
  372. throw new DirectoryNotFoundException("The media folder does not exist");
  373. }
  374. _libraryMonitor.Stop();
  375. try
  376. {
  377. Directory.Delete(path, true);
  378. // Need to add a delay here or directory watchers may still pick up the changes
  379. var delayTask = Task.Delay(1000);
  380. // Have to block here to allow exceptions to bubble
  381. Task.WaitAll(delayTask);
  382. }
  383. finally
  384. {
  385. // No need to start if scanning the library because it will handle it
  386. if (!request.RefreshLibrary)
  387. {
  388. _libraryMonitor.Start();
  389. }
  390. }
  391. if (request.RefreshLibrary)
  392. {
  393. _libraryManager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None);
  394. }
  395. }
  396. /// <summary>
  397. /// Posts the specified request.
  398. /// </summary>
  399. /// <param name="request">The request.</param>
  400. public void Post(AddMediaPath request)
  401. {
  402. if (string.IsNullOrWhiteSpace(request.Name))
  403. {
  404. throw new ArgumentNullException("request");
  405. }
  406. _libraryMonitor.Stop();
  407. try
  408. {
  409. if (string.IsNullOrEmpty(request.UserId))
  410. {
  411. LibraryHelpers.AddMediaPath(_fileSystem, request.Name, request.Path, null, _appPaths);
  412. }
  413. else
  414. {
  415. var user = _userManager.GetUserById(new Guid(request.UserId));
  416. LibraryHelpers.AddMediaPath(_fileSystem, request.Name, request.Path, user, _appPaths);
  417. }
  418. // Need to add a delay here or directory watchers may still pick up the changes
  419. var task = Task.Delay(1000);
  420. // Have to block here to allow exceptions to bubble
  421. Task.WaitAll(task);
  422. }
  423. finally
  424. {
  425. // No need to start if scanning the library because it will handle it
  426. if (!request.RefreshLibrary)
  427. {
  428. _libraryMonitor.Start();
  429. }
  430. }
  431. if (request.RefreshLibrary)
  432. {
  433. _libraryManager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None);
  434. }
  435. }
  436. /// <summary>
  437. /// Deletes the specified request.
  438. /// </summary>
  439. /// <param name="request">The request.</param>
  440. public void Delete(RemoveMediaPath request)
  441. {
  442. if (string.IsNullOrWhiteSpace(request.Name))
  443. {
  444. throw new ArgumentNullException("request");
  445. }
  446. _libraryMonitor.Stop();
  447. try
  448. {
  449. if (string.IsNullOrEmpty(request.UserId))
  450. {
  451. LibraryHelpers.RemoveMediaPath(_fileSystem, request.Name, request.Path, null, _appPaths);
  452. }
  453. else
  454. {
  455. var user = _userManager.GetUserById(new Guid(request.UserId));
  456. LibraryHelpers.RemoveMediaPath(_fileSystem, request.Name, request.Path, user, _appPaths);
  457. }
  458. // Need to add a delay here or directory watchers may still pick up the changes
  459. var task = Task.Delay(1000);
  460. // Have to block here to allow exceptions to bubble
  461. Task.WaitAll(task);
  462. }
  463. finally
  464. {
  465. // No need to start if scanning the library because it will handle it
  466. if (!request.RefreshLibrary)
  467. {
  468. _libraryMonitor.Start();
  469. }
  470. }
  471. if (request.RefreshLibrary)
  472. {
  473. _libraryManager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None);
  474. }
  475. }
  476. }
  477. }