Files
ersatztv/ErsatzTV/Controllers/Api/MediaItemsController.cs
T
timothyandClaude Fable 5 cd31c755bb feat(api): media detail + info + image-folder endpoints (#141/#161)
Add REST endpoints backing the SPA media detail pages and image browser:

- GET /api/movies/{id}, /api/shows/{id}, /api/seasons/{id}, /api/artists/{id}
  wrapping the existing detail queries; 404 on None.
- GET /api/media-items/{id}/info wrapping GetMediaItemInfo; UnableToLocateMediaItem
  -> 404, other errors -> 422.
- GET /api/images/folders?parentId= and PUT /api/images/folders/{id}/duration
  (validates null-or-positive -> 400; existence guard via new ImageFolderExists
  query -> 404).
- Extend GetLibraryBrowseItems parentId drill-in to Episode (episodes of a season,
  episode-number order) and MusicVideo (an artist's music videos, album/track order),
  alongside the existing TelevisionSeason branch.

Response DTOs live in ErsatzTV.Core/Api/* and never expose Application VMs. Artwork
values are rooted for the SPA via a shared ErsatzTV.Core/Api/ApiArtwork helper
(mirrors the #180/#181 browse-handler logic; handles jellyfin/emby proxy prefixes,
http passthrough, empty). Regenerated OpenAPI v1.json + web v1.d.ts. New controllers
registered in ApiControllerSecurityTests.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-07 22:43:47 +02:00

98 lines
3.8 KiB
C#

using System.ComponentModel.DataAnnotations;
using ErsatzTV.Application.MediaItems;
using ErsatzTV.Controllers.Api.Requests;
using ErsatzTV.Core;
using ErsatzTV.Core.Api.MediaItems;
using ErsatzTV.Core.Errors;
using ErsatzTV.Extensions;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace ErsatzTV.Controllers.Api;
[ApiController]
public class MediaItemsController(IMediator mediator) : ControllerBase
{
[HttpDelete("/api/media-items", Name = "DeleteMediaItems")]
[Tags("Media Items")]
[EndpointSummary("Delete media items from the database")]
[EndpointGroupName("general")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status422UnprocessableEntity)]
public async Task<IActionResult> Delete(
[Required] [FromBody] DeleteMediaItemsRequest request,
CancellationToken cancellationToken)
{
if (request.Ids is null || request.Ids.Count == 0)
{
return BaseError.New("At least one media item id is required").ToErrorResult();
}
Either<BaseError, Unit> result = await mediator.Send(request.ToCommand(), cancellationToken);
return result.ToDeletedResult();
}
[HttpGet("/api/media-items/{id:int}/info", Name = "GetMediaItemInfo")]
[Tags("Media Items")]
[EndpointSummary("Get technical media info for a media item")]
[EndpointGroupName("general")]
[ProducesResponseType(typeof(MediaItemInfoResponseModel), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status422UnprocessableEntity)]
public async Task<IActionResult> GetInfo(int id, CancellationToken cancellationToken)
{
Either<BaseError, MediaItemInfo> result = await mediator.Send(new GetMediaItemInfo(id), cancellationToken);
return result.Match(
Left: error => error is UnableToLocateMediaItem
? ApiResults.NotFoundProblem(error.Value)
: error.ToErrorResult(),
Right: info => (IActionResult)new OkObjectResult(Project(info)));
}
private static MediaItemInfoResponseModel Project(MediaItemInfo info) =>
new(
info.Id,
info.Title,
info.Kind,
info.LibraryKind,
string.IsNullOrWhiteSpace(info.ServerName) ? null : info.ServerName,
info.LibraryName,
info.State,
info.Duration,
string.IsNullOrWhiteSpace(info.SampleAspectRatio) ? null : info.SampleAspectRatio,
string.IsNullOrWhiteSpace(info.DisplayAspectRatio) ? null : info.DisplayAspectRatio,
string.IsNullOrWhiteSpace(info.RFrameRate) ? null : info.RFrameRate,
info.VideoScanKind,
info.InterlacedRatio,
info.Width,
info.Height,
info.Streams.Map(Project).ToList(),
info.Chapters.Map(Project).ToList());
private static MediaItemInfoStreamResponseModel Project(MediaItemInfoStream stream) =>
new(
stream.Index,
stream.Kind,
stream.Title,
stream.Codec,
stream.Profile,
stream.Language,
stream.Channels,
stream.Default,
stream.Forced,
stream.AttachedPic,
stream.PixelFormat,
stream.ColorRange,
stream.ColorSpace,
stream.ColorTransfer,
stream.ColorPrimaries,
stream.BitsPerRawSample,
stream.MimeType,
stream.FileName,
stream.IsExtracted);
private static MediaItemInfoChapterResponseModel Project(MediaItemInfoChapter chapter) =>
new(chapter.Title, chapter.StartTime, chapter.EndTime);
}