Lots of work for chapters. This code will be refactored in a chapter rewrite.
This commit is contained in:
parent
f430595d11
commit
a42e54a078
11 changed files with 99 additions and 13 deletions
18
API/Comparators/ChapterSortComparer.cs
Normal file
18
API/Comparators/ChapterSortComparer.cs
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
using System.Collections.Generic;
|
||||
|
||||
namespace API.Comparators
|
||||
{
|
||||
public class ChapterSortComparer : IComparer<int>
|
||||
{
|
||||
public int Compare(int x, int y)
|
||||
{
|
||||
if (x == 0 && y == 0) return 0;
|
||||
// if x is 0, it comes second
|
||||
if (x == 0) return 1;
|
||||
// if y is 0, it comes second
|
||||
if (y == 0) return -1;
|
||||
|
||||
return x.CompareTo(y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -6,6 +6,7 @@ using API.DTOs;
|
|||
using API.Entities;
|
||||
using API.Extensions;
|
||||
using API.Interfaces;
|
||||
using AutoMapper;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
|
|
@ -17,14 +18,16 @@ namespace API.Controllers
|
|||
private readonly ICacheService _cacheService;
|
||||
private readonly ILogger<ReaderController> _logger;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public ReaderController(IDirectoryService directoryService, ICacheService cacheService,
|
||||
ILogger<ReaderController> logger, IUnitOfWork unitOfWork)
|
||||
ILogger<ReaderController> logger, IUnitOfWork unitOfWork, IMapper mapper)
|
||||
{
|
||||
_directoryService = directoryService;
|
||||
_cacheService = cacheService;
|
||||
_logger = logger;
|
||||
_unitOfWork = unitOfWork;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
[HttpGet("image")]
|
||||
|
|
@ -33,9 +36,12 @@ namespace API.Controllers
|
|||
// 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 (path, mangaFile) = _cacheService.GetCachedPagePath(volume, page);
|
||||
if (string.IsNullOrEmpty(path)) return BadRequest($"No such image for page {page}");
|
||||
var file = await _directoryService.ReadImageAsync(path);
|
||||
file.Page = page;
|
||||
file.Chapter = mangaFile.Chapter;
|
||||
file.MangaFileName = mangaFile.FilePath;
|
||||
|
||||
return Ok(file);
|
||||
}
|
||||
|
|
@ -57,6 +63,8 @@ namespace API.Controllers
|
|||
{
|
||||
var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername());
|
||||
_logger.LogInformation($"Saving {user.UserName} progress for {bookmarkDto.VolumeId} to page {bookmarkDto.PageNum}");
|
||||
|
||||
// TODO: Don't let user bookmark past total pages.
|
||||
|
||||
user.Progresses ??= new List<AppUserProgress>();
|
||||
var userProgress = user.Progresses.SingleOrDefault(x => x.VolumeId == bookmarkDto.VolumeId && x.AppUserId == user.Id);
|
||||
|
|
|
|||
|
|
@ -9,5 +9,7 @@
|
|||
public int Height { get; init; }
|
||||
public string Format { get; init; }
|
||||
public byte[] Content { get; init; }
|
||||
public int Chapter { get; set; }
|
||||
public string MangaFileName { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -27,7 +27,7 @@ namespace API.Entities
|
|||
public DateTime LastModified { get; set; }
|
||||
public byte[] CoverImage { get; set; }
|
||||
/// <summary>
|
||||
/// Sum of all Volume pages
|
||||
/// Sum of all Volume page counts
|
||||
/// </summary>
|
||||
public int Pages { get; set; }
|
||||
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ namespace API.Interfaces
|
|||
/// <param name="volume"></param>
|
||||
/// <param name="page">Page number to look for</param>
|
||||
/// <returns></returns>
|
||||
string GetCachedPagePath(Volume volume, int page);
|
||||
(string path, MangaFile file) GetCachedPagePath(Volume volume, int page);
|
||||
|
||||
bool CacheDirectoryIsAccessible();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -169,7 +169,7 @@ namespace API.Parser
|
|||
/// <param name="filePath"></param>
|
||||
/// <param name="rootPath">Root folder</param>
|
||||
/// <returns><see cref="ParserInfo"/> or null if Series was empty</returns>
|
||||
public static ParserInfo? Parse(string filePath, string rootPath)
|
||||
public static ParserInfo Parse(string filePath, string rootPath)
|
||||
{
|
||||
var fileName = Path.GetFileName(filePath);
|
||||
var directoryName = (new FileInfo(filePath)).Directory?.Name;
|
||||
|
|
|
|||
|
|
@ -108,10 +108,12 @@ namespace API.Services
|
|||
|
||||
public IEnumerable<MangaFile> GetOrderedChapters(ICollection<MangaFile> files)
|
||||
{
|
||||
return files.OrderBy(f => f.Chapter).Where(f => f.Chapter > 0 || f.Volume.Number != 0);
|
||||
// BUG: This causes a problem because total pages on a volume assumes "specials" to be there
|
||||
//return files.OrderBy(f => f.Chapter).Where(f => f.Chapter > 0 || f.Volume.Number != 0);
|
||||
return files.OrderBy(f => f.Chapter, new ChapterSortComparer());
|
||||
}
|
||||
|
||||
public string GetCachedPagePath(Volume volume, int page)
|
||||
public (string path, MangaFile file) GetCachedPagePath(Volume volume, int page)
|
||||
{
|
||||
// Calculate what chapter the page belongs to
|
||||
var pagesSoFar = 0;
|
||||
|
|
@ -125,12 +127,12 @@ namespace API.Services
|
|||
var files = _directoryService.GetFiles(path);
|
||||
Array.Sort(files, _numericComparer);
|
||||
|
||||
return files.ElementAt(page - pagesSoFar);
|
||||
return (files.ElementAt(page - pagesSoFar), mangaFile);
|
||||
}
|
||||
|
||||
pagesSoFar += mangaFile.NumberOfPages;
|
||||
}
|
||||
return "";
|
||||
return ("", null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -61,7 +61,7 @@ namespace API.Services
|
|||
FullPath = Path.GetFullPath(imagePath),
|
||||
Width = image.Width,
|
||||
Height = image.Height,
|
||||
Format = image.Format
|
||||
Format = image.Format,
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue