Refactored Cache Cleanup code.

This commit is contained in:
Joseph Milazzo 2021-02-02 09:38:10 -06:00
parent 53e85317f9
commit 0f82b45b9d
7 changed files with 44 additions and 31 deletions

View File

@ -36,13 +36,14 @@ namespace API.Controllers
public async Task<ActionResult<bool>> DeleteSeries(int seriesId) public async Task<ActionResult<bool>> DeleteSeries(int seriesId)
{ {
var username = User.GetUsername(); var username = User.GetUsername();
var volumes = (await _unitOfWork.SeriesRepository.GetVolumesForSeriesAsync(new []{seriesId})).Select(x => x.Id).ToArray(); //var volumes = (await _unitOfWork.SeriesRepository.GetVolumesForSeriesAsync(new []{seriesId})).Select(x => x.Id).ToArray();
var chapterIds = (await _unitOfWork.SeriesRepository.GetChapterIdsForSeriesAsync(new []{seriesId}));
_logger.LogInformation($"Series {seriesId} is being deleted by {username}."); _logger.LogInformation($"Series {seriesId} is being deleted by {username}.");
var result = await _unitOfWork.SeriesRepository.DeleteSeriesAsync(seriesId); var result = await _unitOfWork.SeriesRepository.DeleteSeriesAsync(seriesId);
if (result) if (result)
{ {
_taskScheduler.CleanupVolumes(volumes); _taskScheduler.CleanupChapters(chapterIds);
} }
return Ok(result); return Ok(result);
} }

View File

@ -175,6 +175,32 @@ namespace API.Data
.SingleOrDefaultAsync(); .SingleOrDefaultAsync();
} }
public async Task<int[]> GetChapterIdsForSeriesAsync(int[] seriesIds)
{
var series = await _context.Series
.Where(s => seriesIds.Contains(s.Id))
.Include(s => s.Volumes)
.ThenInclude(v => v.Chapters)
.ToListAsync();
// TODO: refactor this
IList<int> chapterIds = new List<int>();
foreach (var s in series)
{
foreach (var v in s.Volumes)
{
foreach (var c in v.Chapters)
{
chapterIds.Add(c.Id);
}
}
}
return chapterIds.ToArray();
//return series.Select(s => s.Volumes).Select(v => v.Select(v => v.Chapters)).Select(c => c.Id);
}
private async Task AddSeriesModifiers(int userId, List<SeriesDto> series) private async Task AddSeriesModifiers(int userId, List<SeriesDto> series)
{ {
var userProgress = await _context.AppUserProgresses var userProgress = await _context.AppUserProgresses

View File

@ -21,8 +21,8 @@ namespace API.Interfaces
/// <summary> /// <summary>
/// Clears cache directory of all volumes. This can be invoked from deleting a library or a series. /// Clears cache directory of all volumes. This can be invoked from deleting a library or a series.
/// </summary> /// </summary>
/// <param name="volumeIds">Volumes that belong to that library. Assume the library might have been deleted before this invocation.</param> /// <param name="chapterIds">Volumes that belong to that library. Assume the library might have been deleted before this invocation.</param>
void CleanupVolumes(int[] volumeIds); void CleanupChapters(int[] chapterIds);
/// <summary> /// <summary>

View File

@ -26,5 +26,6 @@ namespace API.Interfaces
Task<Series> GetSeriesByIdAsync(int seriesId); Task<Series> GetSeriesByIdAsync(int seriesId);
//Task<MangaFileDto> GetVolumeMangaFileDtos(int volumeId); //Task<MangaFileDto> GetVolumeMangaFileDtos(int volumeId);
Task<int[]> GetChapterIdsForSeriesAsync(int[] seriesIds);
} }
} }

View File

@ -3,7 +3,7 @@
public interface ITaskScheduler public interface ITaskScheduler
{ {
void ScanLibrary(int libraryId, bool forceUpdate = false); void ScanLibrary(int libraryId, bool forceUpdate = false);
void CleanupVolumes(int[] volumeIds); void CleanupChapters(int[] chapterIds);
void ScanSeries(int libraryId, int seriesId); void ScanSeries(int libraryId, int seriesId);
} }
} }

