LibraryStructureController.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. #pragma warning disable CA1801
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using Jellyfin.Api.Constants;
  10. using MediaBrowser.Common.Progress;
  11. using MediaBrowser.Controller;
  12. using MediaBrowser.Controller.Configuration;
  13. using MediaBrowser.Controller.Entities;
  14. using MediaBrowser.Controller.Library;
  15. using MediaBrowser.Model.Configuration;
  16. using MediaBrowser.Model.Entities;
  17. using Microsoft.AspNetCore.Authorization;
  18. using Microsoft.AspNetCore.Http;
  19. using Microsoft.AspNetCore.Mvc;
  20. namespace Jellyfin.Api.Controllers
  21. {
  22. /// <summary>
  23. /// The library structure controller.
  24. /// </summary>
  25. [Route("/Library/VirtualFolders")]
  26. [Authorize(Policy = Policies.FirstTimeSetupOrElevated)]
  27. public class LibraryStructureController : BaseJellyfinApiController
  28. {
  29. private readonly IServerApplicationPaths _appPaths;
  30. private readonly ILibraryManager _libraryManager;
  31. private readonly ILibraryMonitor _libraryMonitor;
  32. /// <summary>
  33. /// Initializes a new instance of the <see cref="LibraryStructureController"/> class.
  34. /// </summary>
  35. /// <param name="serverConfigurationManager">Instance of <see cref="IServerConfigurationManager"/> interface.</param>
  36. /// <param name="libraryManager">Instance of <see cref="ILibraryManager"/> interface.</param>
  37. /// <param name="libraryMonitor">Instance of <see cref="ILibraryMonitor"/> interface.</param>
  38. public LibraryStructureController(
  39. IServerConfigurationManager serverConfigurationManager,
  40. ILibraryManager libraryManager,
  41. ILibraryMonitor libraryMonitor)
  42. {
  43. _appPaths = serverConfigurationManager.ApplicationPaths;
  44. _libraryManager = libraryManager;
  45. _libraryMonitor = libraryMonitor;
  46. }
  47. /// <summary>
  48. /// Gets all virtual folders.
  49. /// </summary>
  50. /// <param name="userId">The user id.</param>
  51. /// <response code="200">Virtual folders retrieved.</response>
  52. /// <returns>An <see cref="IEnumerable{VirtualFolderInfo}"/> with the virtual folders.</returns>
  53. [HttpGet]
  54. [ProducesResponseType(StatusCodes.Status200OK)]
  55. public ActionResult<IEnumerable<VirtualFolderInfo>> GetVirtualFolders([FromQuery] string userId)
  56. {
  57. return _libraryManager.GetVirtualFolders(true);
  58. }
  59. /// <summary>
  60. /// Adds a virtual folder.
  61. /// </summary>
  62. /// <param name="name">The name of the virtual folder.</param>
  63. /// <param name="collectionType">The type of the collection.</param>
  64. /// <param name="refreshLibrary">Whether to refresh the library.</param>
  65. /// <param name="paths">The paths of the virtual folder.</param>
  66. /// <param name="libraryOptions">The library options.</param>
  67. /// <response code="204">Folder added.</response>
  68. /// <returns>A <see cref="NoContentResult"/>.</returns>
  69. [HttpPost]
  70. [ProducesResponseType(StatusCodes.Status204NoContent)]
  71. public ActionResult AddVirtualFolder(
  72. [FromQuery] string name,
  73. [FromQuery] string collectionType,
  74. [FromQuery] bool refreshLibrary,
  75. [FromQuery] string[] paths,
  76. [FromQuery] LibraryOptions libraryOptions)
  77. {
  78. libraryOptions ??= new LibraryOptions();
  79. if (paths != null && paths.Length > 0)
  80. {
  81. libraryOptions.PathInfos = paths.Select(i => new MediaPathInfo { Path = i }).ToArray();
  82. }
  83. _libraryManager.AddVirtualFolder(name, collectionType, libraryOptions, refreshLibrary);
  84. return NoContent();
  85. }
  86. /// <summary>
  87. /// Removes a virtual folder.
  88. /// </summary>
  89. /// <param name="name">The name of the folder.</param>
  90. /// <param name="refreshLibrary">Whether to refresh the library.</param>
  91. /// <response code="204">Folder removed.</response>
  92. /// <returns>A <see cref="NoContentResult"/>.</returns>
  93. [HttpDelete]
  94. [ProducesResponseType(StatusCodes.Status204NoContent)]
  95. public ActionResult RemoveVirtualFolder(
  96. [FromQuery] string name,
  97. [FromQuery] bool refreshLibrary)
  98. {
  99. _libraryManager.RemoveVirtualFolder(name, refreshLibrary);
  100. return NoContent();
  101. }
  102. /// <summary>
  103. /// Renames a virtual folder.
  104. /// </summary>
  105. /// <param name="name">The name of the virtual folder.</param>
  106. /// <param name="newName">The new name.</param>
  107. /// <param name="refreshLibrary">Whether to refresh the library.</param>
  108. /// <response code="204">Folder renamed.</response>
  109. /// <response code="404">Library doesn't exist.</response>
  110. /// <response code="409">Library already exists.</response>
  111. /// <returns>A <see cref="NoContentResult"/> on success, a <see cref="NotFoundResult"/> if the library doesn't exist, a <see cref="ConflictResult"/> if the new name is already taken.</returns>
  112. /// <exception cref="ArgumentNullException">The new name may not be null.</exception>
  113. [HttpPost("Name")]
  114. [ProducesResponseType(StatusCodes.Status204NoContent)]
  115. [ProducesResponseType(StatusCodes.Status404NotFound)]
  116. [ProducesResponseType(StatusCodes.Status409Conflict)]
  117. public ActionResult RenameVirtualFolder(
  118. [FromQuery] string name,
  119. [FromQuery] string newName,
  120. [FromQuery] bool refreshLibrary)
  121. {
  122. if (string.IsNullOrWhiteSpace(name))
  123. {
  124. throw new ArgumentNullException(nameof(name));
  125. }
  126. if (string.IsNullOrWhiteSpace(newName))
  127. {
  128. throw new ArgumentNullException(nameof(newName));
  129. }
  130. var rootFolderPath = _appPaths.DefaultUserViewsPath;
  131. var currentPath = Path.Combine(rootFolderPath, name);
  132. var newPath = Path.Combine(rootFolderPath, newName);
  133. if (!Directory.Exists(currentPath))
  134. {
  135. return NotFound("The media collection does not exist.");
  136. }
  137. if (!string.Equals(currentPath, newPath, StringComparison.OrdinalIgnoreCase) && Directory.Exists(newPath))
  138. {
  139. return Conflict($"The media library already exists at {newPath}.");
  140. }
  141. _libraryMonitor.Stop();
  142. try
  143. {
  144. // Changing capitalization. Handle windows case insensitivity
  145. if (string.Equals(currentPath, newPath, StringComparison.OrdinalIgnoreCase))
  146. {
  147. var tempPath = Path.Combine(
  148. rootFolderPath,
  149. Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture));
  150. Directory.Move(currentPath, tempPath);
  151. currentPath = tempPath;
  152. }
  153. Directory.Move(currentPath, newPath);
  154. }
  155. finally
  156. {
  157. CollectionFolder.OnCollectionFolderChange();
  158. Task.Run(() =>
  159. {
  160. // No need to start if scanning the library because it will handle it
  161. if (refreshLibrary)
  162. {
  163. _libraryManager.ValidateMediaLibrary(new SimpleProgress<double>(), CancellationToken.None);
  164. }
  165. else
  166. {
  167. // Need to add a delay here or directory watchers may still pick up the changes
  168. var task = Task.Delay(1000);
  169. // Have to block here to allow exceptions to bubble
  170. Task.WaitAll(task);
  171. _libraryMonitor.Start();
  172. }
  173. });
  174. }
  175. return NoContent();
  176. }
  177. /// <summary>
  178. /// Add a media path to a library.
  179. /// </summary>
  180. /// <param name="name">The name of the library.</param>
  181. /// <param name="path">The path to add.</param>
  182. /// <param name="pathInfo">The path info.</param>
  183. /// <param name="refreshLibrary">Whether to refresh the library.</param>
  184. /// <returns>A <see cref="NoContentResult"/>.</returns>
  185. /// <response code="204">Media path added.</response>
  186. /// <exception cref="ArgumentNullException">The name of the library may not be empty.</exception>
  187. [HttpPost("Paths")]
  188. [ProducesResponseType(StatusCodes.Status204NoContent)]
  189. public ActionResult AddMediaPath(
  190. [FromQuery] string name,
  191. [FromQuery] string path,
  192. [FromQuery] MediaPathInfo pathInfo,
  193. [FromQuery] bool refreshLibrary)
  194. {
  195. if (string.IsNullOrWhiteSpace(name))
  196. {
  197. throw new ArgumentNullException(nameof(name));
  198. }
  199. _libraryMonitor.Stop();
  200. try
  201. {
  202. var mediaPath = pathInfo ?? new MediaPathInfo { Path = path };
  203. _libraryManager.AddMediaPath(name, mediaPath);
  204. }
  205. finally
  206. {
  207. Task.Run(() =>
  208. {
  209. // No need to start if scanning the library because it will handle it
  210. if (refreshLibrary)
  211. {
  212. _libraryManager.ValidateMediaLibrary(new SimpleProgress<double>(), CancellationToken.None);
  213. }
  214. else
  215. {
  216. // Need to add a delay here or directory watchers may still pick up the changes
  217. var task = Task.Delay(1000);
  218. // Have to block here to allow exceptions to bubble
  219. Task.WaitAll(task);
  220. _libraryMonitor.Start();
  221. }
  222. });
  223. }
  224. return NoContent();
  225. }
  226. /// <summary>
  227. /// Updates a media path.
  228. /// </summary>
  229. /// <param name="name">The name of the library.</param>
  230. /// <param name="pathInfo">The path info.</param>
  231. /// <returns>A <see cref="NoContentResult"/>.</returns>
  232. /// <response code="204">Media path updated.</response>
  233. /// <exception cref="ArgumentNullException">The name of the library may not be empty.</exception>
  234. [HttpPost("Paths/Update")]
  235. [ProducesResponseType(StatusCodes.Status204NoContent)]
  236. public ActionResult UpdateMediaPath(
  237. [FromQuery] string name,
  238. [FromQuery] MediaPathInfo pathInfo)
  239. {
  240. if (string.IsNullOrWhiteSpace(name))
  241. {
  242. throw new ArgumentNullException(nameof(name));
  243. }
  244. _libraryManager.UpdateMediaPath(name, pathInfo);
  245. return NoContent();
  246. }
  247. /// <summary>
  248. /// Remove a media path.
  249. /// </summary>
  250. /// <param name="name">The name of the library.</param>
  251. /// <param name="path">The path to remove.</param>
  252. /// <param name="refreshLibrary">Whether to refresh the library.</param>
  253. /// <returns>A <see cref="NoContentResult"/>.</returns>
  254. /// <response code="204">Media path removed.</response>
  255. /// <exception cref="ArgumentNullException">The name of the library may not be empty.</exception>
  256. [HttpDelete("Paths")]
  257. [ProducesResponseType(StatusCodes.Status204NoContent)]
  258. public ActionResult RemoveMediaPath(
  259. [FromQuery] string name,
  260. [FromQuery] string path,
  261. [FromQuery] bool refreshLibrary)
  262. {
  263. if (string.IsNullOrWhiteSpace(name))
  264. {
  265. throw new ArgumentNullException(nameof(name));
  266. }
  267. _libraryMonitor.Stop();
  268. try
  269. {
  270. _libraryManager.RemoveMediaPath(name, path);
  271. }
  272. finally
  273. {
  274. Task.Run(() =>
  275. {
  276. // No need to start if scanning the library because it will handle it
  277. if (refreshLibrary)
  278. {
  279. _libraryManager.ValidateMediaLibrary(new SimpleProgress<double>(), CancellationToken.None);
  280. }
  281. else
  282. {
  283. // Need to add a delay here or directory watchers may still pick up the changes
  284. var task = Task.Delay(1000);
  285. // Have to block here to allow exceptions to bubble
  286. Task.WaitAll(task);
  287. _libraryMonitor.Start();
  288. }
  289. });
  290. }
  291. return NoContent();
  292. }
  293. /// <summary>
  294. /// Update library options.
  295. /// </summary>
  296. /// <param name="id">The library name.</param>
  297. /// <param name="libraryOptions">The library options.</param>
  298. /// <response code="204">Library updated.</response>
  299. /// <returns>A <see cref="NoContentResult"/>.</returns>
  300. [HttpPost("LibraryOptions")]
  301. [ProducesResponseType(StatusCodes.Status204NoContent)]
  302. public ActionResult UpdateLibraryOptions(
  303. [FromQuery] string id,
  304. [FromQuery] LibraryOptions libraryOptions)
  305. {
  306. var collectionFolder = (CollectionFolder)_libraryManager.GetItemById(id);
  307. collectionFolder.UpdateLibraryOptions(libraryOptions);
  308. return NoContent();
  309. }
  310. }
  311. }