Kavita/API/Controllers/ReaderController.cs

50 lines
1.6 KiB
C#

using System;
using System.Linq;
using System.Threading.Tasks;
using API.Comparators;
using API.DTOs;
using API.Entities;
using API.Interfaces;
using Microsoft.AspNetCore.Mvc;
namespace API.Controllers
{
public class ReaderController : BaseApiController
{
private readonly IDirectoryService _directoryService;
private readonly ICacheService _cacheService;
public ReaderController(IDirectoryService directoryService, ICacheService cacheService)
{
_directoryService = directoryService;
_cacheService = cacheService;
}
[HttpGet("info")]
public async Task<ActionResult<int>> GetInformation(int volumeId)
{
Volume volume = await _cacheService.Ensure(volumeId);
if (volume == null || !volume.Files.Any())
{
// TODO: Move this into Ensure and return negative numbers for different error codes.
return BadRequest("There are no files in the volume to read.");
}
return Ok(volume.Files.Select(x => x.NumberOfPages).Sum());
}
[HttpGet("image")]
public async Task<ActionResult<ImageDto>> GetImage(int volumeId, int page)
{
// Temp let's iterate the directory each call to get next image
var volume = await _cacheService.Ensure(volumeId);
var path = _cacheService.GetCachedPagePath(volume, page);
var file = await _directoryService.ReadImageAsync(path);
file.Page = page;
return Ok(file);
}
}
}