First pass for supporting lazy loading and not using base64 strings for loading images in UI.

This commit is contained in:
Joseph Milazzo 2021-03-12 12:22:04 -06:00
parent fea1f3d152
commit 33515ad865
6 changed files with 98 additions and 3 deletions

View File

@ -0,0 +1,79 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using API.DTOs;
using API.Interfaces;
using API.Interfaces.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
namespace API.Controllers
{
public class ImageController : BaseApiController
{
private readonly IDirectoryService _directoryService;
private readonly ICacheService _cacheService;
private readonly ILogger<ImageController> _logger;
private readonly IUnitOfWork _unitOfWork;
public ImageController(IDirectoryService directoryService, ICacheService cacheService,
ILogger<ImageController> logger, IUnitOfWork unitOfWork)
{
_directoryService = directoryService;
_cacheService = cacheService;
_logger = logger;
_unitOfWork = unitOfWork;
}
[HttpGet("chapter-cover")]
public async Task<ActionResult> GetChapterCoverImage(int chapterId)
{
// TODO: Write custom methods to just get the byte[] as fast as possible
var chapter = await _unitOfWork.VolumeRepository.GetChapterAsync(chapterId);
var content = chapter.CoverImage;
var format = "jpeg"; //Path.GetExtension("jpeg").Replace(".", "");
// Calculates SHA1 Hash for byte[]
using var sha1 = new System.Security.Cryptography.SHA1CryptoServiceProvider();
Response.Headers.Add("ETag", string.Concat(sha1.ComputeHash(content).Select(x => x.ToString("X2"))));
Response.Headers.Add("Cache-Control", "private");
return File(content, "image/" + format);
}
[HttpGet("volume-cover")]
public async Task<ActionResult> GetVolumeCoverImage(int volumeId)
{
var volume = await _unitOfWork.SeriesRepository.GetVolumeAsync(volumeId);
var content = volume.CoverImage;
var format = "jpeg"; //Path.GetExtension("jpeg").Replace(".", "");
// Calculates SHA1 Hash for byte[]
using var sha1 = new System.Security.Cryptography.SHA1CryptoServiceProvider();
Response.Headers.Add("ETag", string.Concat(sha1.ComputeHash(content).Select(x => x.ToString("X2"))));
Response.Headers.Add("Cache-Control", "private");
return File(content, "image/" + format);
}
[HttpGet("series-cover")]
public async Task<ActionResult> GetSeriesCoverImage(int seriesId)
{
var series = await _unitOfWork.SeriesRepository.GetSeriesByIdAsync(seriesId);
var content = series.CoverImage;
var format = "jpeg"; //Path.GetExtension("jpeg").Replace(".", "");
if (content.Length == 0)
{
// How do I handle?
}
// Calculates SHA1 Hash for byte[]
using var sha1 = new System.Security.Cryptography.SHA1CryptoServiceProvider();
Response.Headers.Add("ETag", string.Concat(sha1.ComputeHash(content).Select(x => x.ToString("X2"))));
Response.Headers.Add("Cache-Control", "private");
return File(content, "image/" + format);
}
}
}

View File

@ -13,7 +13,7 @@ namespace API.DTOs
/// Smallest number of the Range.
/// </summary>
public string Number { get; set; }
public byte[] CoverImage { get; set; }
//public byte[] CoverImage { get; set; }
/// <summary>
/// Total number of pages in all MangaFiles
/// </summary>

View File

@ -5,7 +5,7 @@ namespace API.DTOs
public class MangaFileDto
{
public string FilePath { get; set; }
public int NumberOfPages { get; set; }
public int NumberOfPages { get; set; } // TODO: Refactor to Pages
public MangaFormat Format { get; set; }
}

View File

@ -9,7 +9,7 @@ namespace API.DTOs
public int Id { get; set; }
public int Number { get; set; }
public string Name { get; set; }
public byte[] CoverImage { get; set; }
//public byte[] CoverImage { get; set; }
public int Pages { get; set; }
public int PagesRead { get; set; }
public DateTime LastModified { get; set; }

View File

@ -143,6 +143,16 @@ namespace API.Data
.SingleOrDefaultAsync(vol => vol.Id == volumeId);
}
public async Task<VolumeDto> GetVolumeDtoAsync(int volumeId)
{
return await _context.Volume
.Where(vol => vol.Id == volumeId)
.AsNoTracking()
.ProjectTo<VolumeDto>(_mapper.ConfigurationProvider)
.SingleAsync();
}
public async Task<VolumeDto> GetVolumeDtoAsync(int volumeId, int userId)
{
var volume = await _context.Volume

View File

@ -34,6 +34,12 @@ namespace API.Interfaces
Task<SeriesDto> GetSeriesDtoByIdAsync(int seriesId, int userId);
Task<Volume> GetVolumeAsync(int volumeId);
Task<VolumeDto> GetVolumeDtoAsync(int volumeId, int userId);
/// <summary>
/// A fast lookup of just the volume information with no tracking.
/// </summary>
/// <param name="volumeId"></param>
/// <returns></returns>
Task<VolumeDto> GetVolumeDtoAsync(int volumeId);
Task<IEnumerable<Volume>> GetVolumesForSeriesAsync(int[] seriesIds);
Task<bool> DeleteSeriesAsync(int seriesId);
Task<Volume> GetVolumeByIdAsync(int volumeId);