LibraryStructureService.cs 14 KB

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