Cleaned up ReaderController to match the new method of loading images in FE. Sends the full files rather than base64 encoded string.

This commit is contained in:
Joseph Milazzo 2021-03-09 15:50:17 -06:00
parent c6e1fec9f2
commit 2a8931406d
5 changed files with 38 additions and 52 deletions

View File

@ -30,44 +30,34 @@ namespace API.Controllers
} }
[HttpGet("image")] [HttpGet("image")]
public async Task<ActionResult<ImageDto>> GetImage(int chapterId, int page) public async Task<ActionResult> GetImage(int chapterId, int page)
{ {
// Temp let's iterate the directory each call to get next image
var chapter = await _cacheService.Ensure(chapterId); var chapter = await _cacheService.Ensure(chapterId);
if (chapter == null) return BadRequest("There was an issue finding image file for reading"); if (chapter == null) return BadRequest("There was an issue finding image file for reading");
var (path, mangaFile) = await _cacheService.GetCachedPagePath(chapter, page); var (path, mangaFile) = await _cacheService.GetCachedPagePath(chapter, page);
if (string.IsNullOrEmpty(path)) return BadRequest($"No such image for page {page}"); if (string.IsNullOrEmpty(path) || !System.IO.File.Exists(path)) return BadRequest($"No such image for page {page}");
var file = await _directoryService.ReadImageAsync(path);
file.Page = page;
file.MangaFileName = mangaFile.FilePath;
file.NeedsSplitting = file.Width > file.Height;
// TODO: Validate if sending page whole (not base64 encoded) fixes Tablet issue
//Response.Headers.Add("Transfer-Encoding", "gzip");
return Ok(file); var content = await _directoryService.ReadFileAsync(path);
var format = Path.GetExtension(path).Replace(".", "");
// Look into HttpContext.Cache so we can utilize a memorystream for Zip entries (want to limit response time by 300ms)
// 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("image2")]
public async Task<ActionResult> GetImage2(int chapterId, int page)
{
// Temp let's iterate the directory each call to get next image
var chapter = await _cacheService.Ensure(chapterId);
[HttpGet("chapter-path")]
public async Task<ActionResult<string>> GetImagePath(int chapterId)
{
var chapter = await _cacheService.Ensure(chapterId);
if (chapter == null) return BadRequest("There was an issue finding image file for reading"); if (chapter == null) return BadRequest("There was an issue finding image file for reading");
var (path, mangaFile) = await _cacheService.GetCachedPagePath(chapter, page); var (path, mangaFile) = await _cacheService.GetCachedPagePath(chapter, 0);
if (string.IsNullOrEmpty(path)) return BadRequest($"No such image for page {page}"); return Ok(mangaFile.FilePath);
var file = await _directoryService.ReadImageAsync(path);
file.Page = page;
file.MangaFileName = mangaFile.FilePath;
file.NeedsSplitting = file.Width > file.Height;
// TODO: Validate if sending page whole (not base64 encoded) fixes Tablet issue
return File(file.Content, "image/jpeg", mangaFile.FilePath);
} }
[HttpGet("get-bookmark")] [HttpGet("get-bookmark")]

View File

@ -18,5 +18,6 @@ namespace API.Extensions
response.Headers.Add("Pagination", JsonSerializer.Serialize(paginationHeader, options)); response.Headers.Add("Pagination", JsonSerializer.Serialize(paginationHeader, options));
response.Headers.Add("Access-Control-Expose-Headers", "Pagination"); response.Headers.Add("Access-Control-Expose-Headers", "Pagination");
} }
} }
} }

View File

@ -13,8 +13,6 @@ namespace API.Interfaces.Services
/// <param name="rootPath">Absolute path of directory to scan.</param> /// <param name="rootPath">Absolute path of directory to scan.</param>
/// <returns>List of folder names</returns> /// <returns>List of folder names</returns>
IEnumerable<string> ListDirectory(string rootPath); IEnumerable<string> ListDirectory(string rootPath);
Task<ImageDto> ReadImageAsync(string imagePath);
/// <summary> /// <summary>
/// Gets files in a directory. If searchPatternExpression is passed, will match the regex against for filtering. /// Gets files in a directory. If searchPatternExpression is passed, will match the regex against for filtering.
/// </summary> /// </summary>

View File

@ -145,26 +145,6 @@ namespace API.Services
return dirs; return dirs;
} }
public async Task<ImageDto> ReadImageAsync(string imagePath)
{
if (!File.Exists(imagePath))
{
_logger.LogError("Image does not exist on disk");
return null;
}
using var image = Image.NewFromFile(imagePath);
return new ImageDto
{
Content = await ReadFileAsync(imagePath),
Filename = Path.GetFileNameWithoutExtension(imagePath),
FullPath = Path.GetFullPath(imagePath),
Width = image.Width,
Height = image.Height,
Format = image.Format,
};
}
public async Task<byte[]> ReadFileAsync(string path) public async Task<byte[]> ReadFileAsync(string path)
{ {

View File

@ -1,9 +1,12 @@
using System.IO.Compression;
using System.Linq;
using API.Extensions; using API.Extensions;
using API.Middleware; using API.Middleware;
using Hangfire; using Hangfire;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpOverrides; using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.AspNetCore.ResponseCompression;
using Microsoft.AspNetCore.StaticFiles; using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
@ -37,8 +40,20 @@ namespace API
{ {
c.SwaggerDoc("v1", new OpenApiInfo { Title = "API", Version = "v1" }); c.SwaggerDoc("v1", new OpenApiInfo { Title = "API", Version = "v1" });
}); });
// This doesn't seem to work.
// services.AddResponseCompression(options =>
// {
// options.Providers.Add<BrotliCompressionProvider>();
// options.MimeTypes =
// ResponseCompressionDefaults.MimeTypes.Concat(
// new[] { "image/jpeg", "image/jpg" });
// });
// services.Configure<BrotliCompressionProviderOptions>(options =>
// {
// options.Level = CompressionLevel.Fastest;
// });
} }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@ -62,6 +77,8 @@ namespace API
{ {
app.UseCors(policy => policy.AllowAnyHeader().AllowAnyMethod().WithOrigins("http://localhost:4200")); app.UseCors(policy => policy.AllowAnyHeader().AllowAnyMethod().WithOrigins("http://localhost:4200"));
} }
//app.UseResponseCaching();
app.UseAuthentication(); app.UseAuthentication();