LyricsController.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.IO;
  5. using System.Net.Mime;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using Jellyfin.Api.Attributes;
  9. using Jellyfin.Api.Extensions;
  10. using Jellyfin.Extensions;
  11. using MediaBrowser.Common.Api;
  12. using MediaBrowser.Controller.Entities.Audio;
  13. using MediaBrowser.Controller.Library;
  14. using MediaBrowser.Controller.Lyrics;
  15. using MediaBrowser.Controller.Providers;
  16. using MediaBrowser.Model.IO;
  17. using MediaBrowser.Model.Lyrics;
  18. using MediaBrowser.Model.Providers;
  19. using Microsoft.AspNetCore.Authorization;
  20. using Microsoft.AspNetCore.Http;
  21. using Microsoft.AspNetCore.Mvc;
  22. namespace Jellyfin.Api.Controllers;
  23. /// <summary>
  24. /// Lyrics controller.
  25. /// </summary>
  26. [Route("")]
  27. public class LyricsController : BaseJellyfinApiController
  28. {
  29. private readonly ILibraryManager _libraryManager;
  30. private readonly ILyricManager _lyricManager;
  31. private readonly IProviderManager _providerManager;
  32. private readonly IFileSystem _fileSystem;
  33. private readonly IUserManager _userManager;
  34. /// <summary>
  35. /// Initializes a new instance of the <see cref="LyricsController"/> class.
  36. /// </summary>
  37. /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
  38. /// <param name="lyricManager">Instance of the <see cref="ILyricManager"/> interface.</param>
  39. /// <param name="providerManager">Instance of the <see cref="IProviderManager"/> interface.</param>
  40. /// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param>
  41. /// <param name="userManager">Instance of the <see cref="IUserManager"/> interface.</param>
  42. public LyricsController(
  43. ILibraryManager libraryManager,
  44. ILyricManager lyricManager,
  45. IProviderManager providerManager,
  46. IFileSystem fileSystem,
  47. IUserManager userManager)
  48. {
  49. _libraryManager = libraryManager;
  50. _lyricManager = lyricManager;
  51. _providerManager = providerManager;
  52. _fileSystem = fileSystem;
  53. _userManager = userManager;
  54. }
  55. /// <summary>
  56. /// Gets an item's lyrics.
  57. /// </summary>
  58. /// <param name="itemId">Item id.</param>
  59. /// <response code="200">Lyrics returned.</response>
  60. /// <response code="404">Something went wrong. No Lyrics will be returned.</response>
  61. /// <returns>An <see cref="OkResult"/> containing the item's lyrics.</returns>
  62. [HttpGet("Audio/{itemId}/Lyrics")]
  63. [Authorize]
  64. [ProducesResponseType(StatusCodes.Status200OK)]
  65. public async Task<ActionResult<LyricDto>> GetLyrics([FromRoute, Required] Guid itemId)
  66. {
  67. var isApiKey = User.GetIsApiKey();
  68. var userId = User.GetUserId();
  69. if (!isApiKey && userId.IsEmpty())
  70. {
  71. return BadRequest();
  72. }
  73. var audio = _libraryManager.GetItemById<Audio>(itemId);
  74. if (audio is null)
  75. {
  76. return NotFound();
  77. }
  78. if (!isApiKey)
  79. {
  80. var user = _userManager.GetUserById(userId);
  81. if (user is null)
  82. {
  83. return NotFound();
  84. }
  85. // Check the item is visible for the user
  86. if (!audio.IsVisible(user))
  87. {
  88. return Unauthorized($"{user.Username} is not permitted to access item {audio.Name}.");
  89. }
  90. }
  91. var result = await _lyricManager.GetLyricsAsync(audio, CancellationToken.None).ConfigureAwait(false);
  92. if (result is not null)
  93. {
  94. return Ok(result);
  95. }
  96. return NotFound();
  97. }
  98. /// <summary>
  99. /// Upload an external lyric file.
  100. /// </summary>
  101. /// <param name="itemId">The item the lyric belongs to.</param>
  102. /// <param name="fileName">Name of the file being uploaded.</param>
  103. /// <response code="200">Lyrics uploaded.</response>
  104. /// <response code="400">Error processing upload.</response>
  105. /// <response code="404">Item not found.</response>
  106. /// <returns>The uploaded lyric.</returns>
  107. [HttpPost("Audio/{itemId}/Lyrics")]
  108. [Authorize(Policy = Policies.LyricManagement)]
  109. [AcceptsFile(MediaTypeNames.Text.Plain)]
  110. [ProducesResponseType(StatusCodes.Status200OK)]
  111. [ProducesResponseType(StatusCodes.Status400BadRequest)]
  112. [ProducesResponseType(StatusCodes.Status404NotFound)]
  113. public async Task<ActionResult<LyricDto>> UploadLyrics(
  114. [FromRoute, Required] Guid itemId,
  115. [FromQuery, Required] string fileName)
  116. {
  117. var audio = _libraryManager.GetItemById<Audio>(itemId);
  118. if (audio is null)
  119. {
  120. return NotFound();
  121. }
  122. if (Request.ContentLength.GetValueOrDefault(0) == 0)
  123. {
  124. return BadRequest("No lyrics uploaded");
  125. }
  126. // Utilize Path.GetExtension as it provides extra path validation.
  127. var format = Path.GetExtension(fileName.AsSpan()).RightPart('.').ToString();
  128. if (string.IsNullOrEmpty(format))
  129. {
  130. return BadRequest("Extension is required on filename");
  131. }
  132. var stream = new MemoryStream();
  133. await using (stream.ConfigureAwait(false))
  134. {
  135. await Request.Body.CopyToAsync(stream).ConfigureAwait(false);
  136. var uploadedLyric = await _lyricManager.SaveLyricAsync(
  137. audio,
  138. format,
  139. stream)
  140. .ConfigureAwait(false);
  141. if (uploadedLyric is null)
  142. {
  143. return BadRequest();
  144. }
  145. _providerManager.QueueRefresh(audio.Id, new MetadataRefreshOptions(new DirectoryService(_fileSystem)), RefreshPriority.High);
  146. return Ok(uploadedLyric);
  147. }
  148. }
  149. /// <summary>
  150. /// Deletes an external lyric file.
  151. /// </summary>
  152. /// <param name="itemId">The item id.</param>
  153. /// <response code="204">Lyric deleted.</response>
  154. /// <response code="404">Item not found.</response>
  155. /// <returns>A <see cref="NoContentResult"/>.</returns>
  156. [HttpDelete("Audio/{itemId}/Lyrics")]
  157. [Authorize(Policy = Policies.LyricManagement)]
  158. [ProducesResponseType(StatusCodes.Status204NoContent)]
  159. [ProducesResponseType(StatusCodes.Status404NotFound)]
  160. public async Task<ActionResult> DeleteLyrics(
  161. [FromRoute, Required] Guid itemId)
  162. {
  163. var audio = _libraryManager.GetItemById<Audio>(itemId);
  164. if (audio is null)
  165. {
  166. return NotFound();
  167. }
  168. await _lyricManager.DeleteLyricsAsync(audio).ConfigureAwait(false);
  169. return NoContent();
  170. }
  171. /// <summary>
  172. /// Search remote lyrics.
  173. /// </summary>
  174. /// <param name="itemId">The item id.</param>
  175. /// <response code="200">Lyrics retrieved.</response>
  176. /// <response code="404">Item not found.</response>
  177. /// <returns>An array of <see cref="RemoteLyricInfo"/>.</returns>
  178. [HttpGet("Audio/{itemId}/RemoteSearch/Lyrics")]
  179. [Authorize(Policy = Policies.LyricManagement)]
  180. [ProducesResponseType(StatusCodes.Status200OK)]
  181. [ProducesResponseType(StatusCodes.Status404NotFound)]
  182. public async Task<ActionResult<IReadOnlyList<RemoteLyricInfoDto>>> SearchRemoteLyrics([FromRoute, Required] Guid itemId)
  183. {
  184. var audio = _libraryManager.GetItemById<Audio>(itemId);
  185. if (audio is null)
  186. {
  187. return NotFound();
  188. }
  189. var results = await _lyricManager.SearchLyricsAsync(audio, false, CancellationToken.None).ConfigureAwait(false);
  190. return Ok(results);
  191. }
  192. /// <summary>
  193. /// Downloads a remote lyric.
  194. /// </summary>
  195. /// <param name="itemId">The item id.</param>
  196. /// <param name="lyricId">The lyric id.</param>
  197. /// <response code="200">Lyric downloaded.</response>
  198. /// <response code="404">Item not found.</response>
  199. /// <returns>A <see cref="NoContentResult"/>.</returns>
  200. [HttpPost("Audio/{itemId}/RemoteSearch/Lyrics/{lyricId}")]
  201. [Authorize(Policy = Policies.LyricManagement)]
  202. [ProducesResponseType(StatusCodes.Status200OK)]
  203. [ProducesResponseType(StatusCodes.Status404NotFound)]
  204. public async Task<ActionResult<LyricDto>> DownloadRemoteLyrics(
  205. [FromRoute, Required] Guid itemId,
  206. [FromRoute, Required] string lyricId)
  207. {
  208. var audio = _libraryManager.GetItemById<Audio>(itemId);
  209. if (audio is null)
  210. {
  211. return NotFound();
  212. }
  213. var downloadedLyrics = await _lyricManager.DownloadLyricsAsync(audio, lyricId, CancellationToken.None).ConfigureAwait(false);
  214. if (downloadedLyrics is null)
  215. {
  216. return NotFound();
  217. }
  218. _providerManager.QueueRefresh(audio.Id, new MetadataRefreshOptions(new DirectoryService(_fileSystem)), RefreshPriority.High);
  219. return Ok(downloadedLyrics);
  220. }
  221. /// <summary>
  222. /// Gets the remote lyrics.
  223. /// </summary>
  224. /// <param name="lyricId">The remote provider item id.</param>
  225. /// <response code="200">File returned.</response>
  226. /// <response code="404">Lyric not found.</response>
  227. /// <returns>A <see cref="FileStreamResult"/> with the lyric file.</returns>
  228. [HttpGet("Providers/Lyrics/{lyricId}")]
  229. [Authorize(Policy = Policies.LyricManagement)]
  230. [ProducesResponseType(StatusCodes.Status200OK)]
  231. [ProducesResponseType(StatusCodes.Status404NotFound)]
  232. public async Task<ActionResult<LyricDto>> GetRemoteLyrics([FromRoute, Required] string lyricId)
  233. {
  234. var result = await _lyricManager.GetRemoteLyricsAsync(lyricId, CancellationToken.None).ConfigureAwait(false);
  235. if (result is null)
  236. {
  237. return NotFound();
  238. }
  239. return Ok(result);
  240. }
  241. }