Merge pull request #75 from Kareadita/feature/match

Backend changes for new manga reader scheme + Compression
This commit is contained in:
Joseph Milazzo 2021-03-12 10:47:59 -06:00 committed by GitHub
commit fea1f3d152
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 109 additions and 55 deletions

View File

@ -30,44 +30,34 @@ namespace API.Controllers
}
[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);
if (chapter == null) return BadRequest("There was an issue finding image file for reading");
var (path, mangaFile) = await _cacheService.GetCachedPagePath(chapter, page);
if (string.IsNullOrEmpty(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");
if (string.IsNullOrEmpty(path) || !System.IO.File.Exists(path)) return BadRequest($"No such image for page {page}");
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");
var (path, mangaFile) = await _cacheService.GetCachedPagePath(chapter, page);
if (string.IsNullOrEmpty(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
return File(file.Content, "image/jpeg", mangaFile.FilePath);
var (path, mangaFile) = await _cacheService.GetCachedPagePath(chapter, 0);
return Ok(mangaFile.FilePath);
}
[HttpGet("get-bookmark")]
@ -162,6 +152,46 @@ namespace API.Controllers
return BadRequest("There was an issue saving progress");
}
[HttpPost("mark-volume-read")]
public async Task<ActionResult> MarkVolumeAsRead(MarkVolumeReadDto markVolumeReadDto)
{
var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername());
_logger.LogDebug("Saving {UserName} progress for Volume {VolumeID} to read", user.UserName, markVolumeReadDto.VolumeId);
var chapters = await _unitOfWork.VolumeRepository.GetChaptersAsync(markVolumeReadDto.VolumeId);
foreach (var chapter in chapters)
{
user.Progresses ??= new List<AppUserProgress>();
var userProgress = user.Progresses.SingleOrDefault(x => x.ChapterId == chapter.Id && x.AppUserId == user.Id);
if (userProgress == null)
{
user.Progresses.Add(new AppUserProgress
{
PagesRead = chapter.Pages,
VolumeId = markVolumeReadDto.VolumeId,
SeriesId = markVolumeReadDto.SeriesId,
ChapterId = chapter.Id
});
}
else
{
userProgress.PagesRead = chapter.Pages;
userProgress.SeriesId = markVolumeReadDto.SeriesId;
userProgress.VolumeId = markVolumeReadDto.VolumeId;
}
}
_unitOfWork.UserRepository.Update(user);
if (await _unitOfWork.Complete())
{
return Ok();
}
return BadRequest("Could not save progress");
}
[HttpPost("bookmark")]
public async Task<ActionResult> Bookmark(BookmarkDto bookmarkDto)
{

View File

@ -0,0 +1,8 @@
namespace API.DTOs
{
public class MarkVolumeReadDto
{
public int SeriesId { get; init; }
public int VolumeId { get; init; }
}
}

View File

@ -35,9 +35,22 @@ namespace API.Data
{
return await _context.Chapter
.Include(c => c.Files)
.AsNoTracking()
.SingleOrDefaultAsync(c => c.Id == chapterId);
}
/// <summary>
/// Returns Chapters for a volume id.
/// </summary>
/// <param name="volumeId"></param>
/// <returns></returns>
public async Task<IList<Chapter>> GetChaptersAsync(int volumeId)
{
return await _context.Chapter
.Where(c => c.VolumeId == volumeId)
.ToListAsync();
}
public async Task<ChapterDto> GetChapterDtoAsync(int chapterId)
{

View File

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

View File

@ -11,5 +11,6 @@ namespace API.Interfaces
Task<Chapter> GetChapterAsync(int chapterId);
Task<ChapterDto> GetChapterDtoAsync(int chapterId);
Task<IList<MangaFile>> GetFilesForChapter(int chapterId);
Task<IList<Chapter>> GetChaptersAsync(int volumeId);
}
}

View File

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

View File

@ -145,26 +145,6 @@ namespace API.Services
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)
{

View File

@ -78,6 +78,8 @@ namespace API.Services
{
_logger.LogInformation("Enqueuing library scan for: {LibraryId}", libraryId);
BackgroundJob.Enqueue(() => _scannerService.ScanLibrary(libraryId, forceUpdate));
BackgroundJob.Enqueue(() => _cleanupService.Cleanup()); // When we do a scan, force cache to re-unpack in case page numbers change
}
public void CleanupChapters(int[] chapterIds)

View File

@ -1,9 +1,12 @@
using System.IO.Compression;
using System.Linq;
using API.Extensions;
using API.Middleware;
using Hangfire;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.AspNetCore.ResponseCompression;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
@ -37,8 +40,22 @@ namespace API
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "API", Version = "v1" });
});
// This doesn't seem to work.
services.AddResponseCompression(options =>
{
options.Providers.Add<BrotliCompressionProvider>();
options.Providers.Add<GzipCompressionProvider>();
options.MimeTypes =
ResponseCompressionDefaults.MimeTypes.Concat(
new[] { "image/jpeg", "image/jpg" });
options.EnableForHttps = true;
});
services.Configure<BrotliCompressionProviderOptions>(options =>
{
options.Level = CompressionLevel.Fastest;
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@ -52,6 +69,7 @@ namespace API
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "API v1"));
app.UseHangfireDashboard();
}
app.UseResponseCompression();
app.UseForwardedHeaders();
@ -62,13 +80,15 @@ namespace API
{
app.UseCors(policy => policy.AllowAnyHeader().AllowAnyMethod().WithOrigins("http://localhost:4200"));
}
//app.UseResponseCaching();
app.UseAuthentication();
app.UseAuthorization();
app.UseDefaultFiles();
app.UseStaticFiles(new StaticFileOptions
{
ContentTypeProvider = new FileExtensionContentTypeProvider()

View File

@ -8,7 +8,8 @@
"Default": "Debug",
"Microsoft": "Information",
"Microsoft.Hosting.Lifetime": "Error",
"Hangfire": "Information"
"Hangfire": "Information",
"Microsoft.AspNetCore.Hosting.Internal.WebHost": "Information"
},
"File": {
"Path": "kavita.log",