View File

@ -47,7 +47,7 @@ namespace API.Services
foreach (var file in chapter.Files) foreach (var file in chapter.Files)
{ {
var extractPath = GetCachePath(chapterId, file); var extractPath = GetCachePath(chapterId);
_archiveService.ExtractArchive(file.FilePath, extractPath); _archiveService.ExtractArchive(file.FilePath, extractPath);
} }
@ -73,14 +73,14 @@ namespace API.Services
_logger.LogInformation("Cache directory purged."); _logger.LogInformation("Cache directory purged.");
} }
public void CleanupVolumes(int[] volumeIds) public void CleanupChapters(int[] chapterIds)
{ {
// TODO: Fix this code to work with chapters // TODO: Fix this code to work with chapters
_logger.LogInformation($"Running Cache cleanup on Volumes"); _logger.LogInformation($"Running Cache cleanup on Volumes");
foreach (var volume in volumeIds) foreach (var chapter in chapterIds)
{ {
var di = new DirectoryInfo(Path.Join(CacheDirectory, volume + "")); var di = new DirectoryInfo(GetCachePath(chapter));
if (di.Exists) if (di.Exists)
{ {
di.Delete(true); di.Delete(true);
@ -90,31 +90,15 @@ namespace API.Services
_logger.LogInformation("Cache directory purged"); _logger.LogInformation("Cache directory purged");
} }
/// <summary> /// <summary>
/// Returns the cache path for a given Chapter. Should be cacheDirectory/{chapterId}/ /// Returns the cache path for a given Chapter. Should be cacheDirectory/{chapterId}/
/// </summary> /// </summary>
/// <param name="chapterId"></param> /// <param name="chapterId"></param>
/// <param name="file"></param>
/// <returns></returns> /// <returns></returns>
public string GetCachePath(int chapterId, MangaFile file) private string GetCachePath(int chapterId)
{ {
var extractPath = Path.GetFullPath(Path.Join(CacheDirectory, $"{chapterId}/")); return Path.GetFullPath(Path.Join(CacheDirectory, $"{chapterId}/"));
// if (file.Chapter != null)
// {
// extractPath = Path.Join(extractPath, chapterId + "");
// }
return extractPath;
}
public IEnumerable<MangaFile> GetOrderedChapters(ICollection<MangaFile> files)
{
// 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;
//return files.OrderBy(f => f.Chapter, new ChapterSortComparer());
} }
public async Task<(string path, MangaFile file)> GetCachedPagePath(Chapter chapter, int page) public async Task<(string path, MangaFile file)> GetCachedPagePath(Chapter chapter, int page)
@ -124,9 +108,10 @@ namespace API.Services
var chapterFiles = chapter.Files ?? await _unitOfWork.VolumeRepository.GetFilesForChapter(chapter.Id); var chapterFiles = chapter.Files ?? await _unitOfWork.VolumeRepository.GetFilesForChapter(chapter.Id);
foreach (var mangaFile in chapterFiles) foreach (var mangaFile in chapterFiles)
{ {
if (page + 1 < (mangaFile.NumberOfPages + pagesSoFar)) if (page < (mangaFile.NumberOfPages + pagesSoFar))
{ {
var path = GetCachePath(chapter.Id, mangaFile); var path = GetCachePath(chapter.Id);
// TODO: GetFiles should only get image files.
var files = _directoryService.GetFiles(path); var files = _directoryService.GetFiles(path);
Array.Sort(files, _numericComparer); Array.Sort(files, _numericComparer);

View File

@ -34,9 +34,9 @@ namespace API.Services
BackgroundJob.Enqueue(() => _scannerService.ScanLibrary(libraryId, forceUpdate)); BackgroundJob.Enqueue(() => _scannerService.ScanLibrary(libraryId, forceUpdate));
} }
public void CleanupVolumes(int[] volumeIds) public void CleanupChapters(int[] chapterIds)
{ {
BackgroundJob.Enqueue(() => _cacheService.CleanupVolumes(volumeIds)); BackgroundJob.Enqueue(() => _cacheService.CleanupChapters(chapterIds));
} }