Skip to content

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.

Fixed a bug in CleanupBookmarks where the Except was deleting all files because the path separators didn't match. (#932)

Joseph Milazzo7eaa0248c3b1
diff --git a/API.Tests/Services/CleanupServiceTests.cs b/API.Tests/Services/CleanupServiceTests.cs
index b6e4f9454..419dd4126 100644
--- a/API.Tests/Services/CleanupServiceTests.cs
+++ b/API.Tests/Services/CleanupServiceTests.cs
@@ -34,6 +34,7 @@ public class CleanupServiceTests
private const string CacheDirectory = "C:/kavita/config/cache/";
private const string CoverImageDirectory = "C:/kavita/config/covers/";
private const string BackupDirectory = "C:/kavita/config/backups/";
+ private const string BookmarkDirectory = "C:/kavita/config/bookmarks/";
public CleanupServiceTests()
@@ -75,6 +76,9 @@ public class CleanupServiceTests
setting = await _context.ServerSetting.Where(s => s.Key == ServerSettingKey.BackupDirectory).SingleAsync();
setting.Value = BackupDirectory;
+ setting = await _context.ServerSetting.Where(s => s.Key == ServerSettingKey.BookmarkDirectory).SingleAsync();
+ setting.Value = BookmarkDirectory;
+
_context.ServerSetting.Update(setting);
_context.Library.Add(new Library()
@@ -94,6 +98,8 @@ public class CleanupServiceTests
private async Task ResetDB()
{
_context.Series.RemoveRange(_context.Series.ToList());
+ _context.Users.RemoveRange(_context.Users.ToList());
+ _context.AppUserBookmark.RemoveRange(_context.AppUserBookmark.ToList());
await _context.SaveChangesAsync();
}
@@ -106,6 +112,7 @@ public class CleanupServiceTests
fileSystem.AddDirectory(CacheDirectory);
fileSystem.AddDirectory(CoverImageDirectory);
fileSystem.AddDirectory(BackupDirectory);
+ fileSystem.AddDirectory(BookmarkDirectory);
fileSystem.AddDirectory("C:/data/");
return fileSystem;
@@ -356,4 +363,71 @@ public class CleanupServiceTests
}
#endregion
+
+ #region CleanupBookmarks
+
+ [Fact]
+ public async Task CleanupBookmarks_LeaveAllFiles()
+ {
+ var filesystem = CreateFileSystem();
+ filesystem.AddFile($"{BookmarkDirectory}1/1/1/0001.jpg", new MockFileData(""));
+ filesystem.AddFile($"{BookmarkDirectory}1/1/1/0002.jpg", new MockFileData(""));
+
+ // Delete all Series to reset state
+ await ResetDB();
+
+ _context.Series.Add(new Series()
+ {
+ Name = "Test",
+ Library = new Library() {
+ Name = "Test LIb",
+ Type = LibraryType.Manga,
+ },
+ Volumes = new List<Volume>()
+ {
+ new Volume()
+ {
+ Chapters = new List<Chapter>()
+ {
+ new Chapter()
+ {
+
+ }
+ }
+ }
+ }
+ });
+
+ await _context.SaveChangesAsync();
+
+ _context.AppUser.Add(new AppUser()
+ {
+ Bookmarks = new List<AppUserBookmark>()
+ {
+ new AppUserBookmark()
+ {
+ AppUserId = 1,
+ ChapterId = 1,
+ Page = 1,
+ FileName = "1/1/1/0001.jpg",
+ SeriesId = 1,
+ VolumeId = 1
+ }
+ }
+ });
+
+ await _context.SaveChangesAsync();
+
+
+ var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), filesystem);
+ var cleanupService = new CleanupService(_logger, _unitOfWork, _messageHub,
+ ds);
+
+ await cleanupService.CleanupBookmarks();
+
+ Assert.Equal(1, ds.GetFiles(BookmarkDirectory, searchOption:SearchOption.AllDirectories).Count());
+
+ }
+
+ #endregion
}
diff --git a/API/Services/Tasks/CleanupService.cs b/API/Services/Tasks/CleanupService.cs
index 293dddd5e..be5066862 100644
--- a/API/Services/Tasks/CleanupService.cs
+++ b/API/Services/Tasks/CleanupService.cs
@@ -174,16 +174,17 @@ namespace API.Services.Tasks
/// </summary>
public async Task CleanupBookmarks()
{
- // Search all files in bookmarks/
- // except bookmark files and delete those
+ // Search all files in bookmarks/ except bookmark files and delete those
var bookmarkDirectory =
(await _unitOfWork.SettingsRepository.GetSettingAsync(ServerSettingKey.BookmarkDirectory)).Value;
- var allBookmarkFiles = _directoryService.GetFiles(bookmarkDirectory, searchOption: SearchOption.AllDirectories);
+ var allBookmarkFiles = _directoryService.GetFiles(bookmarkDirectory, searchOption: SearchOption.AllDirectories).Select(f => _directoryService.FileSystem.Path.GetFullPath(f));
var bookmarks = (await _unitOfWork.UserRepository.GetAllBookmarksAsync())
- .Select(b => _directoryService.FileSystem.Path.Join(bookmarkDirectory,
- b.FileName));
+ .Select(b => _directoryService.FileSystem.Path.GetFullPath(_directoryService.FileSystem.Path.Join(bookmarkDirectory,
+ b.FileName)));
- var filesToDelete = allBookmarkFiles.Except(bookmarks);
+
+ var filesToDelete = allBookmarkFiles.ToList().Except(bookmarks).ToList();
+ _logger.LogDebug("[Bookmarks] Bookmark cleanup wants to delete {Count} files", filesToDelete.Count());
_directoryService.DeleteFiles(filesToDelete);