Some code cleanup

This commit is contained in:
Joseph Milazzo 2021-03-22 16:38:51 -05:00
parent 585e965a85
commit d73bd22db2
34 changed files with 814 additions and 383 deletions

View file

@ -47,9 +47,9 @@ namespace API.Services
{
try
{
_logger.LogDebug("Archive Type: {ArchiveType}", reader.ArchiveType);
_logger.LogDebug("{ArchivePath}'s Type: {ArchiveType}", archivePath, reader.ArchiveType);
}
catch (System.InvalidOperationException ex)
catch (InvalidOperationException ex)
{
_logger.LogError(ex, "Could not parse the archive. Please validate it is not corrupted");
return 0;
@ -86,37 +86,7 @@ namespace API.Services
try
{
if (!IsValidArchive(filepath)) return Array.Empty<byte>();
// if (SharpCompress.Archives.Zip.ZipArchive.IsZipFile(filepath))
// {
// using var archive = SharpCompress.Archives.Zip.ZipArchive.Open(filepath);
// return FindCoverImage(archive.Entries.Where(entry => !entry.IsDirectory && Parser.Parser.IsImage(entry.Key)), createThumbnail);
// }
//
// if (GZipArchive.IsGZipFile(filepath))
// {
// using var archive = GZipArchive.Open(filepath);
// return FindCoverImage(archive.Entries.Where(entry => !entry.IsDirectory && Parser.Parser.IsImage(entry.Key)), createThumbnail);
// }
//
// if (RarArchive.IsRarFile(filepath))
// {
// using var archive = RarArchive.Open(filepath);
// return FindCoverImage(archive.Entries.Where(entry => !entry.IsDirectory && Parser.Parser.IsImage(entry.Key)), createThumbnail);
// }
//
// if (SevenZipArchive.IsSevenZipFile(filepath))
// {
// using var archive = SevenZipArchive.Open(filepath);
// return FindCoverImage(archive.Entries.Where(entry => !entry.IsDirectory && Parser.Parser.IsImage(entry.Key)), createThumbnail);
// }
//
// if (TarArchive.IsTarFile(filepath))
// {
// using var archive = TarArchive.Open(filepath);
// return FindCoverImage(archive.Entries.Where(entry => !entry.IsDirectory && Parser.Parser.IsImage(entry.Key)), createThumbnail);
// }
using var archive = ArchiveFactory.Open(filepath);
return FindCoverImage(archive.Entries.Where(entry => !entry.IsDirectory && Parser.Parser.IsImage(entry.Key)), createThumbnail);
}
@ -293,40 +263,11 @@ namespace API.Services
{
if (!File.Exists(archivePath)) return;
if (new DirectoryInfo(extractPath).Exists) return;
var sw = Stopwatch.StartNew();
// if (SharpCompress.Archives.Zip.ZipArchive.IsZipFile(archivePath))
// {
//
// //using var archive = SharpCompress.Archives.Zip.ZipArchive.Open(archivePath);
// using var archive = ArchiveFactory.Open(archivePath);
// ExtractArchiveEntities(archive.Entries.Where(entry => !entry.IsDirectory && Parser.Parser.IsImage(entry.Key)), extractPath);
// }
// else if (GZipArchive.IsGZipFile(archivePath))
// {
// using var archive = GZipArchive.Open(archivePath);
// ExtractArchiveEntities(archive.Entries.Where(entry => !entry.IsDirectory && Parser.Parser.IsImage(entry.Key)), extractPath);
// } else if (RarArchive.IsRarFile(archivePath))
// {
// using var archive = RarArchive.Open(archivePath);
// ExtractArchiveEntities(archive.Entries.Where(entry => !entry.IsDirectory && Parser.Parser.IsImage(entry.Key)), extractPath);
// } else if (SevenZipArchive.IsSevenZipFile(archivePath))
// {
// using var archive = SevenZipArchive.Open(archivePath);
// ExtractArchiveEntities(archive.Entries.Where(entry => !entry.IsDirectory && Parser.Parser.IsImage(entry.Key)), extractPath);
// }
// else if (TarArchive.IsTarFile(archivePath))
// {
// using var archive = TarArchive.Open(archivePath);
// ExtractArchiveEntities(archive.Entries.Where(entry => !entry.IsDirectory && Parser.Parser.IsImage(entry.Key)), extractPath);
// }
// else
// {
// _logger.LogError("Could not parse archive file");
// return;
// }
using var archive = ArchiveFactory.Open(archivePath);
ExtractArchiveEntities(archive.Entries.Where(entry => !entry.IsDirectory && Parser.Parser.IsImage(entry.Key)), extractPath);
_logger.LogDebug("[Fallback] Extracted archive to {ExtractPath} in {ElapsedMilliseconds} milliseconds", extractPath, sw.ElapsedMilliseconds);
}
}

View file

@ -106,7 +106,7 @@ namespace API.Services
var chapterFiles = chapter.Files ?? await _unitOfWork.VolumeRepository.GetFilesForChapter(chapter.Id);
foreach (var mangaFile in chapterFiles)
{
if (page <= (mangaFile.NumberOfPages + pagesSoFar))
if (page <= (mangaFile.Pages + pagesSoFar))
{
var path = GetCachePath(chapter.Id);
var files = _directoryService.GetFilesWithExtension(path, Parser.Parser.ImageFileExtensions);
@ -121,7 +121,7 @@ namespace API.Services
return (files.ElementAt(page - pagesSoFar), mangaFile);
}
pagesSoFar += mangaFile.NumberOfPages;
pagesSoFar += mangaFile.Pages;
}
return ("", null);

View file

@ -84,13 +84,17 @@ namespace API.Services
{
Directory.CreateDirectory(directoryPath);
}
catch (Exception ex)
catch (Exception)
{
return false;
}
return true;
}
/// <summary>
/// Deletes all files within the directory, then the directory itself.
/// </summary>
/// <param name="directoryPath"></param>
public void ClearAndDeleteDirectory(string directoryPath)
{
DirectoryInfo di = new DirectoryInfo(directoryPath);
@ -100,6 +104,11 @@ namespace API.Services
di.Delete(true);
}
/// <summary>
/// Deletes all files within the directory.
/// </summary>
/// <param name="directoryPath"></param>
/// <returns></returns>
public void ClearDirectory(string directoryPath)
{
var di = new DirectoryInfo(directoryPath);
@ -239,7 +248,8 @@ namespace API.Services
return ++localCount;
},
(c) => {
Interlocked.Add(ref fileCount, c);
// ReSharper disable once AccessToModifiedClosure
Interlocked.Add(ref fileCount, c);
});
}
}

