mirror of
https://github.com/Kareadita/Kavita.git
synced 2025-07-09 03:04:19 -04:00
Bookmark Refactor (#1049)
* Tweaked how the migration to change users with ChangePassword role happens. It will now only run once. * Refactored bookmarks into it's own service with unit tests. Bookmark management happens in real time and we no longer delete bookmarks on a schedule. This means once you bookmark something, even if you delete the entity, the files will remain. * Commented out a test that no longer is needed
This commit is contained in:
parent
9c9a5f92a1
commit
05c35a1cb6
345
API.Tests/Services/BookmarkServiceTests.cs
Normal file
345
API.Tests/Services/BookmarkServiceTests.cs
Normal file
@ -0,0 +1,345 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Data.Common;
|
||||||
|
using System.IO;
|
||||||
|
using System.IO.Abstractions.TestingHelpers;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using API.Data;
|
||||||
|
using API.Data.Repositories;
|
||||||
|
using API.DTOs.Reader;
|
||||||
|
using API.Entities;
|
||||||
|
using API.Entities.Enums;
|
||||||
|
using API.Services;
|
||||||
|
using API.Services.Tasks;
|
||||||
|
using API.SignalR;
|
||||||
|
using AutoMapper;
|
||||||
|
using Microsoft.AspNetCore.SignalR;
|
||||||
|
using Microsoft.Data.Sqlite;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using NetVips;
|
||||||
|
using NSubstitute;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace API.Tests.Services;
|
||||||
|
|
||||||
|
public class BookmarkServiceTests
|
||||||
|
{
|
||||||
|
private readonly ILogger<CleanupService> _logger = Substitute.For<ILogger<CleanupService>>();
|
||||||
|
private readonly IUnitOfWork _unitOfWork;
|
||||||
|
private readonly IHubContext<MessageHub> _messageHub = Substitute.For<IHubContext<MessageHub>>();
|
||||||
|
|
||||||
|
private readonly DbConnection _connection;
|
||||||
|
private readonly DataContext _context;
|
||||||
|
|
||||||
|
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 BookmarkServiceTests()
|
||||||
|
{
|
||||||
|
var contextOptions = new DbContextOptionsBuilder()
|
||||||
|
.UseSqlite(CreateInMemoryDatabase())
|
||||||
|
.Options;
|
||||||
|
_connection = RelationalOptionsExtension.Extract(contextOptions).Connection;
|
||||||
|
|
||||||
|
_context = new DataContext(contextOptions);
|
||||||
|
Task.Run(SeedDb).GetAwaiter().GetResult();
|
||||||
|
|
||||||
|
_unitOfWork = new UnitOfWork(_context, Substitute.For<IMapper>(), null);
|
||||||
|
}
|
||||||
|
|
||||||
|
#region Setup
|
||||||
|
|
||||||
|
private static DbConnection CreateInMemoryDatabase()
|
||||||
|
{
|
||||||
|
var connection = new SqliteConnection("Filename=:memory:");
|
||||||
|
|
||||||
|
connection.Open();
|
||||||
|
|
||||||
|
return connection;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose() => _connection.Dispose();
|
||||||
|
|
||||||
|
private async Task<bool> SeedDb()
|
||||||
|
{
|
||||||
|
await _context.Database.MigrateAsync();
|
||||||
|
var filesystem = CreateFileSystem();
|
||||||
|
|
||||||
|
await Seed.SeedSettings(_context, new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), filesystem));
|
||||||
|
|
||||||
|
var setting = await _context.ServerSetting.Where(s => s.Key == ServerSettingKey.CacheDirectory).SingleAsync();
|
||||||
|
setting.Value = CacheDirectory;
|
||||||
|
|
||||||
|
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()
|
||||||
|
{
|
||||||
|
Name = "Manga",
|
||||||
|
Folders = new List<FolderPath>()
|
||||||
|
{
|
||||||
|
new FolderPath()
|
||||||
|
{
|
||||||
|
Path = "C:/data/"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return await _context.SaveChangesAsync() > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static MockFileSystem CreateFileSystem()
|
||||||
|
{
|
||||||
|
var fileSystem = new MockFileSystem();
|
||||||
|
fileSystem.Directory.SetCurrentDirectory("C:/kavita/");
|
||||||
|
fileSystem.AddDirectory("C:/kavita/config/");
|
||||||
|
fileSystem.AddDirectory(CacheDirectory);
|
||||||
|
fileSystem.AddDirectory(CoverImageDirectory);
|
||||||
|
fileSystem.AddDirectory(BackupDirectory);
|
||||||
|
fileSystem.AddDirectory(BookmarkDirectory);
|
||||||
|
fileSystem.AddDirectory("C:/data/");
|
||||||
|
|
||||||
|
return fileSystem;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region BookmarkPage
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task BookmarkPage_ShouldCopyTheFileAndUpdateDB()
|
||||||
|
{
|
||||||
|
var filesystem = CreateFileSystem();
|
||||||
|
filesystem.AddFile($"{CacheDirectory}1/0001.jpg", new MockFileData("123"));
|
||||||
|
|
||||||
|
// 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()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
_context.AppUser.Add(new AppUser()
|
||||||
|
{
|
||||||
|
UserName = "Joe"
|
||||||
|
});
|
||||||
|
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
|
||||||
|
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), filesystem);
|
||||||
|
var bookmarkService = new BookmarkService(Substitute.For<ILogger<BookmarkService>>(), _unitOfWork, ds);
|
||||||
|
var user = await _unitOfWork.UserRepository.GetUserByIdAsync(1, AppUserIncludes.Bookmarks);
|
||||||
|
|
||||||
|
var result = await bookmarkService.BookmarkPage(user, new BookmarkDto()
|
||||||
|
{
|
||||||
|
ChapterId = 1,
|
||||||
|
Page = 1,
|
||||||
|
SeriesId = 1,
|
||||||
|
VolumeId = 1
|
||||||
|
}, $"{CacheDirectory}1/0001.jpg");
|
||||||
|
|
||||||
|
|
||||||
|
Assert.True(result);
|
||||||
|
Assert.Equal(1, ds.GetFiles(BookmarkDirectory, searchOption:SearchOption.AllDirectories).Count());
|
||||||
|
Assert.NotNull(await _unitOfWork.UserRepository.GetBookmarkAsync(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task BookmarkPage_ShouldDeleteFileOnUnbookmark()
|
||||||
|
{
|
||||||
|
var filesystem = CreateFileSystem();
|
||||||
|
filesystem.AddFile($"{CacheDirectory}1/0001.jpg", new MockFileData("123"));
|
||||||
|
filesystem.AddFile($"{BookmarkDirectory}1/1/0001.jpg", new MockFileData("123"));
|
||||||
|
|
||||||
|
// 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()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
_context.AppUser.Add(new AppUser()
|
||||||
|
{
|
||||||
|
UserName = "Joe",
|
||||||
|
Bookmarks = new List<AppUserBookmark>()
|
||||||
|
{
|
||||||
|
new AppUserBookmark()
|
||||||
|
{
|
||||||
|
Page = 1,
|
||||||
|
ChapterId = 1,
|
||||||
|
FileName = $"1/1/0001.jpg",
|
||||||
|
SeriesId = 1,
|
||||||
|
VolumeId = 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
|
||||||
|
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), filesystem);
|
||||||
|
var bookmarkService = new BookmarkService(Substitute.For<ILogger<BookmarkService>>(), _unitOfWork, ds);
|
||||||
|
var user = await _unitOfWork.UserRepository.GetUserByIdAsync(1, AppUserIncludes.Bookmarks);
|
||||||
|
|
||||||
|
var result = await bookmarkService.RemoveBookmarkPage(user, new BookmarkDto()
|
||||||
|
{
|
||||||
|
ChapterId = 1,
|
||||||
|
Page = 1,
|
||||||
|
SeriesId = 1,
|
||||||
|
VolumeId = 1
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
Assert.True(result);
|
||||||
|
Assert.Equal(0, ds.GetFiles(BookmarkDirectory, searchOption:SearchOption.AllDirectories).Count());
|
||||||
|
Assert.Null(await _unitOfWork.UserRepository.GetBookmarkAsync(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region DeleteBookmarkFiles
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task DeleteBookmarkFiles_ShouldDeleteOnlyPassedFiles()
|
||||||
|
{
|
||||||
|
var filesystem = CreateFileSystem();
|
||||||
|
filesystem.AddFile($"{CacheDirectory}1/0001.jpg", new MockFileData("123"));
|
||||||
|
filesystem.AddFile($"{BookmarkDirectory}1/1/1/0001.jpg", new MockFileData("123"));
|
||||||
|
filesystem.AddFile($"{BookmarkDirectory}1/2/1/0002.jpg", new MockFileData("123"));
|
||||||
|
filesystem.AddFile($"{BookmarkDirectory}1/2/1/0001.jpg", new MockFileData("123"));
|
||||||
|
|
||||||
|
// 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()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
_context.AppUser.Add(new AppUser()
|
||||||
|
{
|
||||||
|
UserName = "Joe",
|
||||||
|
Bookmarks = new List<AppUserBookmark>()
|
||||||
|
{
|
||||||
|
new AppUserBookmark()
|
||||||
|
{
|
||||||
|
Page = 1,
|
||||||
|
ChapterId = 1,
|
||||||
|
FileName = $"1/1/1/0001.jpg",
|
||||||
|
SeriesId = 1,
|
||||||
|
VolumeId = 1
|
||||||
|
},
|
||||||
|
new AppUserBookmark()
|
||||||
|
{
|
||||||
|
Page = 2,
|
||||||
|
ChapterId = 1,
|
||||||
|
FileName = $"1/2/1/0002.jpg",
|
||||||
|
SeriesId = 2,
|
||||||
|
VolumeId = 1
|
||||||
|
},
|
||||||
|
new AppUserBookmark()
|
||||||
|
{
|
||||||
|
Page = 1,
|
||||||
|
ChapterId = 2,
|
||||||
|
FileName = $"1/2/1/0001.jpg",
|
||||||
|
SeriesId = 2,
|
||||||
|
VolumeId = 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
|
||||||
|
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), filesystem);
|
||||||
|
var bookmarkService = new BookmarkService(Substitute.For<ILogger<BookmarkService>>(), _unitOfWork, ds);
|
||||||
|
|
||||||
|
await bookmarkService.DeleteBookmarkFiles(new [] {new AppUserBookmark()
|
||||||
|
{
|
||||||
|
Page = 1,
|
||||||
|
ChapterId = 1,
|
||||||
|
FileName = $"1/1/1/0001.jpg",
|
||||||
|
SeriesId = 1,
|
||||||
|
VolumeId = 1
|
||||||
|
}});
|
||||||
|
|
||||||
|
|
||||||
|
Assert.Equal(2, ds.GetFiles(BookmarkDirectory, searchOption:SearchOption.AllDirectories).Count());
|
||||||
|
Assert.False(ds.FileSystem.FileInfo.FromFileName(Path.Join(BookmarkDirectory, "1/1/1/0001.jpg")).Exists);
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
}
|
@ -364,142 +364,142 @@ public class CleanupServiceTests
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region CleanupBookmarks
|
// #region CleanupBookmarks
|
||||||
|
//
|
||||||
[Fact]
|
// [Fact]
|
||||||
public async Task CleanupBookmarks_LeaveAllFiles()
|
// public async Task CleanupBookmarks_LeaveAllFiles()
|
||||||
{
|
// {
|
||||||
var filesystem = CreateFileSystem();
|
// var filesystem = CreateFileSystem();
|
||||||
filesystem.AddFile($"{BookmarkDirectory}1/1/1/0001.jpg", new MockFileData(""));
|
// filesystem.AddFile($"{BookmarkDirectory}1/1/1/0001.jpg", new MockFileData(""));
|
||||||
filesystem.AddFile($"{BookmarkDirectory}1/1/1/0002.jpg", new MockFileData(""));
|
// filesystem.AddFile($"{BookmarkDirectory}1/1/1/0002.jpg", new MockFileData(""));
|
||||||
|
//
|
||||||
// Delete all Series to reset state
|
// // Delete all Series to reset state
|
||||||
await ResetDB();
|
// await ResetDB();
|
||||||
|
//
|
||||||
_context.Series.Add(new Series()
|
// _context.Series.Add(new Series()
|
||||||
{
|
// {
|
||||||
Name = "Test",
|
// Name = "Test",
|
||||||
Library = new Library() {
|
// Library = new Library() {
|
||||||
Name = "Test LIb",
|
// Name = "Test LIb",
|
||||||
Type = LibraryType.Manga,
|
// Type = LibraryType.Manga,
|
||||||
},
|
// },
|
||||||
Volumes = new List<Volume>()
|
// Volumes = new List<Volume>()
|
||||||
{
|
// {
|
||||||
new Volume()
|
// new Volume()
|
||||||
{
|
// {
|
||||||
Chapters = new List<Chapter>()
|
// Chapters = new List<Chapter>()
|
||||||
{
|
// {
|
||||||
new Chapter()
|
// new Chapter()
|
||||||
{
|
// {
|
||||||
|
//
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
});
|
// });
|
||||||
|
//
|
||||||
await _context.SaveChangesAsync();
|
// await _context.SaveChangesAsync();
|
||||||
|
//
|
||||||
_context.AppUser.Add(new AppUser()
|
// _context.AppUser.Add(new AppUser()
|
||||||
{
|
// {
|
||||||
Bookmarks = new List<AppUserBookmark>()
|
// Bookmarks = new List<AppUserBookmark>()
|
||||||
{
|
// {
|
||||||
new AppUserBookmark()
|
// new AppUserBookmark()
|
||||||
{
|
// {
|
||||||
AppUserId = 1,
|
// AppUserId = 1,
|
||||||
ChapterId = 1,
|
// ChapterId = 1,
|
||||||
Page = 1,
|
// Page = 1,
|
||||||
FileName = "1/1/1/0001.jpg",
|
// FileName = "1/1/1/0001.jpg",
|
||||||
SeriesId = 1,
|
// SeriesId = 1,
|
||||||
VolumeId = 1
|
// VolumeId = 1
|
||||||
},
|
// },
|
||||||
new AppUserBookmark()
|
// new AppUserBookmark()
|
||||||
{
|
// {
|
||||||
AppUserId = 1,
|
// AppUserId = 1,
|
||||||
ChapterId = 1,
|
// ChapterId = 1,
|
||||||
Page = 2,
|
// Page = 2,
|
||||||
FileName = "1/1/1/0002.jpg",
|
// FileName = "1/1/1/0002.jpg",
|
||||||
SeriesId = 1,
|
// SeriesId = 1,
|
||||||
VolumeId = 1
|
// VolumeId = 1
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
});
|
// });
|
||||||
|
//
|
||||||
await _context.SaveChangesAsync();
|
// await _context.SaveChangesAsync();
|
||||||
|
//
|
||||||
|
//
|
||||||
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), filesystem);
|
// var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), filesystem);
|
||||||
var cleanupService = new CleanupService(_logger, _unitOfWork, _messageHub,
|
// var cleanupService = new CleanupService(_logger, _unitOfWork, _messageHub,
|
||||||
ds);
|
// ds);
|
||||||
|
//
|
||||||
await cleanupService.CleanupBookmarks();
|
// await cleanupService.CleanupBookmarks();
|
||||||
|
//
|
||||||
Assert.Equal(2, ds.GetFiles(BookmarkDirectory, searchOption:SearchOption.AllDirectories).Count());
|
// Assert.Equal(2, ds.GetFiles(BookmarkDirectory, searchOption:SearchOption.AllDirectories).Count());
|
||||||
|
//
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
[Fact]
|
// [Fact]
|
||||||
public async Task CleanupBookmarks_LeavesOneFiles()
|
// public async Task CleanupBookmarks_LeavesOneFiles()
|
||||||
{
|
// {
|
||||||
var filesystem = CreateFileSystem();
|
// var filesystem = CreateFileSystem();
|
||||||
filesystem.AddFile($"{BookmarkDirectory}1/1/1/0001.jpg", new MockFileData(""));
|
// filesystem.AddFile($"{BookmarkDirectory}1/1/1/0001.jpg", new MockFileData(""));
|
||||||
filesystem.AddFile($"{BookmarkDirectory}1/1/2/0002.jpg", new MockFileData(""));
|
// filesystem.AddFile($"{BookmarkDirectory}1/1/2/0002.jpg", new MockFileData(""));
|
||||||
|
//
|
||||||
// Delete all Series to reset state
|
// // Delete all Series to reset state
|
||||||
await ResetDB();
|
// await ResetDB();
|
||||||
|
//
|
||||||
_context.Series.Add(new Series()
|
// _context.Series.Add(new Series()
|
||||||
{
|
// {
|
||||||
Name = "Test",
|
// Name = "Test",
|
||||||
Library = new Library() {
|
// Library = new Library() {
|
||||||
Name = "Test LIb",
|
// Name = "Test LIb",
|
||||||
Type = LibraryType.Manga,
|
// Type = LibraryType.Manga,
|
||||||
},
|
// },
|
||||||
Volumes = new List<Volume>()
|
// Volumes = new List<Volume>()
|
||||||
{
|
// {
|
||||||
new Volume()
|
// new Volume()
|
||||||
{
|
// {
|
||||||
Chapters = new List<Chapter>()
|
// Chapters = new List<Chapter>()
|
||||||
{
|
// {
|
||||||
new Chapter()
|
// new Chapter()
|
||||||
{
|
// {
|
||||||
|
//
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
});
|
// });
|
||||||
|
//
|
||||||
await _context.SaveChangesAsync();
|
// await _context.SaveChangesAsync();
|
||||||
|
//
|
||||||
_context.AppUser.Add(new AppUser()
|
// _context.AppUser.Add(new AppUser()
|
||||||
{
|
// {
|
||||||
Bookmarks = new List<AppUserBookmark>()
|
// Bookmarks = new List<AppUserBookmark>()
|
||||||
{
|
// {
|
||||||
new AppUserBookmark()
|
// new AppUserBookmark()
|
||||||
{
|
// {
|
||||||
AppUserId = 1,
|
// AppUserId = 1,
|
||||||
ChapterId = 1,
|
// ChapterId = 1,
|
||||||
Page = 1,
|
// Page = 1,
|
||||||
FileName = "1/1/1/0001.jpg",
|
// FileName = "1/1/1/0001.jpg",
|
||||||
SeriesId = 1,
|
// SeriesId = 1,
|
||||||
VolumeId = 1
|
// VolumeId = 1
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
});
|
// });
|
||||||
|
//
|
||||||
await _context.SaveChangesAsync();
|
// await _context.SaveChangesAsync();
|
||||||
|
//
|
||||||
|
//
|
||||||
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), filesystem);
|
// var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), filesystem);
|
||||||
var cleanupService = new CleanupService(_logger, _unitOfWork, _messageHub,
|
// var cleanupService = new CleanupService(_logger, _unitOfWork, _messageHub,
|
||||||
ds);
|
// ds);
|
||||||
|
//
|
||||||
await cleanupService.CleanupBookmarks();
|
// await cleanupService.CleanupBookmarks();
|
||||||
|
//
|
||||||
Assert.Equal(1, ds.GetFiles(BookmarkDirectory, searchOption:SearchOption.AllDirectories).Count());
|
// Assert.Equal(1, ds.GetFiles(BookmarkDirectory, searchOption:SearchOption.AllDirectories).Count());
|
||||||
Assert.Equal(1, ds.FileSystem.Directory.GetDirectories($"{BookmarkDirectory}1/1/").Length);
|
// Assert.Equal(1, ds.FileSystem.Directory.GetDirectories($"{BookmarkDirectory}1/1/").Length);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
#endregion
|
// #endregion
|
||||||
}
|
}
|
||||||
|
@ -28,12 +28,13 @@ namespace API.Controllers
|
|||||||
private readonly IReaderService _readerService;
|
private readonly IReaderService _readerService;
|
||||||
private readonly IDirectoryService _directoryService;
|
private readonly IDirectoryService _directoryService;
|
||||||
private readonly ICleanupService _cleanupService;
|
private readonly ICleanupService _cleanupService;
|
||||||
|
private readonly IBookmarkService _bookmarkService;
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public ReaderController(ICacheService cacheService,
|
public ReaderController(ICacheService cacheService,
|
||||||
IUnitOfWork unitOfWork, ILogger<ReaderController> logger,
|
IUnitOfWork unitOfWork, ILogger<ReaderController> logger,
|
||||||
IReaderService readerService, IDirectoryService directoryService,
|
IReaderService readerService, IDirectoryService directoryService,
|
||||||
ICleanupService cleanupService)
|
ICleanupService cleanupService, IBookmarkService bookmarkService)
|
||||||
{
|
{
|
||||||
_cacheService = cacheService;
|
_cacheService = cacheService;
|
||||||
_unitOfWork = unitOfWork;
|
_unitOfWork = unitOfWork;
|
||||||
@ -41,6 +42,7 @@ namespace API.Controllers
|
|||||||
_readerService = readerService;
|
_readerService = readerService;
|
||||||
_directoryService = directoryService;
|
_directoryService = directoryService;
|
||||||
_cleanupService = cleanupService;
|
_cleanupService = cleanupService;
|
||||||
|
_bookmarkService = bookmarkService;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -451,6 +453,7 @@ namespace API.Controllers
|
|||||||
if (user.Bookmarks == null) return Ok("Nothing to remove");
|
if (user.Bookmarks == null) return Ok("Nothing to remove");
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
var bookmarksToRemove = user.Bookmarks.Where(bmk => bmk.SeriesId == dto.SeriesId).ToList();
|
||||||
user.Bookmarks = user.Bookmarks.Where(bmk => bmk.SeriesId != dto.SeriesId).ToList();
|
user.Bookmarks = user.Bookmarks.Where(bmk => bmk.SeriesId != dto.SeriesId).ToList();
|
||||||
_unitOfWork.UserRepository.Update(user);
|
_unitOfWork.UserRepository.Update(user);
|
||||||
|
|
||||||
@ -458,7 +461,7 @@ namespace API.Controllers
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await _cleanupService.CleanupBookmarks();
|
await _bookmarkService.DeleteBookmarkFiles(bookmarksToRemove);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@ -514,49 +517,17 @@ namespace API.Controllers
|
|||||||
{
|
{
|
||||||
// Don't let user save past total pages.
|
// Don't let user save past total pages.
|
||||||
bookmarkDto.Page = await _readerService.CapPageToChapter(bookmarkDto.ChapterId, bookmarkDto.Page);
|
bookmarkDto.Page = await _readerService.CapPageToChapter(bookmarkDto.ChapterId, bookmarkDto.Page);
|
||||||
|
var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername(), AppUserIncludes.Bookmarks);
|
||||||
|
var chapter = await _cacheService.Ensure(bookmarkDto.ChapterId);
|
||||||
|
if (chapter == null) return BadRequest("Could not find cached image. Reload and try again.");
|
||||||
|
var path = _cacheService.GetCachedPagePath(chapter, bookmarkDto.Page);
|
||||||
|
|
||||||
try
|
if (await _bookmarkService.BookmarkPage(user, bookmarkDto, path))
|
||||||
{
|
{
|
||||||
var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername(), AppUserIncludes.Bookmarks);
|
return Ok();
|
||||||
var userBookmark =
|
|
||||||
await _unitOfWork.UserRepository.GetBookmarkForPage(bookmarkDto.Page, bookmarkDto.ChapterId, user.Id);
|
|
||||||
|
|
||||||
// We need to get the image
|
|
||||||
var chapter = await _cacheService.Ensure(bookmarkDto.ChapterId);
|
|
||||||
if (chapter == null) return BadRequest("There was an issue finding image file for reading");
|
|
||||||
var path = _cacheService.GetCachedPagePath(chapter, bookmarkDto.Page);
|
|
||||||
var fileInfo = new FileInfo(path);
|
|
||||||
|
|
||||||
var bookmarkDirectory =
|
|
||||||
(await _unitOfWork.SettingsRepository.GetSettingAsync(ServerSettingKey.BookmarkDirectory)).Value;
|
|
||||||
_directoryService.CopyFileToDirectory(path, Path.Join(bookmarkDirectory,
|
|
||||||
$"{user.Id}", $"{bookmarkDto.SeriesId}", $"{bookmarkDto.ChapterId}"));
|
|
||||||
|
|
||||||
|
|
||||||
if (userBookmark == null)
|
|
||||||
{
|
|
||||||
user.Bookmarks ??= new List<AppUserBookmark>();
|
|
||||||
user.Bookmarks.Add(new AppUserBookmark()
|
|
||||||
{
|
|
||||||
Page = bookmarkDto.Page,
|
|
||||||
VolumeId = bookmarkDto.VolumeId,
|
|
||||||
SeriesId = bookmarkDto.SeriesId,
|
|
||||||
ChapterId = bookmarkDto.ChapterId,
|
|
||||||
FileName = Path.Join($"{user.Id}", $"{bookmarkDto.SeriesId}", $"{bookmarkDto.ChapterId}", fileInfo.Name)
|
|
||||||
|
|
||||||
});
|
|
||||||
_unitOfWork.UserRepository.Update(user);
|
|
||||||
}
|
|
||||||
|
|
||||||
await _unitOfWork.CommitAsync();
|
|
||||||
}
|
|
||||||
catch (Exception)
|
|
||||||
{
|
|
||||||
await _unitOfWork.RollbackAsync();
|
|
||||||
return BadRequest("Could not save bookmark");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return Ok();
|
return BadRequest("Could not save bookmark");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -568,27 +539,11 @@ namespace API.Controllers
|
|||||||
public async Task<ActionResult> UnBookmarkPage(BookmarkDto bookmarkDto)
|
public async Task<ActionResult> UnBookmarkPage(BookmarkDto bookmarkDto)
|
||||||
{
|
{
|
||||||
var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername(), AppUserIncludes.Bookmarks);
|
var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername(), AppUserIncludes.Bookmarks);
|
||||||
|
|
||||||
if (user.Bookmarks == null) return Ok();
|
if (user.Bookmarks == null) return Ok();
|
||||||
try
|
|
||||||
{
|
|
||||||
var bookmarkToDelete = user.Bookmarks.SingleOrDefault(x =>
|
|
||||||
x.ChapterId == bookmarkDto.ChapterId && x.AppUserId == user.Id && x.Page == bookmarkDto.Page &&
|
|
||||||
x.SeriesId == bookmarkDto.SeriesId);
|
|
||||||
|
|
||||||
if (bookmarkToDelete != null)
|
if (await _bookmarkService.RemoveBookmarkPage(user, bookmarkDto))
|
||||||
{
|
|
||||||
_unitOfWork.UserRepository.Delete(bookmarkToDelete);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (await _unitOfWork.CommitAsync())
|
|
||||||
{
|
|
||||||
return Ok();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception)
|
|
||||||
{
|
{
|
||||||
await _unitOfWork.RollbackAsync();
|
return Ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
return BadRequest("Could not remove bookmark");
|
return BadRequest("Could not remove bookmark");
|
||||||
|
@ -38,6 +38,7 @@ namespace API.Extensions
|
|||||||
services.AddScoped<IReadingItemService, ReadingItemService>();
|
services.AddScoped<IReadingItemService, ReadingItemService>();
|
||||||
services.AddScoped<IAccountService, AccountService>();
|
services.AddScoped<IAccountService, AccountService>();
|
||||||
services.AddScoped<IEmailService, EmailService>();
|
services.AddScoped<IEmailService, EmailService>();
|
||||||
|
services.AddScoped<IBookmarkService, BookmarkService>();
|
||||||
|
|
||||||
services.AddScoped<IFileSystem, FileSystem>();
|
services.AddScoped<IFileSystem, FileSystem>();
|
||||||
services.AddScoped<IFileService, FileService>();
|
services.AddScoped<IFileService, FileService>();
|
||||||
|
147
API/Services/BookmarkService.cs
Normal file
147
API/Services/BookmarkService.cs
Normal file
@ -0,0 +1,147 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using API.Data;
|
||||||
|
using API.Data.Repositories;
|
||||||
|
using API.DTOs.Reader;
|
||||||
|
using API.Entities;
|
||||||
|
using API.Entities.Enums;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
namespace API.Services;
|
||||||
|
|
||||||
|
public interface IBookmarkService
|
||||||
|
{
|
||||||
|
Task DeleteBookmarkFiles(IEnumerable<AppUserBookmark> bookmarks);
|
||||||
|
Task<bool> BookmarkPage(AppUser userWithBookmarks, BookmarkDto bookmarkDto, string imageToBookmark);
|
||||||
|
Task<bool> RemoveBookmarkPage(AppUser userWithBookmarks, BookmarkDto bookmarkDto);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class BookmarkService : IBookmarkService
|
||||||
|
{
|
||||||
|
private readonly ILogger<BookmarkService> _logger;
|
||||||
|
private readonly IUnitOfWork _unitOfWork;
|
||||||
|
private readonly IDirectoryService _directoryService;
|
||||||
|
|
||||||
|
public BookmarkService(ILogger<BookmarkService> logger, IUnitOfWork unitOfWork, IDirectoryService directoryService)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
_unitOfWork = unitOfWork;
|
||||||
|
_directoryService = directoryService;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Deletes the files associated with the list of Bookmarks passed. Will clean up empty folders.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="bookmarks"></param>
|
||||||
|
public async Task DeleteBookmarkFiles(IEnumerable<AppUserBookmark> bookmarks)
|
||||||
|
{
|
||||||
|
var bookmarkDirectory =
|
||||||
|
(await _unitOfWork.SettingsRepository.GetSettingAsync(ServerSettingKey.BookmarkDirectory)).Value;
|
||||||
|
|
||||||
|
var bookmarkFilesToDelete = bookmarks.Select(b => Parser.Parser.NormalizePath(
|
||||||
|
_directoryService.FileSystem.Path.Join(bookmarkDirectory,
|
||||||
|
b.FileName))).ToList();
|
||||||
|
|
||||||
|
if (bookmarkFilesToDelete.Count == 0) return;
|
||||||
|
|
||||||
|
_directoryService.DeleteFiles(bookmarkFilesToDelete);
|
||||||
|
|
||||||
|
// Delete any leftover folders
|
||||||
|
foreach (var directory in _directoryService.FileSystem.Directory.GetDirectories(bookmarkDirectory, "", SearchOption.AllDirectories))
|
||||||
|
{
|
||||||
|
if (_directoryService.FileSystem.Directory.GetFiles(directory, "", SearchOption.AllDirectories).Length == 0 &&
|
||||||
|
_directoryService.FileSystem.Directory.GetDirectories(directory).Length == 0)
|
||||||
|
{
|
||||||
|
_directoryService.FileSystem.Directory.Delete(directory, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new entry in the AppUserBookmarks and copies an image to BookmarkDirectory.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="userWithBookmarks">An AppUser object with Bookmarks populated</param>
|
||||||
|
/// <param name="bookmarkDto"></param>
|
||||||
|
/// <param name="imageToBookmark">Full path to the cached image that is going to be copied</param>
|
||||||
|
/// <returns>If the save to DB and copy was successful</returns>
|
||||||
|
public async Task<bool> BookmarkPage(AppUser userWithBookmarks, BookmarkDto bookmarkDto, string imageToBookmark)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var userBookmark =
|
||||||
|
await _unitOfWork.UserRepository.GetBookmarkForPage(bookmarkDto.Page, bookmarkDto.ChapterId, userWithBookmarks.Id);
|
||||||
|
|
||||||
|
if (userBookmark != null)
|
||||||
|
{
|
||||||
|
_logger.LogError("Bookmark already exists for Series {SeriesId}, Volume {VolumeId}, Chapter {ChapterId}, Page {PageNum}", bookmarkDto.SeriesId, bookmarkDto.VolumeId, bookmarkDto.ChapterId, bookmarkDto.Page);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
var fileInfo = new FileInfo(imageToBookmark);
|
||||||
|
var bookmarkDirectory =
|
||||||
|
(await _unitOfWork.SettingsRepository.GetSettingAsync(ServerSettingKey.BookmarkDirectory)).Value;
|
||||||
|
var targetFolderStem = BookmarkStem(userWithBookmarks.Id, bookmarkDto.SeriesId, bookmarkDto.ChapterId);
|
||||||
|
var targetFilepath = Path.Join(bookmarkDirectory, targetFolderStem);
|
||||||
|
|
||||||
|
userWithBookmarks.Bookmarks ??= new List<AppUserBookmark>();
|
||||||
|
userWithBookmarks.Bookmarks.Add(new AppUserBookmark()
|
||||||
|
{
|
||||||
|
Page = bookmarkDto.Page,
|
||||||
|
VolumeId = bookmarkDto.VolumeId,
|
||||||
|
SeriesId = bookmarkDto.SeriesId,
|
||||||
|
ChapterId = bookmarkDto.ChapterId,
|
||||||
|
FileName = Path.Join(targetFolderStem, fileInfo.Name)
|
||||||
|
});
|
||||||
|
_directoryService.CopyFileToDirectory(imageToBookmark, targetFilepath);
|
||||||
|
_unitOfWork.UserRepository.Update(userWithBookmarks);
|
||||||
|
await _unitOfWork.CommitAsync();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "There was an exception when saving bookmark");
|
||||||
|
await _unitOfWork.RollbackAsync();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Removes the Bookmark entity and the file from BookmarkDirectory
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="userWithBookmarks"></param>
|
||||||
|
/// <param name="bookmarkDto"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<bool> RemoveBookmarkPage(AppUser userWithBookmarks, BookmarkDto bookmarkDto)
|
||||||
|
{
|
||||||
|
if (userWithBookmarks.Bookmarks == null) return true;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var bookmarkToDelete = userWithBookmarks.Bookmarks.SingleOrDefault(x =>
|
||||||
|
x.ChapterId == bookmarkDto.ChapterId && x.AppUserId == userWithBookmarks.Id && x.Page == bookmarkDto.Page &&
|
||||||
|
x.SeriesId == bookmarkDto.SeriesId);
|
||||||
|
|
||||||
|
if (bookmarkToDelete != null)
|
||||||
|
{
|
||||||
|
await DeleteBookmarkFiles(new[] {bookmarkToDelete});
|
||||||
|
_unitOfWork.UserRepository.Delete(bookmarkToDelete);
|
||||||
|
}
|
||||||
|
|
||||||
|
await _unitOfWork.CommitAsync();
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
await _unitOfWork.RollbackAsync();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string BookmarkStem(int userId, int seriesId, int chapterId)
|
||||||
|
{
|
||||||
|
return Path.Join($"{userId}", $"{seriesId}", $"{chapterId}");
|
||||||
|
}
|
||||||
|
}
|
@ -198,11 +198,10 @@ namespace API.Services
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
var fileInfo = FileSystem.FileInfo.FromFileName(fullFilePath);
|
var fileInfo = FileSystem.FileInfo.FromFileName(fullFilePath);
|
||||||
if (fileInfo.Exists)
|
if (!fileInfo.Exists) return;
|
||||||
{
|
|
||||||
ExistOrCreate(targetDirectory);
|
ExistOrCreate(targetDirectory);
|
||||||
fileInfo.CopyTo(FileSystem.Path.Join(targetDirectory, fileInfo.Name), true);
|
fileInfo.CopyTo(FileSystem.Path.Join(targetDirectory, fileInfo.Name), true);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
@ -66,8 +66,8 @@ namespace API.Services.Tasks
|
|||||||
await SendProgress(0.7F);
|
await SendProgress(0.7F);
|
||||||
await DeleteTagCoverImages();
|
await DeleteTagCoverImages();
|
||||||
await SendProgress(0.8F);
|
await SendProgress(0.8F);
|
||||||
_logger.LogInformation("Cleaning old bookmarks");
|
//_logger.LogInformation("Cleaning old bookmarks");
|
||||||
await CleanupBookmarks();
|
//await CleanupBookmarks();
|
||||||
await SendProgress(1F);
|
await SendProgress(1F);
|
||||||
_logger.LogInformation("Cleanup finished");
|
_logger.LogInformation("Cleanup finished");
|
||||||
}
|
}
|
||||||
@ -172,33 +172,35 @@ namespace API.Services.Tasks
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Removes all files in the BookmarkDirectory that don't currently have bookmarks in the Database
|
/// Removes all files in the BookmarkDirectory that don't currently have bookmarks in the Database
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public async Task CleanupBookmarks()
|
public Task CleanupBookmarks()
|
||||||
{
|
{
|
||||||
|
// This is disabled for now while we test and validate a new method of deleting bookmarks
|
||||||
|
return Task.CompletedTask;
|
||||||
// Search all files in bookmarks/ except bookmark files and delete those
|
// Search all files in bookmarks/ except bookmark files and delete those
|
||||||
var bookmarkDirectory =
|
// var bookmarkDirectory =
|
||||||
(await _unitOfWork.SettingsRepository.GetSettingAsync(ServerSettingKey.BookmarkDirectory)).Value;
|
// (await _unitOfWork.SettingsRepository.GetSettingAsync(ServerSettingKey.BookmarkDirectory)).Value;
|
||||||
var allBookmarkFiles = _directoryService.GetFiles(bookmarkDirectory, searchOption: SearchOption.AllDirectories).Select(Parser.Parser.NormalizePath);
|
// var allBookmarkFiles = _directoryService.GetFiles(bookmarkDirectory, searchOption: SearchOption.AllDirectories).Select(Parser.Parser.NormalizePath);
|
||||||
var bookmarks = (await _unitOfWork.UserRepository.GetAllBookmarksAsync())
|
// var bookmarks = (await _unitOfWork.UserRepository.GetAllBookmarksAsync())
|
||||||
.Select(b => Parser.Parser.NormalizePath(_directoryService.FileSystem.Path.Join(bookmarkDirectory,
|
// .Select(b => Parser.Parser.NormalizePath(_directoryService.FileSystem.Path.Join(bookmarkDirectory,
|
||||||
b.FileName)));
|
// b.FileName)));
|
||||||
|
//
|
||||||
|
//
|
||||||
var filesToDelete = allBookmarkFiles.AsEnumerable().Except(bookmarks).ToList();
|
// var filesToDelete = allBookmarkFiles.AsEnumerable().Except(bookmarks).ToList();
|
||||||
_logger.LogDebug("[Bookmarks] Bookmark cleanup wants to delete {Count} files", filesToDelete.Count);
|
// _logger.LogDebug("[Bookmarks] Bookmark cleanup wants to delete {Count} files", filesToDelete.Count);
|
||||||
|
//
|
||||||
if (filesToDelete.Count == 0) return;
|
// if (filesToDelete.Count == 0) return;
|
||||||
|
//
|
||||||
_directoryService.DeleteFiles(filesToDelete);
|
// _directoryService.DeleteFiles(filesToDelete);
|
||||||
|
//
|
||||||
// Clear all empty directories
|
// // Clear all empty directories
|
||||||
foreach (var directory in _directoryService.FileSystem.Directory.GetDirectories(bookmarkDirectory, "", SearchOption.AllDirectories))
|
// foreach (var directory in _directoryService.FileSystem.Directory.GetDirectories(bookmarkDirectory, "", SearchOption.AllDirectories))
|
||||||
{
|
// {
|
||||||
if (_directoryService.FileSystem.Directory.GetFiles(directory, "", SearchOption.AllDirectories).Length == 0 &&
|
// if (_directoryService.FileSystem.Directory.GetFiles(directory, "", SearchOption.AllDirectories).Length == 0 &&
|
||||||
_directoryService.FileSystem.Directory.GetDirectories(directory).Length == 0)
|
// _directoryService.FileSystem.Directory.GetDirectories(directory).Length == 0)
|
||||||
{
|
// {
|
||||||
_directoryService.FileSystem.Directory.Delete(directory, false);
|
// _directoryService.FileSystem.Directory.Delete(directory, false);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ using System.Linq;
|
|||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using API.Constants;
|
||||||
using API.Data;
|
using API.Data;
|
||||||
using API.Entities;
|
using API.Entities;
|
||||||
using API.Extensions;
|
using API.Extensions;
|
||||||
@ -150,7 +151,12 @@ namespace API
|
|||||||
await MigrateBookmarks.Migrate(directoryService, unitOfWork,
|
await MigrateBookmarks.Migrate(directoryService, unitOfWork,
|
||||||
logger, cacheService);
|
logger, cacheService);
|
||||||
|
|
||||||
await MigrateChangePasswordRoles.Migrate(unitOfWork, userManager);
|
// Only run this if we are upgrading
|
||||||
|
var usersWithRole = await userManager.GetUsersInRoleAsync(PolicyConstants.ChangePasswordRole);
|
||||||
|
if (usersWithRole.Count == 0)
|
||||||
|
{
|
||||||
|
await MigrateChangePasswordRoles.Migrate(unitOfWork, userManager);
|
||||||
|
}
|
||||||
|
|
||||||
var requiresCoverImageMigration = !Directory.Exists(directoryService.CoverImageDirectory);
|
var requiresCoverImageMigration = !Directory.Exists(directoryService.CoverImageDirectory);
|
||||||
try
|
try
|
||||||
|
Loading…
x
Reference in New Issue
Block a user