LibraryStructureService.cs 15 KB

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