LibraryStructureService.cs 18 KB

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