LibraryStructureService.cs 12 KB

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