// Kyoo - A portable and vast media library solution.
// Copyright (c) Kyoo.
//
// See AUTHORS.md and LICENSE file in the project root for full license information.
//
// Kyoo is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// Kyoo is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Kyoo. If not, see .
using System.Threading.Tasks;
using Kyoo.Abstractions.Controllers;
using Kyoo.Abstractions.Models;
using Kyoo.Abstractions.Models.Attributes;
using Kyoo.Abstractions.Models.Permissions;
using Kyoo.Abstractions.Models.Utils;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using static Kyoo.Abstractions.Models.Utils.Constants;
namespace Kyoo.Core.Api
{
///
/// Retrieve information of an as a .
/// A watch item is another representation of an episode in a form easier to read and display for playback.
/// It contains streams (video, audio, subtitles) information, chapters, next and previous episodes and a bit of
/// information of the show.
///
[Route("watch")]
[Route("watchitem", Order = AlternativeRoute)]
[ApiController]
[ApiDefinition("Watch Items", Group = WatchGroup)]
public class WatchApi : ControllerBase
{
///
/// The library manager used to modify or retrieve information in the data store.
///
private readonly ILibraryManager _libraryManager;
///
/// A file system used to retrieve chapters informations.
///
private readonly IFileSystem _files;
///
/// The transcoder used to list fonts.
///
private readonly ITranscoder _transcoder;
///
/// Create a new .
///
///
/// The library manager used to modify or retrieve information in the data store.
///
/// A file system used to retrieve chapters informations.
/// The transcoder used to list fonts.
public WatchApi(ILibraryManager libraryManager, IFileSystem fs, ITranscoder transcoder)
{
_libraryManager = libraryManager;
_files = fs;
_transcoder = transcoder;
}
///
/// Get a watch item
///
///
/// Retrieve a watch item of an episode.
///
/// The ID or slug of the .
/// A page of items.
/// No episode with the given ID or slug could be found.
[HttpGet("{identifier:id}")]
[Permission("watch", Kind.Read)]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task> GetWatchItem(Identifier identifier)
{
Episode item = await identifier.Match(
id => _libraryManager.GetOrDefault(id),
slug => _libraryManager.GetOrDefault(slug)
);
if (item == null)
return NotFound();
return await WatchItem.FromEpisode(item, _libraryManager, _files, _transcoder);
}
///
/// Get font
///
///
/// Get a font file that is used in subtitles of this episode.
///
/// The ID or slug of the .
/// The slug of the font to retrieve.
/// A page of collections.
/// No show with the given ID/slug could be found or the font does not exist.
[HttpGet("{identifier:id}/fonts/{slug}")]
[HttpGet("{identifier:id}/font/{slug}", Order = AlternativeRoute)]
[PartialPermission(Kind.Read)]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task GetFont(Identifier identifier, string slug)
{
Episode episode = await identifier.Match(
id => _libraryManager.GetOrDefault(id),
slug => _libraryManager.GetOrDefault(slug)
);
if (episode == null)
return NotFound();
if (slug.Contains('.'))
slug = slug[..slug.LastIndexOf('.')];
Font font = await _transcoder.GetFont(episode, slug);
if (font == null)
return NotFound();
return _files.FileResult(font.Path);
}
}
}