View file

@ -1,5 +1,4 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using API.Entities.Enums;
using API.Helpers.Converters;
@ -21,17 +20,13 @@ namespace API.Services
private readonly IMetadataService _metadataService;
private readonly IBackupService _backupService;
private readonly ICleanupService _cleanupService;
private readonly IDirectoryService _directoryService;
public static BackgroundJobServer Client => new BackgroundJobServer(new BackgroundJobServerOptions()
{
WorkerCount = 1
});
public static BackgroundJobServer Client => new BackgroundJobServer();
public TaskScheduler(ICacheService cacheService, ILogger<TaskScheduler> logger, IScannerService scannerService,
IUnitOfWork unitOfWork, IMetadataService metadataService, IBackupService backupService, ICleanupService cleanupService,
IDirectoryService directoryService, IWebHostEnvironment env)
IWebHostEnvironment env)
{
_cacheService = cacheService;
_logger = logger;
@ -40,7 +35,6 @@ namespace API.Services
_metadataService = metadataService;
_backupService = backupService;
_cleanupService = cleanupService;
_directoryService = directoryService;
if (!env.IsDevelopment())
{
@ -58,9 +52,8 @@ namespace API.Services
public void ScheduleTasks()
{
_logger.LogInformation("Scheduling reoccurring tasks");
string setting = null;
setting = Task.Run(() => _unitOfWork.SettingsRepository.GetSettingAsync(ServerSettingKey.TaskScan)).Result.Value;
string setting = Task.Run(() => _unitOfWork.SettingsRepository.GetSettingAsync(ServerSettingKey.TaskScan)).Result.Value;
if (setting != null)
{
_logger.LogDebug("Scheduling Scan Library Task for {Cron}", setting);
@ -87,7 +80,7 @@ namespace API.Services
public void ScanLibrary(int libraryId, bool forceUpdate = false)
{
// TODO: We shouldn't queue up a job if one is already in progress
_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

View file

@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using API.Entities.Enums;
using API.Extensions;
@ -55,7 +54,7 @@ namespace API.Services.Tasks
var files = maxRollingFiles > 0
? _directoryService.GetFiles(Directory.GetCurrentDirectory(), $@"{fi.Name}{multipleFileRegex}\.log")
: new string[] {"kavita.log"};
: new[] {"kavita.log"};
return files;
}

View file

@ -11,14 +11,12 @@ namespace API.Services.Tasks
public class CleanupService : ICleanupService
{
private readonly ICacheService _cacheService;
private readonly IDirectoryService _directoryService;
private readonly ILogger<CleanupService> _logger;
private readonly IBackupService _backupService;
public CleanupService(ICacheService cacheService, IDirectoryService directoryService, ILogger<CleanupService> logger, IBackupService backupService)
public CleanupService(ICacheService cacheService, ILogger<CleanupService> logger, IBackupService backupService)
{
_cacheService = cacheService;
_directoryService = directoryService;
_logger = logger;
_backupService = backupService;
}

View file

@ -284,7 +284,7 @@ namespace API.Services.Tasks
AddOrUpdateFileForChapter(chapter, info);
chapter.Number = Parser.Parser.MinimumNumberFromRange(info.Chapters) + "";
chapter.Range = info.Chapters;
chapter.Pages = chapter.Files.Sum(f => f.NumberOfPages);
chapter.Pages = chapter.Files.Sum(f => f.Pages);
_metadataService.UpdateMetadata(chapter, _forceUpdate);
}
@ -350,7 +350,7 @@ namespace API.Services.Tasks
{
FilePath = info.FullFilePath,
Format = info.Format,
NumberOfPages = _archiveService.GetNumberOfPagesFromArchive(info.FullFilePath)
Pages = _archiveService.GetNumberOfPagesFromArchive(info.FullFilePath)
};
}
@ -361,7 +361,7 @@ namespace API.Services.Tasks
if (existingFile != null)
{
existingFile.Format = info.Format;
existingFile.NumberOfPages = _archiveService.GetNumberOfPagesFromArchive(info.FullFilePath);
existingFile.Pages = _archiveService.GetNumberOfPagesFromArchive(info.FullFilePath);
}
else
{