Kavita is a fast, feature rich, cross platform reading server. Built with the goal of being a full solution for all your reading needs. Setup your own server and share your reading collection with your friends and family.
C# annotationscbzcomicinfocomicscomics-readercross-platformcsharpepubepub-readerfreelinuxmangamedia-servermetadataopdsopds-feedpdfself-hostedubooquitywindows
Refactored Cache Cleanup code.
Joseph Milazzo0f82b45b9dba
diff --git a/API/Controllers/SeriesController.cs b/API/Controllers/SeriesController.csindex 561b4ec20..7a59a4ae8 100644--- a/API/Controllers/SeriesController.cs+++ b/API/Controllers/SeriesController.cs@@ -36,13 +36,14 @@ namespace API.Controllerspublic async Task<ActionResult<bool>> DeleteSeries(int seriesId){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}.");var result = await _unitOfWork.SeriesRepository.DeleteSeriesAsync(seriesId);if (result){- _taskScheduler.CleanupVolumes(volumes);+ _taskScheduler.CleanupChapters(chapterIds);}return Ok(result);}diff --git a/API/Data/SeriesRepository.cs b/API/Data/SeriesRepository.csindex 14d2c0349..b188f232c 100644--- a/API/Data/SeriesRepository.cs+++ b/API/Data/SeriesRepository.cs@@ -175,6 +175,32 @@ namespace API.Data.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){var userProgress = await _context.AppUserProgressesdiff --git a/API/Interfaces/ICacheService.cs b/API/Interfaces/ICacheService.csindex 9d9721b4a..470099859 100644--- a/API/Interfaces/ICacheService.cs+++ b/API/Interfaces/ICacheService.cs@@ -21,8 +21,8 @@ namespace API.Interfaces/// <summary>/// Clears cache directory of all volumes. This can be invoked from deleting a library or a series./// </summary>- /// <param name="volumeIds">Volumes that belong to that library. Assume the library might have been deleted before this invocation.</param>- void CleanupVolumes(int[] volumeIds);+ /// <param name="chapterIds">Volumes that belong to that library. Assume the library might have been deleted before this invocation.</param>+ void CleanupChapters(int[] chapterIds);/// <summary>diff --git a/API/Interfaces/ISeriesRepository.cs b/API/Interfaces/ISeriesRepository.csindex fedf5c10e..86f3c3387 100644--- a/API/Interfaces/ISeriesRepository.cs+++ b/API/Interfaces/ISeriesRepository.cs@@ -26,5 +26,6 @@ namespace API.InterfacesTask<Series> GetSeriesByIdAsync(int seriesId);//Task<MangaFileDto> GetVolumeMangaFileDtos(int volumeId);+ Task<int[]> GetChapterIdsForSeriesAsync(int[] seriesIds);}}\ No newline at end of filediff --git a/API/Interfaces/ITaskScheduler.cs b/API/Interfaces/ITaskScheduler.csindex b19dc9291..f7e13d7a6 100644--- a/API/Interfaces/ITaskScheduler.cs+++ b/API/Interfaces/ITaskScheduler.cs@@ -3,7 +3,7 @@public interface ITaskScheduler{void ScanLibrary(int libraryId, bool forceUpdate = false);- void CleanupVolumes(int[] volumeIds);+ void CleanupChapters(int[] chapterIds);void ScanSeries(int libraryId, int seriesId);}}\ No newline at end of filediff --git a/API/Services/CacheService.cs b/API/Services/CacheService.csindex d59a7f2db..b1e04a83d 100644--- a/API/Services/CacheService.cs+++ b/API/Services/CacheService.cs@@ -47,7 +47,7 @@ namespace API.Servicesforeach (var file in chapter.Files){- var extractPath = GetCachePath(chapterId, file);+ var extractPath = GetCachePath(chapterId);_archiveService.ExtractArchive(file.FilePath, extractPath);}@@ -73,14 +73,14 @@ namespace API.Services_logger.LogInformation("Cache directory purged.");}- public void CleanupVolumes(int[] volumeIds)+ public void CleanupChapters(int[] chapterIds){// TODO: Fix this code to work with chapters_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){di.Delete(true);@@ -90,31 +90,15 @@ namespace API.Services_logger.LogInformation("Cache directory purged");}--/// <summary>/// Returns the cache path for a given Chapter. Should be cacheDirectory/{chapterId}//// </summary>/// <param name="chapterId"></param>- /// <param name="file"></param>/// <returns></returns>- public string GetCachePath(int chapterId, MangaFile file)- {- var extractPath = Path.GetFullPath(Path.Join(CacheDirectory, $"{chapterId}/"));- // if (file.Chapter != null)- // {- // extractPath = Path.Join(extractPath, chapterId + "");- // }- return extractPath;- }-- public IEnumerable<MangaFile> GetOrderedChapters(ICollection<MangaFile> files)+ private string GetCachePath(int chapterId){- // 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());+ return Path.GetFullPath(Path.Join(CacheDirectory, $"{chapterId}/"));}public async Task<(string path, MangaFile file)> GetCachedPagePath(Chapter chapter, int page)@@ -124,9 +108,10 @@ namespace API.Servicesvar chapterFiles = chapter.Files ?? await _unitOfWork.VolumeRepository.GetFilesForChapter(chapter.Id);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);Array.Sort(files, _numericComparer);diff --git a/API/Services/TaskScheduler.cs b/API/Services/TaskScheduler.csindex 2ed039b8d..f888087ea 100644--- a/API/Services/TaskScheduler.cs+++ b/API/Services/TaskScheduler.cs@@ -34,9 +34,9 @@ namespace API.ServicesBackgroundJob.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));}