Files
ersatztv/ErsatzTV/Controllers/Api/ImagesController.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

82 lines
3.2 KiB
C#

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using ErsatzTV.Application.Images;
using ErsatzTV.Controllers.Api.Requests;
using ErsatzTV.Core;
using ErsatzTV.Core.Api.Images;
using ErsatzTV.Extensions;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace ErsatzTV.Controllers.Api;
[ApiController]
public class ImagesController(IMediator mediator) : ControllerBase
{
[HttpGet("/api/images/folders", Name = "GetImageFolders")]
[Tags("Images")]
[EndpointSummary("List image library folders")]
[EndpointDescription("Omit parentId for the top-level folders; pass a folder id to list that folder's children.")]
[EndpointGroupName("general")]
[ProducesResponseType(typeof(List<ImageFolderResponseModel>), StatusCodes.Status200OK)]
public async Task<List<ImageFolderResponseModel>> GetFolders(
[FromQuery]
[Description("Parent image library-folder id; omit for the top-level folders")]
int? parentId = null,
CancellationToken cancellationToken = default)
{
Option<int> maybeParentId = parentId ?? Option<int>.None;
List<ImageFolderViewModel> folders = await mediator.Send(new GetImageFolders(maybeParentId), cancellationToken);
return folders.Map(Project).ToList();
}
[HttpPut("/api/images/folders/{id:int}/duration", Name = "UpdateImageFolderDuration")]
[Tags("Images")]
[EndpointSummary("Set or clear an image folder's playout duration")]
[EndpointDescription(
"Pass a positive durationSeconds to set the per-image duration for this folder (cascades to descendant " +
"images that don't override it); pass null to clear it and inherit from an ancestor.")]
[EndpointGroupName("general")]
[ProducesResponseType(typeof(UpdateImageFolderDurationResponseModel), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
public async Task<IActionResult> UpdateDuration(
int id,
[Required] [FromBody] UpdateImageFolderDurationRequest request,
CancellationToken cancellationToken)
{
if (request.DurationSeconds is <= 0)
{
return BadRequest(
new ProblemDetails
{
Status = StatusCodes.Status400BadRequest,
Title = "Validation failed",
Detail = "durationSeconds must be greater than zero, or null to clear"
});
}
bool exists = await mediator.Send(new ImageFolderExists(id), cancellationToken);
if (!exists)
{
return ApiResults.NotFoundProblem("Image folder not found");
}
double? duration = await mediator.Send(
new UpdateImageFolderDuration(id, request.DurationSeconds),
cancellationToken);
return Ok(new UpdateImageFolderDurationResponseModel(duration));
}
private static ImageFolderResponseModel Project(ImageFolderViewModel vm) =>
new(
vm.LibraryFolderId,
vm.Name,
vm.FullPath,
vm.SubfolderCount,
vm.ImageCount,
vm.DurationSeconds.ToNullable());
}