using System;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Plugin.JRay.Models;
using Jellyfin.Plugin.JRay.Services.Interfaces;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace Jellyfin.Plugin.JRay.Controllers;
///
/// Accepts scene-actor-extraction "truth" data pushed directly by a remote
/// extraction worker, for servers that cannot run the extraction pipeline
/// locally. See SPEC.md.
///
[ApiController]
[Route("Plugins/JRay/Items/{itemId}/Truth")]
[Authorize(Roles = "Administrator")]
public class TruthController : ControllerBase
{
private const int SupportedSchemaVersion = 1;
private readonly IManagedTruthStore _managedTruthStore;
private readonly ITruthDataService _truthDataService;
///
/// Initializes a new instance of the class.
///
/// The managed truth store.
/// The truth data service.
public TruthController(IManagedTruthStore managedTruthStore, ITruthDataService truthDataService)
{
_managedTruthStore = managedTruthStore;
_truthDataService = truthDataService;
}
///
/// Uploads (creates or replaces) the truth data for an item.
///
/// The Jellyfin item id.
/// The truth file contents.
/// Cancellation token.
/// 204 on success, or 400 if the schema version is unsupported.
[HttpPut]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task PutTruth(Guid itemId, [FromBody] TruthFile truth, CancellationToken cancellationToken)
{
if (truth.SchemaVersion != SupportedSchemaVersion)
{
return BadRequest($"Unsupported schema_version {truth.SchemaVersion}; expected {SupportedSchemaVersion}.");
}
await _managedTruthStore.SaveAsync(itemId, truth, cancellationToken).ConfigureAwait(false);
_truthDataService.Invalidate(itemId);
return NoContent();
}
///
/// Removes any managed truth data for an item. The item falls back to
/// its sidecar truth file (if any) on subsequent reads.
///
/// The Jellyfin item id.
/// 204, whether or not managed data existed.
[HttpDelete]
[ProducesResponseType(StatusCodes.Status204NoContent)]
public IActionResult DeleteTruth(Guid itemId)
{
_managedTruthStore.Delete(itemId);
_truthDataService.Invalidate(itemId);
return NoContent();
}
}