Store lyrics in the database as media streams (#9951)
parent
59f50ae8b2
commit
0bc41c015f
@ -0,0 +1,267 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using System.IO;
|
||||||
|
using System.Net.Mime;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Jellyfin.Api.Attributes;
|
||||||
|
using Jellyfin.Api.Extensions;
|
||||||
|
using Jellyfin.Extensions;
|
||||||
|
using MediaBrowser.Common.Api;
|
||||||
|
using MediaBrowser.Controller.Entities.Audio;
|
||||||
|
using MediaBrowser.Controller.Library;
|
||||||
|
using MediaBrowser.Controller.Lyrics;
|
||||||
|
using MediaBrowser.Controller.Providers;
|
||||||
|
using MediaBrowser.Model.IO;
|
||||||
|
using MediaBrowser.Model.Lyrics;
|
||||||
|
using MediaBrowser.Model.Providers;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
namespace Jellyfin.Api.Controllers;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Lyrics controller.
|
||||||
|
/// </summary>
|
||||||
|
[Route("")]
|
||||||
|
public class LyricsController : BaseJellyfinApiController
|
||||||
|
{
|
||||||
|
private readonly ILibraryManager _libraryManager;
|
||||||
|
private readonly ILyricManager _lyricManager;
|
||||||
|
private readonly IProviderManager _providerManager;
|
||||||
|
private readonly IFileSystem _fileSystem;
|
||||||
|
private readonly IUserManager _userManager;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="LyricsController"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
|
||||||
|
/// <param name="lyricManager">Instance of the <see cref="ILyricManager"/> interface.</param>
|
||||||
|
/// <param name="providerManager">Instance of the <see cref="IProviderManager"/> interface.</param>
|
||||||
|
/// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param>
|
||||||
|
/// <param name="userManager">Instance of the <see cref="IUserManager"/> interface.</param>
|
||||||
|
public LyricsController(
|
||||||
|
ILibraryManager libraryManager,
|
||||||
|
ILyricManager lyricManager,
|
||||||
|
IProviderManager providerManager,
|
||||||
|
IFileSystem fileSystem,
|
||||||
|
IUserManager userManager)
|
||||||
|
{
|
||||||
|
_libraryManager = libraryManager;
|
||||||
|
_lyricManager = lyricManager;
|
||||||
|
_providerManager = providerManager;
|
||||||
|
_fileSystem = fileSystem;
|
||||||
|
_userManager = userManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets an item's lyrics.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="itemId">Item id.</param>
|
||||||
|
/// <response code="200">Lyrics returned.</response>
|
||||||
|
/// <response code="404">Something went wrong. No Lyrics will be returned.</response>
|
||||||
|
/// <returns>An <see cref="OkResult"/> containing the item's lyrics.</returns>
|
||||||
|
[HttpGet("Audio/{itemId}/Lyrics")]
|
||||||
|
[Authorize]
|
||||||
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||||
|
public async Task<ActionResult<LyricDto>> GetLyrics([FromRoute, Required] Guid itemId)
|
||||||
|
{
|
||||||
|
var isApiKey = User.GetIsApiKey();
|
||||||
|
var userId = User.GetUserId();
|
||||||
|
if (!isApiKey && userId.IsEmpty())
|
||||||
|
{
|
||||||
|
return BadRequest();
|
||||||
|
}
|
||||||
|
|
||||||
|
var audio = _libraryManager.GetItemById<Audio>(itemId);
|
||||||
|
if (audio is null)
|
||||||
|
{
|
||||||
|
return NotFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isApiKey)
|
||||||
|
{
|
||||||
|
var user = _userManager.GetUserById(userId);
|
||||||
|
if (user is null)
|
||||||
|
{
|
||||||
|
return NotFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check the item is visible for the user
|
||||||
|
if (!audio.IsVisible(user))
|
||||||
|
{
|
||||||
|
return Unauthorized($"{user.Username} is not permitted to access item {audio.Name}.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var result = await _lyricManager.GetLyricsAsync(audio, CancellationToken.None).ConfigureAwait(false);
|
||||||
|
if (result is not null)
|
||||||
|
{
|
||||||
|
return Ok(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
return NotFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Upload an external lyric file.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="itemId">The item the lyric belongs to.</param>
|
||||||
|
/// <param name="fileName">Name of the file being uploaded.</param>
|
||||||
|
/// <response code="200">Lyrics uploaded.</response>
|
||||||
|
/// <response code="400">Error processing upload.</response>
|
||||||
|
/// <response code="404">Item not found.</response>
|
||||||
|
/// <returns>The uploaded lyric.</returns>
|
||||||
|
[HttpPost("Audio/{itemId}/Lyrics")]
|
||||||
|
[Authorize(Policy = Policies.LyricManagement)]
|
||||||
|
[AcceptsFile(MediaTypeNames.Text.Plain)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
|
public async Task<ActionResult<LyricDto>> UploadLyrics(
|
||||||
|
[FromRoute, Required] Guid itemId,
|
||||||
|
[FromQuery, Required] string fileName)
|
||||||
|
{
|
||||||
|
var audio = _libraryManager.GetItemById<Audio>(itemId);
|
||||||
|
if (audio is null)
|
||||||
|
{
|
||||||
|
return NotFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Request.ContentLength.GetValueOrDefault(0) == 0)
|
||||||
|
{
|
||||||
|
return BadRequest("No lyrics uploaded");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Utilize Path.GetExtension as it provides extra path validation.
|
||||||
|
var format = Path.GetExtension(fileName.AsSpan()).RightPart('.').ToString();
|
||||||
|
if (string.IsNullOrEmpty(format))
|
||||||
|
{
|
||||||
|
return BadRequest("Extension is required on filename");
|
||||||
|
}
|
||||||
|
|
||||||
|
var stream = new MemoryStream();
|
||||||
|
await using (stream.ConfigureAwait(false))
|
||||||
|
{
|
||||||
|
await Request.Body.CopyToAsync(stream).ConfigureAwait(false);
|
||||||
|
var uploadedLyric = await _lyricManager.UploadLyricAsync(
|
||||||
|
audio,
|
||||||
|
new LyricResponse
|
||||||
|
{
|
||||||
|
Format = format,
|
||||||
|
Stream = stream
|
||||||
|
}).ConfigureAwait(false);
|
||||||
|
|
||||||
|
if (uploadedLyric is null)
|
||||||
|
{
|
||||||
|
return BadRequest();
|
||||||
|
}
|
||||||
|
|
||||||
|
_providerManager.QueueRefresh(audio.Id, new MetadataRefreshOptions(new DirectoryService(_fileSystem)), RefreshPriority.High);
|
||||||
|
return Ok(uploadedLyric);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Deletes an external lyric file.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="itemId">The item id.</param>
|
||||||
|
/// <response code="204">Lyric deleted.</response>
|
||||||
|
/// <response code="404">Item not found.</response>
|
||||||
|
/// <returns>A <see cref="NoContentResult"/>.</returns>
|
||||||
|
[HttpDelete("Audio/{itemId}/Lyrics")]
|
||||||
|
[Authorize(Policy = Policies.LyricManagement)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
|
public async Task<ActionResult> DeleteLyrics(
|
||||||
|
[FromRoute, Required] Guid itemId)
|
||||||
|
{
|
||||||
|
var audio = _libraryManager.GetItemById<Audio>(itemId);
|
||||||
|
if (audio is null)
|
||||||
|
{
|
||||||
|
return NotFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
await _lyricManager.DeleteLyricsAsync(audio).ConfigureAwait(false);
|
||||||
|
return NoContent();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Search remote lyrics.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="itemId">The item id.</param>
|
||||||
|
/// <response code="200">Lyrics retrieved.</response>
|
||||||
|
/// <response code="404">Item not found.</response>
|
||||||
|
/// <returns>An array of <see cref="RemoteLyricInfo"/>.</returns>
|
||||||
|
[HttpGet("Audio/{itemId}/RemoteSearch/Lyrics")]
|
||||||
|
[Authorize(Policy = Policies.LyricManagement)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
|
public async Task<ActionResult<IReadOnlyList<RemoteLyricInfoDto>>> SearchRemoteLyrics([FromRoute, Required] Guid itemId)
|
||||||
|
{
|
||||||
|
var audio = _libraryManager.GetItemById<Audio>(itemId);
|
||||||
|
if (audio is null)
|
||||||
|
{
|
||||||
|
return NotFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
var results = await _lyricManager.SearchLyricsAsync(audio, false, CancellationToken.None).ConfigureAwait(false);
|
||||||
|
return Ok(results);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Downloads a remote lyric.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="itemId">The item id.</param>
|
||||||
|
/// <param name="lyricId">The lyric id.</param>
|
||||||
|
/// <response code="200">Lyric downloaded.</response>
|
||||||
|
/// <response code="404">Item not found.</response>
|
||||||
|
/// <returns>A <see cref="NoContentResult"/>.</returns>
|
||||||
|
[HttpPost("Audio/{itemId}/RemoteSearch/Lyrics/{lyricId}")]
|
||||||
|
[Authorize(Policy = Policies.LyricManagement)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
|
public async Task<ActionResult<LyricDto>> DownloadRemoteLyrics(
|
||||||
|
[FromRoute, Required] Guid itemId,
|
||||||
|
[FromRoute, Required] string lyricId)
|
||||||
|
{
|
||||||
|
var audio = _libraryManager.GetItemById<Audio>(itemId);
|
||||||
|
if (audio is null)
|
||||||
|
{
|
||||||
|
return NotFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
var downloadedLyrics = await _lyricManager.DownloadLyricsAsync(audio, lyricId, CancellationToken.None).ConfigureAwait(false);
|
||||||
|
if (downloadedLyrics is null)
|
||||||
|
{
|
||||||
|
return NotFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
_providerManager.QueueRefresh(audio.Id, new MetadataRefreshOptions(new DirectoryService(_fileSystem)), RefreshPriority.High);
|
||||||
|
return Ok(downloadedLyrics);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the remote lyrics.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="lyricId">The remote provider item id.</param>
|
||||||
|
/// <response code="200">File returned.</response>
|
||||||
|
/// <response code="404">Lyric not found.</response>
|
||||||
|
/// <returns>A <see cref="FileStreamResult"/> with the lyric file.</returns>
|
||||||
|
[HttpGet("Providers/Lyrics/{lyricId}")]
|
||||||
|
[Authorize(Policy = Policies.LyricManagement)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
|
public async Task<ActionResult<LyricDto>> GetRemoteLyrics([FromRoute, Required] string lyricId)
|
||||||
|
{
|
||||||
|
var result = await _lyricManager.GetRemoteLyricsAsync(lyricId, CancellationToken.None).ConfigureAwait(false);
|
||||||
|
if (result is null)
|
||||||
|
{
|
||||||
|
return NotFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(result);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,34 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using MediaBrowser.Model.Lyrics;
|
||||||
|
using MediaBrowser.Model.Providers;
|
||||||
|
|
||||||
|
namespace MediaBrowser.Controller.Lyrics;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Interface ILyricsProvider.
|
||||||
|
/// </summary>
|
||||||
|
public interface ILyricProvider
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the provider name.
|
||||||
|
/// </summary>
|
||||||
|
string Name { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Search for lyrics.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="request">The search request.</param>
|
||||||
|
/// <param name="cancellationToken">The cancellation token.</param>
|
||||||
|
/// <returns>The list of remote lyrics.</returns>
|
||||||
|
Task<IEnumerable<RemoteLyricInfo>> SearchAsync(LyricSearchRequest request, CancellationToken cancellationToken);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get the lyrics.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">The remote lyric id.</param>
|
||||||
|
/// <param name="cancellationToken">The cancellation token.</param>
|
||||||
|
/// <returns>The lyric response.</returns>
|
||||||
|
Task<LyricResponse?> GetLyricsAsync(string id, CancellationToken cancellationToken);
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
using System;
|
||||||
|
using MediaBrowser.Controller.Entities;
|
||||||
|
|
||||||
|
namespace MediaBrowser.Controller.Lyrics
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// An event that occurs when subtitle downloading fails.
|
||||||
|
/// </summary>
|
||||||
|
public class LyricDownloadFailureEventArgs : EventArgs
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the item.
|
||||||
|
/// </summary>
|
||||||
|
public required BaseItem Item { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the provider.
|
||||||
|
/// </summary>
|
||||||
|
public required string Provider { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the exception.
|
||||||
|
/// </summary>
|
||||||
|
public required Exception Exception { get; set; }
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
namespace MediaBrowser.Providers.Lyric;
|
namespace MediaBrowser.Model.Lyrics;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The information for a raw lyrics file before parsing.
|
/// The information for a raw lyrics file before parsing.
|
@ -1,4 +1,4 @@
|
|||||||
namespace MediaBrowser.Controller.Lyrics;
|
namespace MediaBrowser.Model.Lyrics;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Lyric model.
|
/// Lyric model.
|
@ -0,0 +1,19 @@
|
|||||||
|
using System.IO;
|
||||||
|
|
||||||
|
namespace MediaBrowser.Model.Lyrics;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// LyricResponse model.
|
||||||
|
/// </summary>
|
||||||
|
public class LyricResponse
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the lyric stream.
|
||||||
|
/// </summary>
|
||||||
|
public required Stream Stream { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the lyric format.
|
||||||
|
/// </summary>
|
||||||
|
public required string Format { get; set; }
|
||||||
|
}
|
@ -0,0 +1,59 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using MediaBrowser.Model.Entities;
|
||||||
|
|
||||||
|
namespace MediaBrowser.Model.Lyrics;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Lyric search request.
|
||||||
|
/// </summary>
|
||||||
|
public class LyricSearchRequest : IHasProviderIds
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the media path.
|
||||||
|
/// </summary>
|
||||||
|
public string? MediaPath { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the artist name.
|
||||||
|
/// </summary>
|
||||||
|
public IReadOnlyList<string>? ArtistNames { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the album name.
|
||||||
|
/// </summary>
|
||||||
|
public string? AlbumName { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the song name.
|
||||||
|
/// </summary>
|
||||||
|
public string? SongName { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the track duration in ticks.
|
||||||
|
/// </summary>
|
||||||
|
public long? Duration { get; set; }
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public Dictionary<string, string> ProviderIds { get; set; } = new(StringComparer.OrdinalIgnoreCase);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets a value indicating whether to search all providers.
|
||||||
|
/// </summary>
|
||||||
|
public bool SearchAllProviders { get; set; } = true;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the list of disabled lyric fetcher names.
|
||||||
|
/// </summary>
|
||||||
|
public IReadOnlyList<string> DisabledLyricFetchers { get; set; } = [];
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the order of lyric fetchers.
|
||||||
|
/// </summary>
|
||||||
|
public IReadOnlyList<string> LyricFetcherOrder { get; set; } = [];
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets a value indicating whether this request is automated.
|
||||||
|
/// </summary>
|
||||||
|
public bool IsAutomated { get; set; }
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
namespace MediaBrowser.Model.Lyrics;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The remote lyric info dto.
|
||||||
|
/// </summary>
|
||||||
|
public class RemoteLyricInfoDto
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the id for the lyric.
|
||||||
|
/// </summary>
|
||||||
|
public required string Id { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the provider name.
|
||||||
|
/// </summary>
|
||||||
|
public required string ProviderName { get; init; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the lyrics.
|
||||||
|
/// </summary>
|
||||||
|
public required LyricDto Lyrics { get; init; }
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
|
||||||
|
namespace MediaBrowser.Model.Lyrics;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Upload lyric dto.
|
||||||
|
/// </summary>
|
||||||
|
public class UploadLyricDto
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the lyrics file.
|
||||||
|
/// </summary>
|
||||||
|
[Required]
|
||||||
|
public IFormFile Lyrics { get; set; } = null!;
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
namespace MediaBrowser.Model.Providers;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Lyric provider info.
|
||||||
|
/// </summary>
|
||||||
|
public class LyricProviderInfo
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the provider name.
|
||||||
|
/// </summary>
|
||||||
|
public required string Name { get; init; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the provider id.
|
||||||
|
/// </summary>
|
||||||
|
public required string Id { get; init; }
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
using MediaBrowser.Model.Lyrics;
|
||||||
|
|
||||||
|
namespace MediaBrowser.Model.Providers;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The remote lyric info.
|
||||||
|
/// </summary>
|
||||||
|
public class RemoteLyricInfo
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the id for the lyric.
|
||||||
|
/// </summary>
|
||||||
|
public required string Id { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the provider name.
|
||||||
|
/// </summary>
|
||||||
|
public required string ProviderName { get; init; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the lyric metadata.
|
||||||
|
/// </summary>
|
||||||
|
public required LyricMetadata Metadata { get; init; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the lyrics.
|
||||||
|
/// </summary>
|
||||||
|
public required LyricResponse Lyrics { get; init; }
|
||||||
|
}
|
@ -1,69 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.IO;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Jellyfin.Extensions;
|
|
||||||
using MediaBrowser.Controller.Entities;
|
|
||||||
using MediaBrowser.Controller.Resolvers;
|
|
||||||
|
|
||||||
namespace MediaBrowser.Providers.Lyric;
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public class DefaultLyricProvider : ILyricProvider
|
|
||||||
{
|
|
||||||
private static readonly string[] _lyricExtensions = { ".lrc", ".elrc", ".txt" };
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public string Name => "DefaultLyricProvider";
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public ResolverPriority Priority => ResolverPriority.First;
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public bool HasLyrics(BaseItem item)
|
|
||||||
{
|
|
||||||
var path = GetLyricsPath(item);
|
|
||||||
return path is not null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public async Task<LyricFile?> GetLyrics(BaseItem item)
|
|
||||||
{
|
|
||||||
var path = GetLyricsPath(item);
|
|
||||||
if (path is not null)
|
|
||||||
{
|
|
||||||
var content = await File.ReadAllTextAsync(path).ConfigureAwait(false);
|
|
||||||
if (!string.IsNullOrEmpty(content))
|
|
||||||
{
|
|
||||||
return new LyricFile(path, content);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private string? GetLyricsPath(BaseItem item)
|
|
||||||
{
|
|
||||||
// Ensure the path to the item is not null
|
|
||||||
string? itemDirectoryPath = Path.GetDirectoryName(item.Path);
|
|
||||||
if (itemDirectoryPath is null)
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure the directory path exists
|
|
||||||
if (!Directory.Exists(itemDirectoryPath))
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var lyricFilePath in Directory.GetFiles(itemDirectoryPath, $"{Path.GetFileNameWithoutExtension(item.Path)}.*"))
|
|
||||||
{
|
|
||||||
if (_lyricExtensions.Contains(Path.GetExtension(lyricFilePath.AsSpan()), StringComparison.OrdinalIgnoreCase))
|
|
||||||
{
|
|
||||||
return lyricFilePath;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,36 +0,0 @@
|
|||||||
using System.Threading.Tasks;
|
|
||||||
using MediaBrowser.Controller.Entities;
|
|
||||||
using MediaBrowser.Controller.Resolvers;
|
|
||||||
|
|
||||||
namespace MediaBrowser.Providers.Lyric;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Interface ILyricsProvider.
|
|
||||||
/// </summary>
|
|
||||||
public interface ILyricProvider
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Gets a value indicating the provider name.
|
|
||||||
/// </summary>
|
|
||||||
string Name { get; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the priority.
|
|
||||||
/// </summary>
|
|
||||||
/// <value>The priority.</value>
|
|
||||||
ResolverPriority Priority { get; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Checks if an item has lyrics available.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="item">The media item.</param>
|
|
||||||
/// <returns>Whether lyrics where found or not.</returns>
|
|
||||||
bool HasLyrics(BaseItem item);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the lyrics.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="item">The media item.</param>
|
|
||||||
/// <returns>A task representing found lyrics.</returns>
|
|
||||||
Task<LyricFile?> GetLyrics(BaseItem item);
|
|
||||||
}
|
|
@ -0,0 +1,39 @@
|
|||||||
|
using Emby.Naming.Common;
|
||||||
|
using MediaBrowser.Controller.Entities.Audio;
|
||||||
|
using MediaBrowser.Controller.MediaEncoding;
|
||||||
|
using MediaBrowser.Model.Dlna;
|
||||||
|
using MediaBrowser.Model.Globalization;
|
||||||
|
using MediaBrowser.Model.IO;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
namespace MediaBrowser.Providers.MediaInfo;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Resolves external lyric files for <see cref="Audio"/>.
|
||||||
|
/// </summary>
|
||||||
|
public class LyricResolver : MediaInfoResolver
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="LyricResolver"/> class for external subtitle file processing.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="logger">The logger.</param>
|
||||||
|
/// <param name="localizationManager">The localization manager.</param>
|
||||||
|
/// <param name="mediaEncoder">The media encoder.</param>
|
||||||
|
/// <param name="fileSystem">The file system.</param>
|
||||||
|
/// <param name="namingOptions">The <see cref="NamingOptions"/> object containing FileExtensions, MediaDefaultFlags, MediaForcedFlags and MediaFlagDelimiters.</param>
|
||||||
|
public LyricResolver(
|
||||||
|
ILogger<LyricResolver> logger,
|
||||||
|
ILocalizationManager localizationManager,
|
||||||
|
IMediaEncoder mediaEncoder,
|
||||||
|
IFileSystem fileSystem,
|
||||||
|
NamingOptions namingOptions)
|
||||||
|
: base(
|
||||||
|
logger,
|
||||||
|
localizationManager,
|
||||||
|
mediaEncoder,
|
||||||
|
fileSystem,
|
||||||
|
namingOptions,
|
||||||
|
DlnaProfileType.Lyric)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue