LibraryStructureService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. /// <summary>
  102. /// Gets or sets a value indicating whether [refresh library].
  103. /// </summary>
  104. /// <value><c>true</c> if [refresh library]; otherwise, <c>false</c>.</value>
  105. public bool RefreshLibrary { get; set; }
  106. }
  107. [Route("/Library/VirtualFolders/Paths", "DELETE")]
  108. public class RemoveMediaPath : IReturnVoid
  109. {
  110. /// <summary>
  111. /// Gets or sets the name.
  112. /// </summary>
  113. /// <value>The name.</value>
  114. public string Name { get; set; }
  115. /// <summary>
  116. /// Gets or sets the name.
  117. /// </summary>
  118. /// <value>The name.</value>
  119. public string Path { get; set; }
  120. /// <summary>
  121. /// Gets or sets a value indicating whether [refresh library].
  122. /// </summary>
  123. /// <value><c>true</c> if [refresh library]; otherwise, <c>false</c>.</value>
  124. public bool RefreshLibrary { get; set; }
  125. }
  126. [Route("/Library/VirtualFolders/LibraryOptions", "POST")]
  127. public class UpdateLibraryOptions : IReturnVoid
  128. {
  129. public string Id { get; set; }
  130. public LibraryOptions LibraryOptions { get; set; }
  131. }
  132. /// <summary>
  133. /// Class LibraryStructureService
  134. /// </summary>
  135. [Authenticated(Roles = "Admin", AllowBeforeStartupWizard = true)]
  136. public class LibraryStructureService : BaseApiService
  137. {
  138. /// <summary>
  139. /// The _app paths
  140. /// </summary>
  141. private readonly IServerApplicationPaths _appPaths;
  142. /// <summary>
  143. /// The _library manager
  144. /// </summary>
  145. private readonly ILibraryManager _libraryManager;
  146. private readonly ILibraryMonitor _libraryMonitor;
  147. private readonly IFileSystem _fileSystem;
  148. /// <summary>
  149. /// Initializes a new instance of the <see cref="LibraryStructureService" /> class.
  150. /// </summary>
  151. public LibraryStructureService(IServerApplicationPaths appPaths, ILibraryManager libraryManager, ILibraryMonitor libraryMonitor, IFileSystem fileSystem)
  152. {
  153. if (appPaths == null)
  154. {
  155. throw new ArgumentNullException("appPaths");
  156. }
  157. _appPaths = appPaths;
  158. _libraryManager = libraryManager;
  159. _libraryMonitor = libraryMonitor;
  160. _fileSystem = fileSystem;
  161. }
  162. /// <summary>
  163. /// Gets the specified request.
  164. /// </summary>
  165. /// <param name="request">The request.</param>
  166. /// <returns>System.Object.</returns>
  167. public object Get(GetVirtualFolders request)
  168. {
  169. var result = _libraryManager.GetVirtualFolders().OrderBy(i => i.Name).ToList();
  170. return ToOptimizedSerializedResultUsingCache(result);
  171. }
  172. public void Post(UpdateLibraryOptions request)
  173. {
  174. var collectionFolder = (CollectionFolder)_libraryManager.GetItemById(request.Id);
  175. collectionFolder.UpdateLibraryOptions(request.LibraryOptions);
  176. }
  177. /// <summary>
  178. /// Posts the specified request.
  179. /// </summary>
  180. /// <param name="request">The request.</param>
  181. public void Post(AddVirtualFolder request)
  182. {
  183. var libraryOptions = request.LibraryOptions ?? new LibraryOptions();
  184. _libraryManager.AddVirtualFolder(request.Name, request.CollectionType, request.Paths, libraryOptions, request.RefreshLibrary);
  185. }
  186. /// <summary>
  187. /// Posts the specified request.
  188. /// </summary>
  189. /// <param name="request">The request.</param>
  190. public void Post(RenameVirtualFolder request)
  191. {
  192. if (string.IsNullOrWhiteSpace(request.Name))
  193. {
  194. throw new ArgumentNullException("request");
  195. }
  196. if (string.IsNullOrWhiteSpace(request.NewName))
  197. {
  198. throw new ArgumentNullException("request");
  199. }
  200. var rootFolderPath = _appPaths.DefaultUserViewsPath;
  201. var currentPath = Path.Combine(rootFolderPath, request.Name);
  202. var newPath = Path.Combine(rootFolderPath, request.NewName);
  203. if (!_fileSystem.DirectoryExists(currentPath))
  204. {
  205. throw new DirectoryNotFoundException("The media collection does not exist");
  206. }
  207. if (!string.Equals(currentPath, newPath, StringComparison.OrdinalIgnoreCase) && _fileSystem.DirectoryExists(newPath))
  208. {
  209. throw new ArgumentException("There is already a media collection with the name " + newPath + ".");
  210. }
  211. _libraryMonitor.Stop();
  212. try
  213. {
  214. // Only make a two-phase move when changing capitalization
  215. if (string.Equals(currentPath, newPath, StringComparison.OrdinalIgnoreCase))
  216. {
  217. //Create an unique name
  218. var temporaryName = Guid.NewGuid().ToString();
  219. var temporaryPath = Path.Combine(rootFolderPath, temporaryName);
  220. _fileSystem.MoveDirectory(currentPath, temporaryPath);
  221. currentPath = temporaryPath;
  222. }
  223. _fileSystem.MoveDirectory(currentPath, newPath);
  224. }
  225. finally
  226. {
  227. Task.Run(() =>
  228. {
  229. // No need to start if scanning the library because it will handle it
  230. if (request.RefreshLibrary)
  231. {
  232. _libraryManager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None);
  233. }
  234. else
  235. {
  236. // Need to add a delay here or directory watchers may still pick up the changes
  237. var task = Task.Delay(1000);
  238. // Have to block here to allow exceptions to bubble
  239. Task.WaitAll(task);
  240. _libraryMonitor.Start();
  241. }
  242. });
  243. }
  244. }
  245. /// <summary>
  246. /// Deletes the specified request.
  247. /// </summary>
  248. /// <param name="request">The request.</param>
  249. public void Delete(RemoveVirtualFolder request)
  250. {
  251. _libraryManager.RemoveVirtualFolder(request.Name, request.RefreshLibrary);
  252. }
  253. /// <summary>
  254. /// Posts the specified request.
  255. /// </summary>
  256. /// <param name="request">The request.</param>
  257. public void Post(AddMediaPath request)
  258. {
  259. if (string.IsNullOrWhiteSpace(request.Name))
  260. {
  261. throw new ArgumentNullException("request");
  262. }
  263. _libraryMonitor.Stop();
  264. try
  265. {
  266. _libraryManager.AddMediaPath(request.Name, request.Path);
  267. }
  268. finally
  269. {
  270. Task.Run(() =>
  271. {
  272. // No need to start if scanning the library because it will handle it
  273. if (request.RefreshLibrary)
  274. {
  275. _libraryManager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None);
  276. }
  277. else
  278. {
  279. // Need to add a delay here or directory watchers may still pick up the changes
  280. var task = Task.Delay(1000);
  281. // Have to block here to allow exceptions to bubble
  282. Task.WaitAll(task);
  283. _libraryMonitor.Start();
  284. }
  285. });
  286. }
  287. }
  288. /// <summary>
  289. /// Deletes the specified request.
  290. /// </summary>
  291. /// <param name="request">The request.</param>
  292. public void Delete(RemoveMediaPath request)
  293. {
  294. if (string.IsNullOrWhiteSpace(request.Name))
  295. {
  296. throw new ArgumentNullException("request");
  297. }
  298. _libraryMonitor.Stop();
  299. try
  300. {
  301. _libraryManager.RemoveMediaPath(request.Name, request.Path);
  302. }
  303. finally
  304. {
  305. Task.Run(() =>
  306. {
  307. // No need to start if scanning the library because it will handle it
  308. if (request.RefreshLibrary)
  309. {
  310. _libraryManager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None);
  311. }
  312. else
  313. {
  314. // Need to add a delay here or directory watchers may still pick up the changes
  315. var task = Task.Delay(1000);
  316. // Have to block here to allow exceptions to bubble
  317. Task.WaitAll(task);
  318. _libraryMonitor.Start();
  319. }
  320. });
  321. }
  322. }
  323. }
  324. }