API: Fixing watch items images and previous/next handling

This commit is contained in:
Zoe Roux 2021-10-10 21:25:00 +02:00
parent 504bd5bca8
commit 6a3e48a1d1
4 changed files with 29 additions and 7 deletions

View File

@ -18,6 +18,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
@ -32,7 +33,7 @@ namespace Kyoo.Abstractions.Models
/// Information about tracks and display information that could be used by the player.
/// This contains mostly data from an <see cref="Episode"/> with another form.
/// </summary>
public class WatchItem
public class WatchItem : CustomTypeDescriptor, IThumbnails
{
/// <summary>
/// The ID of the episode associated with this item.
@ -101,6 +102,9 @@ namespace Kyoo.Abstractions.Models
/// </summary>
public bool IsMovie { get; set; }
/// <inheritdoc />
public Dictionary<int, string> Images { get; set; }
/// <summary>
/// The container of the video file of this episode.
/// Common containers are mp4, mkv, avi and so on.
@ -147,11 +151,11 @@ namespace Kyoo.Abstractions.Models
if (ep.AbsoluteNumber != null)
{
previous = await library.GetOrDefault(
x => x.ShowID == ep.ShowID && x.AbsoluteNumber <= ep.AbsoluteNumber,
x => x.ShowID == ep.ShowID && x.AbsoluteNumber < ep.AbsoluteNumber,
new Sort<Episode>(x => x.AbsoluteNumber, true)
);
next = await library.GetOrDefault(
x => x.ShowID == ep.ShowID && x.AbsoluteNumber >= ep.AbsoluteNumber,
x => x.ShowID == ep.ShowID && x.AbsoluteNumber > ep.AbsoluteNumber,
new Sort<Episode>(x => x.AbsoluteNumber)
);
}
@ -195,6 +199,7 @@ namespace Kyoo.Abstractions.Models
Title = ep.Title,
ReleaseDate = ep.ReleaseDate,
Path = ep.Path,
Images = ep.Show.Images,
Container = PathIO.GetExtension(ep.Path)![1..],
Video = ep.Tracks.FirstOrDefault(x => x.Type == StreamType.Video),
Audios = ep.Tracks.Where(x => x.Type == StreamType.Audio).ToArray(),
@ -232,5 +237,17 @@ namespace Kyoo.Abstractions.Models
return Array.Empty<Chapter>();
}
}
/// <inheritdoc />
public override string GetClassName()
{
return nameof(Show);
}
/// <inheritdoc />
public override string GetComponentName()
{
return ShowSlug;
}
}
}

View File

@ -152,13 +152,15 @@ namespace Kyoo.Core.Api
/// <inheritdoc />
public object GetValue(object target)
{
if (target is not (IThumbnails thumb and IResource res)
string slug = (target as IResource)?.Slug ?? (target as ICustomTypeDescriptor)?.GetComponentName();
if (target is not IThumbnails thumb
|| slug == null
|| string.IsNullOrEmpty(thumb.Images?.GetValueOrDefault(_imageIndex)))
return null;
string type = target is ICustomTypeDescriptor descriptor
? descriptor.GetClassName()
: target.GetType().Name;
return new Uri(_host, $"/api/{type}/{res.Slug}/{Images.ImageName[_imageIndex]}".ToLower())
return new Uri(_host, $"/api/{type}/{slug}/{Images.ImageName[_imageIndex]}".ToLower())
.ToString();
}
}

View File

@ -17,6 +17,7 @@
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
@ -75,11 +76,13 @@ namespace Kyoo.Core.Api
/// <param name="extension">An optional extension for the subtitle file.</param>
/// <returns>The subtitle file</returns>
/// <response code="404">No subtitle exist with the given ID or slug.</response>
[HttpGet("{identifier:id}", Order = AlternativeRoute)]
[HttpGet("{identifier:int}", Order = AlternativeRoute)]
[HttpGet("{identifier:id}.{extension}")]
[PartialPermission(Kind.Read)]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[SuppressMessage("ReSharper", "RouteTemplates.ParameterTypeAndConstraintsMismatch",
Justification = "An indentifier can be constructed with an int.")]
public async Task<IActionResult> GetSubtitle(Identifier identifier, string extension)
{
Track subtitle = await identifier.Match(

@ -1 +1 @@
Subproject commit 7bf53b40080d1d43228f1cdcad510b302ead99ff
Subproject commit 846dbcb22ed29244a2384d240180c821ec18df2b