LibraryStructureService.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Controller;
  3. using MediaBrowser.Controller.Library;
  4. using MediaBrowser.Controller.Net;
  5. using MediaBrowser.Model.Entities;
  6. using MediaBrowser.Model.Logging;
  7. using ServiceStack;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. namespace MediaBrowser.Api.Library
  15. {
  16. /// <summary>
  17. /// Class GetDefaultVirtualFolders
  18. /// </summary>
  19. [Route("/Library/VirtualFolders", "GET")]
  20. [Route("/Users/{UserId}/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. }
  48. [Route("/Library/VirtualFolders", "DELETE")]
  49. public class RemoveVirtualFolder : IReturnVoid
  50. {
  51. /// <summary>
  52. /// Gets or sets the name.
  53. /// </summary>
  54. /// <value>The name.</value>
  55. public string Name { get; set; }
  56. /// <summary>
  57. /// Gets or sets a value indicating whether [refresh library].
  58. /// </summary>
  59. /// <value><c>true</c> if [refresh library]; otherwise, <c>false</c>.</value>
  60. public bool RefreshLibrary { get; set; }
  61. }
  62. [Route("/Library/VirtualFolders/Name", "POST")]
  63. public class RenameVirtualFolder : IReturnVoid
  64. {
  65. /// <summary>
  66. /// Gets or sets the name.
  67. /// </summary>
  68. /// <value>The name.</value>
  69. public string Name { get; set; }
  70. /// <summary>
  71. /// Gets or sets the name.
  72. /// </summary>
  73. /// <value>The name.</value>
  74. public string NewName { get; set; }
  75. /// <summary>
  76. /// Gets or sets a value indicating whether [refresh library].
  77. /// </summary>
  78. /// <value><c>true</c> if [refresh library]; otherwise, <c>false</c>.</value>
  79. public bool RefreshLibrary { get; set; }
  80. }
  81. [Route("/Library/VirtualFolders/Paths", "POST")]
  82. public class AddMediaPath : IReturnVoid
  83. {
  84. /// <summary>
  85. /// Gets or sets the name.
  86. /// </summary>
  87. /// <value>The name.</value>
  88. public string Name { get; set; }
  89. /// <summary>
  90. /// Gets or sets the name.
  91. /// </summary>
  92. /// <value>The name.</value>
  93. public string Path { get; set; }
  94. /// <summary>
  95. /// Gets or sets a value indicating whether [refresh library].
  96. /// </summary>
  97. /// <value><c>true</c> if [refresh library]; otherwise, <c>false</c>.</value>
  98. public bool RefreshLibrary { get; set; }
  99. }
  100. [Route("/Library/VirtualFolders/Paths", "DELETE")]
  101. public class RemoveMediaPath : IReturnVoid
  102. {
  103. /// <summary>
  104. /// Gets or sets the name.
  105. /// </summary>
  106. /// <value>The name.</value>
  107. public string Name { get; set; }
  108. /// <summary>
  109. /// Gets or sets the name.
  110. /// </summary>
  111. /// <value>The name.</value>
  112. public string Path { get; set; }
  113. /// <summary>
  114. /// Gets or sets a value indicating whether [refresh library].
  115. /// </summary>
  116. /// <value><c>true</c> if [refresh library]; otherwise, <c>false</c>.</value>
  117. public bool RefreshLibrary { get; set; }
  118. }
  119. /// <summary>
  120. /// Class LibraryStructureService
  121. /// </summary>
  122. [Authenticated]
  123. public class LibraryStructureService : BaseApiService
  124. {
  125. /// <summary>
  126. /// The _app paths
  127. /// </summary>
  128. private readonly IServerApplicationPaths _appPaths;
  129. /// <summary>
  130. /// The _user manager
  131. /// </summary>
  132. private readonly IUserManager _userManager;
  133. /// <summary>
  134. /// The _library manager
  135. /// </summary>
  136. private readonly ILibraryManager _libraryManager;
  137. private readonly ILibraryMonitor _libraryMonitor;
  138. private readonly IFileSystem _fileSystem;
  139. private readonly ILogger _logger;
  140. /// <summary>
  141. /// Initializes a new instance of the <see cref="LibraryStructureService"/> class.
  142. /// </summary>
  143. /// <param name="appPaths">The app paths.</param>
  144. /// <param name="userManager">The user manager.</param>
  145. /// <param name="libraryManager">The library manager.</param>
  146. /// <exception cref="System.ArgumentNullException">appPaths</exception>
  147. public LibraryStructureService(IServerApplicationPaths appPaths, IUserManager userManager, ILibraryManager libraryManager, ILibraryMonitor libraryMonitor, IFileSystem fileSystem, ILogger logger)
  148. {
  149. if (appPaths == null)
  150. {
  151. throw new ArgumentNullException("appPaths");
  152. }
  153. _userManager = userManager;
  154. _appPaths = appPaths;
  155. _libraryManager = libraryManager;
  156. _libraryMonitor = libraryMonitor;
  157. _fileSystem = fileSystem;
  158. _logger = logger;
  159. }
  160. /// <summary>
  161. /// Gets the specified request.
  162. /// </summary>
  163. /// <param name="request">The request.</param>
  164. /// <returns>System.Object.</returns>
  165. public object Get(GetVirtualFolders request)
  166. {
  167. if (string.IsNullOrEmpty(request.UserId))
  168. {
  169. var result = _libraryManager.GetDefaultVirtualFolders().OrderBy(i => i.Name).ToList();
  170. return ToOptimizedSerializedResultUsingCache(result);
  171. }
  172. else
  173. {
  174. var user = _userManager.GetUserById(new Guid(request.UserId));
  175. var result = _libraryManager.GetVirtualFolders(user).OrderBy(i => i.Name).ToList();
  176. return ToOptimizedSerializedResultUsingCache(result);
  177. }
  178. }
  179. /// <summary>
  180. /// Posts the specified request.
  181. /// </summary>
  182. /// <param name="request">The request.</param>
  183. public void Post(AddVirtualFolder request)
  184. {
  185. if (string.IsNullOrWhiteSpace(request.Name))
  186. {
  187. throw new ArgumentNullException("request");
  188. }
  189. var name = _fileSystem.GetValidFilename(request.Name);
  190. var rootFolderPath = _appPaths.DefaultUserViewsPath;
  191. var virtualFolderPath = Path.Combine(rootFolderPath, name);
  192. if (Directory.Exists(virtualFolderPath))
  193. {
  194. throw new ArgumentException("There is already a media collection with the name " + name + ".");
  195. }
  196. _libraryMonitor.Stop();
  197. try
  198. {
  199. Directory.CreateDirectory(virtualFolderPath);
  200. if (!string.IsNullOrEmpty(request.CollectionType))
  201. {
  202. var path = Path.Combine(virtualFolderPath, request.CollectionType + ".collection");
  203. File.Create(path);
  204. }
  205. // Need to add a delay here or directory watchers may still pick up the changes
  206. var task = Task.Delay(1000);
  207. // Have to block here to allow exceptions to bubble
  208. Task.WaitAll(task);
  209. }
  210. finally
  211. {
  212. // No need to start if scanning the library because it will handle it
  213. if (!request.RefreshLibrary)
  214. {
  215. _libraryMonitor.Start();
  216. }
  217. }
  218. if (request.RefreshLibrary)
  219. {
  220. _libraryManager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None);
  221. }
  222. }
  223. /// <summary>
  224. /// Posts the specified request.
  225. /// </summary>
  226. /// <param name="request">The request.</param>
  227. public void Post(RenameVirtualFolder request)
  228. {
  229. if (string.IsNullOrWhiteSpace(request.Name))
  230. {
  231. throw new ArgumentNullException("request");
  232. }
  233. if (string.IsNullOrWhiteSpace(request.NewName))
  234. {
  235. throw new ArgumentNullException("request");
  236. }
  237. var rootFolderPath = _appPaths.DefaultUserViewsPath;
  238. var currentPath = Path.Combine(rootFolderPath, request.Name);
  239. var newPath = Path.Combine(rootFolderPath, request.NewName);
  240. if (!Directory.Exists(currentPath))
  241. {
  242. throw new DirectoryNotFoundException("The media collection does not exist");
  243. }
  244. if (!string.Equals(currentPath, newPath, StringComparison.OrdinalIgnoreCase) && Directory.Exists(newPath))
  245. {
  246. throw new ArgumentException("There is already a media collection with the name " + newPath + ".");
  247. }
  248. _libraryMonitor.Stop();
  249. try
  250. {
  251. // Only make a two-phase move when changing capitalization
  252. if (string.Equals(currentPath, newPath, StringComparison.OrdinalIgnoreCase))
  253. {
  254. //Create an unique name
  255. var temporaryName = Guid.NewGuid().ToString();
  256. var temporaryPath = Path.Combine(rootFolderPath, temporaryName);
  257. Directory.Move(currentPath, temporaryPath);
  258. currentPath = temporaryPath;
  259. }
  260. Directory.Move(currentPath, newPath);
  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. }
  266. finally
  267. {
  268. // No need to start if scanning the library because it will handle it
  269. if (!request.RefreshLibrary)
  270. {
  271. _libraryMonitor.Start();
  272. }
  273. }
  274. if (request.RefreshLibrary)
  275. {
  276. _libraryManager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None);
  277. }
  278. }
  279. /// <summary>
  280. /// Deletes the specified request.
  281. /// </summary>
  282. /// <param name="request">The request.</param>
  283. public void Delete(RemoveVirtualFolder request)
  284. {
  285. if (string.IsNullOrWhiteSpace(request.Name))
  286. {
  287. throw new ArgumentNullException("request");
  288. }
  289. var rootFolderPath = _appPaths.DefaultUserViewsPath;
  290. var path = Path.Combine(rootFolderPath, request.Name);
  291. if (!Directory.Exists(path))
  292. {
  293. throw new DirectoryNotFoundException("The media folder does not exist");
  294. }
  295. _libraryMonitor.Stop();
  296. try
  297. {
  298. Directory.Delete(path, true);
  299. // Need to add a delay here or directory watchers may still pick up the changes
  300. var delayTask = Task.Delay(1000);
  301. // Have to block here to allow exceptions to bubble
  302. Task.WaitAll(delayTask);
  303. }
  304. finally
  305. {
  306. // No need to start if scanning the library because it will handle it
  307. if (!request.RefreshLibrary)
  308. {
  309. _libraryMonitor.Start();
  310. }
  311. }
  312. if (request.RefreshLibrary)
  313. {
  314. _libraryManager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None);
  315. }
  316. }
  317. /// <summary>
  318. /// Posts the specified request.
  319. /// </summary>
  320. /// <param name="request">The request.</param>
  321. public void Post(AddMediaPath request)
  322. {
  323. if (string.IsNullOrWhiteSpace(request.Name))
  324. {
  325. throw new ArgumentNullException("request");
  326. }
  327. _libraryMonitor.Stop();
  328. try
  329. {
  330. LibraryHelpers.AddMediaPath(_fileSystem, request.Name, request.Path, _appPaths);
  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. }
  336. finally
  337. {
  338. // No need to start if scanning the library because it will handle it
  339. if (!request.RefreshLibrary)
  340. {
  341. _libraryMonitor.Start();
  342. }
  343. }
  344. if (request.RefreshLibrary)
  345. {
  346. _libraryManager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None);
  347. }
  348. }
  349. /// <summary>
  350. /// Deletes the specified request.
  351. /// </summary>
  352. /// <param name="request">The request.</param>
  353. public void Delete(RemoveMediaPath request)
  354. {
  355. if (string.IsNullOrWhiteSpace(request.Name))
  356. {
  357. throw new ArgumentNullException("request");
  358. }
  359. _libraryMonitor.Stop();
  360. try
  361. {
  362. LibraryHelpers.RemoveMediaPath(_fileSystem, request.Name, request.Path, _appPaths);
  363. // Need to add a delay here or directory watchers may still pick up the changes
  364. var task = Task.Delay(1000);
  365. // Have to block here to allow exceptions to bubble
  366. Task.WaitAll(task);
  367. }
  368. finally
  369. {
  370. // No need to start if scanning the library because it will handle it
  371. if (!request.RefreshLibrary)
  372. {
  373. _libraryMonitor.Start();
  374. }
  375. }
  376. if (request.RefreshLibrary)
  377. {
  378. _libraryManager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None);
  379. }
  380. }
  381. }
  382. }