LibraryStructureService.cs 13 KB

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