mirror of
https://github.com/Kareadita/Kavita.git
synced 2025-05-24 00:52:23 -04:00
* Navigate users to library page instead of home to prevent history block. * Cleaned up the Contributing to describe new code structure * Fixed a critical bug for how we find files for a chapter download (use ChapterId for lookup, not MangaFile.Id). Refactored how downloading works on the UI side to use the backend's filename whenever possible, else provide a custom name (and use backend's extension) for bundled downloads. * Fixed a bug where scroll intersection wasn't working on books without a table of content, even though it should have. * If user is using a direct url and hits an authentication guard, cache the url, allow authentication, then redirect them to said url * Added a transaction for bookmarking due to a rare case (in dev machines) where bookmark progress can duplicate * Re-enabled webtoon preference in reader settings. Refactored gotopage into it's own, dedicated handler to simplify logic. * Moved the prefetching code to occur whenever the page number within infinite scroller changes. This results in an easier to understand functioning. * Fixed isElementVisible() which was not properly calculating element visibility * GoToPage going forwards is working as expected, going backwards is completly broken * After performing a gotopage, make sure we update the scrolling direction based on the delta. * Removed some stuff thats not used, split the prefetching code up into separate functions to prepare for a rewrite. * Reworked prefetching to ensure we have a buffer of pages around ourselves. It is not fully tested, but working much better than previous implementation. Will be enhanced with DOM Pruning. * Cleaned up some old cruft from the backend code * Cleaned up the webtoon page change handler to use setPageNum, which will handle the correct prefetching of next/prev chapter * More cleanup around the codebase * Refactored the code to use a map to keep track of what is loaded or not, which works better than max/min in cases where you jump to a page that doesn't have anything preloaded and loads images out of order * Fixed a bad placement of code for when you are unauthenticated, the code will now redirect to the original location you requested before you had to login. * Some cleanup. Fixed the scrolling issue with prev page, spec seems to not work on intersection observer. using 0.01 instead of 0.0.
136 lines
5.2 KiB
C#
136 lines
5.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using API.Entities;
|
|
using API.Extensions;
|
|
using API.Interfaces;
|
|
using API.Interfaces.Services;
|
|
using API.Services;
|
|
using Kavita.Common;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.StaticFiles;
|
|
|
|
namespace API.Controllers
|
|
{
|
|
[Authorize(Policy = "RequireDownloadRole")]
|
|
public class DownloadController : BaseApiController
|
|
{
|
|
private readonly IUnitOfWork _unitOfWork;
|
|
private readonly IArchiveService _archiveService;
|
|
private readonly IDirectoryService _directoryService;
|
|
|
|
public DownloadController(IUnitOfWork unitOfWork, IArchiveService archiveService, IDirectoryService directoryService)
|
|
{
|
|
_unitOfWork = unitOfWork;
|
|
_archiveService = archiveService;
|
|
_directoryService = directoryService;
|
|
}
|
|
|
|
[HttpGet("volume-size")]
|
|
public async Task<ActionResult<long>> GetVolumeSize(int volumeId)
|
|
{
|
|
var files = await _unitOfWork.VolumeRepository.GetFilesForVolume(volumeId);
|
|
return Ok(DirectoryService.GetTotalSize(files.Select(c => c.FilePath)));
|
|
}
|
|
|
|
[HttpGet("chapter-size")]
|
|
public async Task<ActionResult<long>> GetChapterSize(int chapterId)
|
|
{
|
|
var files = await _unitOfWork.VolumeRepository.GetFilesForChapter(chapterId);
|
|
return Ok(DirectoryService.GetTotalSize(files.Select(c => c.FilePath)));
|
|
}
|
|
|
|
[HttpGet("series-size")]
|
|
public async Task<ActionResult<long>> GetSeriesSize(int seriesId)
|
|
{
|
|
var files = await _unitOfWork.SeriesRepository.GetFilesForSeries(seriesId);
|
|
return Ok(DirectoryService.GetTotalSize(files.Select(c => c.FilePath)));
|
|
}
|
|
|
|
[HttpGet("volume")]
|
|
public async Task<ActionResult> DownloadVolume(int volumeId)
|
|
{
|
|
var files = await _unitOfWork.VolumeRepository.GetFilesForVolume(volumeId);
|
|
try
|
|
{
|
|
if (files.Count == 1)
|
|
{
|
|
return await GetFirstFileDownload(files);
|
|
}
|
|
var (fileBytes, zipPath) = await _archiveService.CreateZipForDownload(files.Select(c => c.FilePath),
|
|
$"download_{User.GetUsername()}_v{volumeId}");
|
|
return File(fileBytes, "application/zip", Path.GetFileNameWithoutExtension(zipPath) + ".zip");
|
|
}
|
|
catch (KavitaException ex)
|
|
{
|
|
return BadRequest(ex.Message);
|
|
}
|
|
}
|
|
|
|
private async Task<ActionResult> GetFirstFileDownload(IEnumerable<MangaFile> files)
|
|
{
|
|
var firstFile = files.Select(c => c.FilePath).First();
|
|
var fileProvider = new FileExtensionContentTypeProvider();
|
|
// Figures out what the content type should be based on the file name.
|
|
if (!fileProvider.TryGetContentType(firstFile, out var contentType))
|
|
{
|
|
contentType = Path.GetExtension(firstFile).ToLowerInvariant() switch
|
|
{
|
|
".cbz" => "application/zip",
|
|
".cbr" => "application/vnd.rar",
|
|
".cb7" => "application/x-compressed",
|
|
".epub" => "application/epub+zip",
|
|
".7z" => "application/x-7z-compressed",
|
|
".7zip" => "application/x-7z-compressed",
|
|
_ => contentType
|
|
};
|
|
}
|
|
|
|
return File(await _directoryService.ReadFileAsync(firstFile), contentType, Path.GetFileName(firstFile));
|
|
}
|
|
|
|
[HttpGet("chapter")]
|
|
public async Task<ActionResult> DownloadChapter(int chapterId)
|
|
{
|
|
var files = await _unitOfWork.VolumeRepository.GetFilesForChapter(chapterId);
|
|
try
|
|
{
|
|
if (files.Count == 1)
|
|
{
|
|
return await GetFirstFileDownload(files);
|
|
}
|
|
var (fileBytes, zipPath) = await _archiveService.CreateZipForDownload(files.Select(c => c.FilePath),
|
|
$"download_{User.GetUsername()}_c{chapterId}");
|
|
return File(fileBytes, "application/zip", Path.GetFileNameWithoutExtension(zipPath) + ".zip");
|
|
}
|
|
catch (KavitaException ex)
|
|
{
|
|
return BadRequest(ex.Message);
|
|
}
|
|
}
|
|
|
|
[HttpGet("series")]
|
|
public async Task<ActionResult> DownloadSeries(int seriesId)
|
|
{
|
|
var files = await _unitOfWork.SeriesRepository.GetFilesForSeries(seriesId);
|
|
try
|
|
{
|
|
if (files.Count == 1)
|
|
{
|
|
return await GetFirstFileDownload(files);
|
|
}
|
|
var (fileBytes, zipPath) = await _archiveService.CreateZipForDownload(files.Select(c => c.FilePath),
|
|
$"download_{User.GetUsername()}_s{seriesId}");
|
|
return File(fileBytes, "application/zip", Path.GetFileNameWithoutExtension(zipPath) + ".zip");
|
|
}
|
|
catch (KavitaException ex)
|
|
{
|
|
return BadRequest(ex.Message);
|
|
}
|
|
}
|
|
}
|
|
}
|