Use async functions

This commit is contained in:
1hitsong 2022-09-22 08:13:53 -04:00
parent 35399ce8fe
commit a50bdb4770
6 changed files with 31 additions and 16 deletions

View File

@ -397,7 +397,7 @@ namespace Jellyfin.Api.Controllers
/// <returns>An <see cref="OkResult"/> containing the item's lyrics.</returns>
[HttpGet("Users/{userId}/Items/{itemId}/Lyrics")]
[ProducesResponseType(StatusCodes.Status200OK)]
public ActionResult<LyricResponse> GetLyrics([FromRoute, Required] Guid userId, [FromRoute, Required] Guid itemId)
public async Task<ActionResult<LyricResponse>> GetLyrics([FromRoute, Required] Guid userId, [FromRoute, Required] Guid itemId)
{
var user = _userManager.GetUserById(userId);
@ -415,7 +415,7 @@ namespace Jellyfin.Api.Controllers
return NotFound();
}
var result = _lyricManager.GetLyrics(item);
var result = await _lyricManager.GetLyrics(item).ConfigureAwait(false);
if (result is not null)
{
return Ok(result);

View File

@ -1,3 +1,4 @@
using System.Threading.Tasks;
using MediaBrowser.Controller.Entities;
namespace MediaBrowser.Controller.Lyrics;
@ -11,8 +12,8 @@ public interface ILyricManager
/// Gets the lyrics.
/// </summary>
/// <param name="item">The media item.</param>
/// <returns>Lyrics for passed item.</returns>
LyricResponse? GetLyrics(BaseItem item);
/// <returns>A task representing found lyrics the passed item.</returns>
Task<LyricResponse?> GetLyrics(BaseItem item);
/// <summary>
/// Checks if requested item has a matching local lyric file.

View File

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Resolvers;
@ -30,6 +31,6 @@ public interface ILyricProvider
/// Gets the lyrics.
/// </summary>
/// <param name="item">The media item.</param>
/// <returns>If found, returns lyrics for passed item; otherwise, null.</returns>
LyricResponse? GetLyrics(BaseItem item);
/// <returns>A task representing found lyrics.</returns>
Task<LyricResponse?> GetLyrics(BaseItem item);
}

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using LrcParser.Model;
using LrcParser.Parser;
using MediaBrowser.Controller.Entities;
@ -50,7 +51,7 @@ public class LrcLyricProvider : ILyricProvider
/// </summary>
/// <param name="item">The item to to process.</param>
/// <returns>If provider can determine lyrics, returns a <see cref="LyricResponse"/> with or without metadata; otherwise, null.</returns>
public LyricResponse? GetLyrics(BaseItem item)
public async Task<LyricResponse?> GetLyrics(BaseItem item)
{
string? lyricFilePath = this.GetLyricFilePath(item.Path);
@ -60,7 +61,7 @@ public class LrcLyricProvider : ILyricProvider
}
var fileMetaData = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
string lrcFileContent = File.ReadAllText(lyricFilePath);
string lrcFileContent = await Task.FromResult(File.ReadAllText(lyricFilePath)).ConfigureAwait(false);
Song lyricData;
@ -94,15 +95,15 @@ public class LrcLyricProvider : ILyricProvider
// Remove square bracket before field name, and after field value
// Example 1: [au: 1hitsong]
// Example 2: [ar: Calabrese]
var metaDataFieldNameSpan = metaDataRow.AsSpan(1, index - 1).Trim();
var metaDataFieldValueSpan = metaDataRow.AsSpan(index + 1, metaDataRow.Length - index - 2).Trim();
var metaDataFieldName = GetMetadataFieldName(metaDataRow, index);
var metaDataFieldValue = GetMetadataValue(metaDataRow, index);
if (metaDataFieldValueSpan.IsEmpty || metaDataFieldValueSpan.IsEmpty)
if (string.IsNullOrEmpty(metaDataFieldName) || string.IsNullOrEmpty(metaDataFieldValue))
{
continue;
}
fileMetaData[metaDataFieldNameSpan.ToString()] = metaDataFieldValueSpan.ToString();
fileMetaData[metaDataFieldName.ToString()] = metaDataFieldValue.ToString();
}
if (sortedLyricData.Count == 0)
@ -197,4 +198,14 @@ public class LrcLyricProvider : ILyricProvider
return lyricMetadata;
}
private static string GetMetadataFieldName(string metaDataRow, int index)
{
return metaDataRow.AsSpan(1, index - 1).Trim().ToString();
}
private static string GetMetadataValue(string metaDataRow, int index)
{
return metaDataRow.AsSpan(index + 1, metaDataRow.Length - index - 2).Trim().ToString();
}
}

View File

@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Lyrics;
@ -22,11 +23,11 @@ public class LyricManager : ILyricManager
}
/// <inheritdoc />
public LyricResponse? GetLyrics(BaseItem item)
public async Task<LyricResponse?> GetLyrics(BaseItem item)
{
foreach (ILyricProvider provider in _lyricProviders)
{
var results = provider.GetLyrics(item);
var results = await provider.GetLyrics(item).ConfigureAwait(false);
if (results is not null)
{
return results;

View File

@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Lyrics;
using MediaBrowser.Controller.Resolvers;
@ -29,7 +30,7 @@ public class TxtLyricProvider : ILyricProvider
/// </summary>
/// <param name="item">The item to to process.</param>
/// <returns>If provider can determine lyrics, returns a <see cref="LyricResponse"/>; otherwise, null.</returns>
public LyricResponse? GetLyrics(BaseItem item)
public async Task<LyricResponse?> GetLyrics(BaseItem item)
{
string? lyricFilePath = this.GetLyricFilePath(item.Path);
@ -38,7 +39,7 @@ public class TxtLyricProvider : ILyricProvider
return null;
}
string[] lyricTextLines = File.ReadAllLines(lyricFilePath);
string[] lyricTextLines = await Task.FromResult(File.ReadAllLines(lyricFilePath)).ConfigureAwait(false);
if (lyricTextLines.Length == 0)
{