LibraryStructureService.cs 15 KB

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