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
Added more edge case handling for Library backup
Joseph Milazzo0258c43b3767
diff --git a/API/Interfaces/Services/IDirectoryService.cs b/API/Interfaces/Services/IDirectoryService.csindex 19aba03dd..abd83dd36 100644--- a/API/Interfaces/Services/IDirectoryService.cs+++ b/API/Interfaces/Services/IDirectoryService.cs@@ -27,5 +27,7 @@ namespace API.Interfaces.Services/// <param name="directoryPath"></param>/// <returns></returns>bool ExistOrCreate(string directoryPath);++ void ClearAndDeleteDirectory(string directoryPath);}}\ No newline at end of filediff --git a/API/Services/BackupService.cs b/API/Services/BackupService.csindex 7ab168712..17b9cfb94 100644--- a/API/Services/BackupService.cs+++ b/API/Services/BackupService.cs@@ -16,6 +16,7 @@ namespace API.Servicesprivate readonly IUnitOfWork _unitOfWork;private readonly ILogger<BackupService> _logger;private readonly IDirectoryService _directoryService;+ private readonly string _tempDirectory = Path.Join(Directory.GetCurrentDirectory(), "temp");private readonly IList<string> _backupFiles = new List<string>(){@@ -39,6 +40,7 @@ namespace API.Services{_logger.LogInformation("Beginning backup of Database at {BackupTime}", DateTime.Now);var backupDirectory = Task.Run(() => _unitOfWork.SettingsRepository.GetSettingAsync(ServerSettingKey.BackupDirectory)).Result.Value;+_logger.LogDebug("Backing up to {BackupDirectory}", backupDirectory);if (!_directoryService.ExistOrCreate(backupDirectory)){@@ -46,19 +48,36 @@ namespace API.Servicesreturn;}- var fileInfos = _backupFiles.Select(file => new FileInfo(Path.Join(Directory.GetCurrentDirectory(), file))).ToList();+ var dateString = DateTime.Now.ToShortDateString().Replace("/", "_");+ var zipPath = Path.Join(backupDirectory, $"kavita_backup_{dateString}.zip");++ if (File.Exists(zipPath))+ {+ _logger.LogInformation("{ZipFile} already exists, aborting", zipPath);+ return;+ }++ var tempDirectory = Path.Join(_tempDirectory, dateString);+ _directoryService.ExistOrCreate(tempDirectory);+- var zipPath = Path.Join(backupDirectory, $"kavita_backup_{DateTime.Now}.zip");- using (var zipArchive = ZipFile.Open(zipPath, ZipArchiveMode.Create))+ foreach (var file in _backupFiles){- foreach (var fileInfo in fileInfos)- {- zipArchive.CreateEntryFromFile(fileInfo.FullName, fileInfo.Name);- }+ var originalFile = new FileInfo(Path.Join(Directory.GetCurrentDirectory(), file));+ originalFile.CopyTo(Path.Join(tempDirectory, originalFile.Name));}-++ try+ {+ ZipFile.CreateFromDirectory(tempDirectory, zipPath);+ }+ catch (AggregateException ex)+ {+ _logger.LogError(ex, "There was an issue when archiving library backup");+ }++ _directoryService.ClearAndDeleteDirectory(tempDirectory);_logger.LogInformation("Database backup completed");- throw new System.NotImplementedException();}}}\ No newline at end of filediff --git a/API/Services/DirectoryService.cs b/API/Services/DirectoryService.csindex e6410cd1c..9481302bd 100644--- a/API/Services/DirectoryService.cs+++ b/API/Services/DirectoryService.cs@@ -66,6 +66,22 @@ namespace API.Servicesreturn true;}+ public void ClearAndDeleteDirectory(string directoryPath)+ {+ DirectoryInfo di = new DirectoryInfo(directoryPath);++ foreach (var file in di.EnumerateFiles())+ {+ file.Delete();+ }+ foreach (var dir in di.EnumerateDirectories())+ {+ dir.Delete(true);+ }++ di.Delete(true);+ }+public IEnumerable<string> ListDirectory(string rootPath){if (!Directory.Exists(rootPath)) return ImmutableList<string>.Empty;