LibraryStructureService.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. // Need to add a delay here or directory watchers may still pick up the changes
  184. var task = Task.Delay(1000);
  185. // Have to block here to allow exceptions to bubble
  186. Task.WaitAll(task);
  187. }
  188. finally
  189. {
  190. // No need to start if scanning the library because it will handle it
  191. if (!request.RefreshLibrary)
  192. {
  193. _libraryMonitor.Start();
  194. }
  195. }
  196. if (request.RefreshLibrary)
  197. {
  198. _libraryManager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None);
  199. }
  200. }
  201. /// <summary>
  202. /// Posts the specified request.
  203. /// </summary>
  204. /// <param name="request">The request.</param>
  205. public void Post(RenameVirtualFolder request)
  206. {
  207. if (string.IsNullOrWhiteSpace(request.Name))
  208. {
  209. throw new ArgumentNullException("request");
  210. }
  211. if (string.IsNullOrWhiteSpace(request.NewName))
  212. {
  213. throw new ArgumentNullException("request");
  214. }
  215. var rootFolderPath = _appPaths.DefaultUserViewsPath;
  216. var currentPath = Path.Combine(rootFolderPath, request.Name);
  217. var newPath = Path.Combine(rootFolderPath, request.NewName);
  218. if (!Directory.Exists(currentPath))
  219. {
  220. throw new DirectoryNotFoundException("The media collection does not exist");
  221. }
  222. if (!string.Equals(currentPath, newPath, StringComparison.OrdinalIgnoreCase) && Directory.Exists(newPath))
  223. {
  224. throw new ArgumentException("There is already a media collection with the name " + newPath + ".");
  225. }
  226. _libraryMonitor.Stop();
  227. try
  228. {
  229. // Only make a two-phase move when changing capitalization
  230. if (string.Equals(currentPath, newPath, StringComparison.OrdinalIgnoreCase))
  231. {
  232. //Create an unique name
  233. var temporaryName = Guid.NewGuid().ToString();
  234. var temporaryPath = Path.Combine(rootFolderPath, temporaryName);
  235. Directory.Move(currentPath, temporaryPath);
  236. currentPath = temporaryPath;
  237. }
  238. Directory.Move(currentPath, newPath);
  239. // Need to add a delay here or directory watchers may still pick up the changes
  240. var task = Task.Delay(1000);
  241. // Have to block here to allow exceptions to bubble
  242. Task.WaitAll(task);
  243. }
  244. finally
  245. {
  246. // No need to start if scanning the library because it will handle it
  247. if (!request.RefreshLibrary)
  248. {
  249. _libraryMonitor.Start();
  250. }
  251. }
  252. if (request.RefreshLibrary)
  253. {
  254. _libraryManager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None);
  255. }
  256. }
  257. /// <summary>
  258. /// Deletes the specified request.
  259. /// </summary>
  260. /// <param name="request">The request.</param>
  261. public void Delete(RemoveVirtualFolder request)
  262. {
  263. if (string.IsNullOrWhiteSpace(request.Name))
  264. {
  265. throw new ArgumentNullException("request");
  266. }
  267. var rootFolderPath = _appPaths.DefaultUserViewsPath;
  268. var path = Path.Combine(rootFolderPath, request.Name);
  269. if (!Directory.Exists(path))
  270. {
  271. throw new DirectoryNotFoundException("The media folder does not exist");
  272. }
  273. _libraryMonitor.Stop();
  274. try
  275. {
  276. _fileSystem.DeleteDirectory(path, true);
  277. // Need to add a delay here or directory watchers may still pick up the changes
  278. var delayTask = Task.Delay(1000);
  279. // Have to block here to allow exceptions to bubble
  280. Task.WaitAll(delayTask);
  281. }
  282. finally
  283. {
  284. // No need to start if scanning the library because it will handle it
  285. if (!request.RefreshLibrary)
  286. {
  287. _libraryMonitor.Start();
  288. }
  289. }
  290. if (request.RefreshLibrary)
  291. {
  292. _libraryManager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None);
  293. }
  294. }
  295. /// <summary>
  296. /// Posts the specified request.
  297. /// </summary>
  298. /// <param name="request">The request.</param>
  299. public void Post(AddMediaPath request)
  300. {
  301. if (string.IsNullOrWhiteSpace(request.Name))
  302. {
  303. throw new ArgumentNullException("request");
  304. }
  305. _libraryMonitor.Stop();
  306. try
  307. {
  308. LibraryHelpers.AddMediaPath(_fileSystem, request.Name, request.Path, _appPaths);
  309. // Need to add a delay here or directory watchers may still pick up the changes
  310. var task = Task.Delay(1000);
  311. // Have to block here to allow exceptions to bubble
  312. Task.WaitAll(task);
  313. }
  314. finally
  315. {
  316. // No need to start if scanning the library because it will handle it
  317. if (!request.RefreshLibrary)
  318. {
  319. _libraryMonitor.Start();
  320. }
  321. }
  322. if (request.RefreshLibrary)
  323. {
  324. _libraryManager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None);
  325. }
  326. }
  327. /// <summary>
  328. /// Deletes the specified request.
  329. /// </summary>
  330. /// <param name="request">The request.</param>
  331. public void Delete(RemoveMediaPath request)
  332. {
  333. if (string.IsNullOrWhiteSpace(request.Name))
  334. {
  335. throw new ArgumentNullException("request");
  336. }
  337. _libraryMonitor.Stop();
  338. try
  339. {
  340. LibraryHelpers.RemoveMediaPath(_fileSystem, request.Name, request.Path, _appPaths);
  341. // Need to add a delay here or directory watchers may still pick up the changes
  342. var task = Task.Delay(1000);
  343. // Have to block here to allow exceptions to bubble
  344. Task.WaitAll(task);
  345. }
  346. finally
  347. {
  348. // No need to start if scanning the library because it will handle it
  349. if (!request.RefreshLibrary)
  350. {
  351. _libraryMonitor.Start();
  352. }
  353. }
  354. if (request.RefreshLibrary)
  355. {
  356. _libraryManager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None);
  357. }
  358. }
  359. }
  360. }