LibraryStructureService.cs 14 KB

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