LibraryStructureService.cs 14 KB

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