LibraryStructureService.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. _libraryManager.AddVirtualFolder(request.Name, request.CollectionType, request.Paths, request.RefreshLibrary);
  169. }
  170. /// <summary>
  171. /// Posts the specified request.
  172. /// </summary>
  173. /// <param name="request">The request.</param>
  174. public void Post(RenameVirtualFolder request)
  175. {
  176. if (string.IsNullOrWhiteSpace(request.Name))
  177. {
  178. throw new ArgumentNullException("request");
  179. }
  180. if (string.IsNullOrWhiteSpace(request.NewName))
  181. {
  182. throw new ArgumentNullException("request");
  183. }
  184. var rootFolderPath = _appPaths.DefaultUserViewsPath;
  185. var currentPath = Path.Combine(rootFolderPath, request.Name);
  186. var newPath = Path.Combine(rootFolderPath, request.NewName);
  187. if (!_fileSystem.DirectoryExists(currentPath))
  188. {
  189. throw new DirectoryNotFoundException("The media collection does not exist");
  190. }
  191. if (!string.Equals(currentPath, newPath, StringComparison.OrdinalIgnoreCase) && _fileSystem.DirectoryExists(newPath))
  192. {
  193. throw new ArgumentException("There is already a media collection with the name " + newPath + ".");
  194. }
  195. _libraryMonitor.Stop();
  196. try
  197. {
  198. // Only make a two-phase move when changing capitalization
  199. if (string.Equals(currentPath, newPath, StringComparison.OrdinalIgnoreCase))
  200. {
  201. //Create an unique name
  202. var temporaryName = Guid.NewGuid().ToString();
  203. var temporaryPath = Path.Combine(rootFolderPath, temporaryName);
  204. _fileSystem.MoveDirectory(currentPath, temporaryPath);
  205. currentPath = temporaryPath;
  206. }
  207. _fileSystem.MoveDirectory(currentPath, newPath);
  208. }
  209. finally
  210. {
  211. Task.Run(() =>
  212. {
  213. // No need to start if scanning the library because it will handle it
  214. if (request.RefreshLibrary)
  215. {
  216. _libraryManager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None);
  217. }
  218. else
  219. {
  220. // Need to add a delay here or directory watchers may still pick up the changes
  221. var task = Task.Delay(1000);
  222. // Have to block here to allow exceptions to bubble
  223. Task.WaitAll(task);
  224. _libraryMonitor.Start();
  225. }
  226. });
  227. }
  228. }
  229. /// <summary>
  230. /// Deletes the specified request.
  231. /// </summary>
  232. /// <param name="request">The request.</param>
  233. public void Delete(RemoveVirtualFolder request)
  234. {
  235. if (string.IsNullOrWhiteSpace(request.Name))
  236. {
  237. throw new ArgumentNullException("request");
  238. }
  239. var rootFolderPath = _appPaths.DefaultUserViewsPath;
  240. var path = Path.Combine(rootFolderPath, request.Name);
  241. if (!_fileSystem.DirectoryExists(path))
  242. {
  243. throw new DirectoryNotFoundException("The media folder does not exist");
  244. }
  245. _libraryMonitor.Stop();
  246. try
  247. {
  248. _fileSystem.DeleteDirectory(path, true);
  249. }
  250. finally
  251. {
  252. Task.Run(() =>
  253. {
  254. // No need to start if scanning the library because it will handle it
  255. if (request.RefreshLibrary)
  256. {
  257. _libraryManager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None);
  258. }
  259. else
  260. {
  261. // Need to add a delay here or directory watchers may still pick up the changes
  262. var task = Task.Delay(1000);
  263. // Have to block here to allow exceptions to bubble
  264. Task.WaitAll(task);
  265. _libraryMonitor.Start();
  266. }
  267. });
  268. }
  269. }
  270. /// <summary>
  271. /// Posts the specified request.
  272. /// </summary>
  273. /// <param name="request">The request.</param>
  274. public void Post(AddMediaPath request)
  275. {
  276. if (string.IsNullOrWhiteSpace(request.Name))
  277. {
  278. throw new ArgumentNullException("request");
  279. }
  280. _libraryMonitor.Stop();
  281. try
  282. {
  283. _libraryManager.AddMediaPath(request.Name, request.Path);
  284. }
  285. finally
  286. {
  287. Task.Run(() =>
  288. {
  289. // No need to start if scanning the library because it will handle it
  290. if (request.RefreshLibrary)
  291. {
  292. _libraryManager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None);
  293. }
  294. else
  295. {
  296. // Need to add a delay here or directory watchers may still pick up the changes
  297. var task = Task.Delay(1000);
  298. // Have to block here to allow exceptions to bubble
  299. Task.WaitAll(task);
  300. _libraryMonitor.Start();
  301. }
  302. });
  303. }
  304. }
  305. /// <summary>
  306. /// Deletes the specified request.
  307. /// </summary>
  308. /// <param name="request">The request.</param>
  309. public void Delete(RemoveMediaPath request)
  310. {
  311. if (string.IsNullOrWhiteSpace(request.Name))
  312. {
  313. throw new ArgumentNullException("request");
  314. }
  315. _libraryMonitor.Stop();
  316. try
  317. {
  318. LibraryHelpers.RemoveMediaPath(_fileSystem, request.Name, request.Path, _appPaths);
  319. }
  320. finally
  321. {
  322. Task.Run(() =>
  323. {
  324. // No need to start if scanning the library because it will handle it
  325. if (request.RefreshLibrary)
  326. {
  327. _libraryManager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None);
  328. }
  329. else
  330. {
  331. // Need to add a delay here or directory watchers may still pick up the changes
  332. var task = Task.Delay(1000);
  333. // Have to block here to allow exceptions to bubble
  334. Task.WaitAll(task);
  335. _libraryMonitor.Start();
  336. }
  337. });
  338. }
  339. }
  340. }
  341. }