LibraryStructureService.cs 12 KB

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