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. if (_fileSystem.DirectoryExists(virtualFolderPath))
  177. {
  178. throw new ArgumentException("There is already a media library with the name " + 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, request.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. }