LyricsController.cs 9.1 KB

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