diff --git a/API.Tests/API.Tests.csproj b/API.Tests/API.Tests.csproj index 708e253c0..5b1cacac2 100644 --- a/API.Tests/API.Tests.csproj +++ b/API.Tests/API.Tests.csproj @@ -7,12 +7,12 @@ - - + + - + - + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/API.Tests/Services/BookmarkServiceTests.cs b/API.Tests/Services/BookmarkServiceTests.cs index 13acf3684..f04c8e676 100644 --- a/API.Tests/Services/BookmarkServiceTests.cs +++ b/API.Tests/Services/BookmarkServiceTests.cs @@ -47,6 +47,12 @@ public class BookmarkServiceTests _unitOfWork = new UnitOfWork(_context, Substitute.For(), null); } + private BookmarkService Create(IDirectoryService ds) + { + return new BookmarkService(Substitute.For>(), _unitOfWork, ds, + Substitute.For(), Substitute.For()); + } + #region Setup private static DbConnection CreateInMemoryDatabase() @@ -121,7 +127,8 @@ public class BookmarkServiceTests public async Task BookmarkPage_ShouldCopyTheFileAndUpdateDB() { var filesystem = CreateFileSystem(); - filesystem.AddFile($"{CacheDirectory}1/0001.jpg", new MockFileData("123")); + var file = $"{CacheDirectory}1/0001.jpg"; + filesystem.AddFile(file, new MockFileData("123")); // Delete all Series to reset state await ResetDB(); @@ -157,7 +164,7 @@ public class BookmarkServiceTests var ds = new DirectoryService(Substitute.For>(), filesystem); - var bookmarkService = new BookmarkService(Substitute.For>(), _unitOfWork, ds); + var bookmarkService = Create(ds); var user = await _unitOfWork.UserRepository.GetUserByIdAsync(1, AppUserIncludes.Bookmarks); var result = await bookmarkService.BookmarkPage(user, new BookmarkDto() @@ -166,7 +173,7 @@ public class BookmarkServiceTests Page = 1, SeriesId = 1, VolumeId = 1 - }, $"{CacheDirectory}1/0001.jpg"); + }, file); Assert.True(result); @@ -227,7 +234,7 @@ public class BookmarkServiceTests var ds = new DirectoryService(Substitute.For>(), filesystem); - var bookmarkService = new BookmarkService(Substitute.For>(), _unitOfWork, ds); + var bookmarkService = Create(ds); var user = await _unitOfWork.UserRepository.GetUserByIdAsync(1, AppUserIncludes.Bookmarks); var result = await bookmarkService.RemoveBookmarkPage(user, new BookmarkDto() @@ -319,7 +326,7 @@ public class BookmarkServiceTests var ds = new DirectoryService(Substitute.For>(), filesystem); - var bookmarkService = new BookmarkService(Substitute.For>(), _unitOfWork, ds); + var bookmarkService = Create(ds); await bookmarkService.DeleteBookmarkFiles(new [] {new AppUserBookmark() { @@ -378,7 +385,7 @@ public class BookmarkServiceTests var ds = new DirectoryService(Substitute.For>(), filesystem); - var bookmarkService = new BookmarkService(Substitute.For>(), _unitOfWork, ds); + var bookmarkService = Create(ds); var user = await _unitOfWork.UserRepository.GetUserByIdAsync(1, AppUserIncludes.Bookmarks); await bookmarkService.BookmarkPage(user, new BookmarkDto() diff --git a/API/API.csproj b/API/API.csproj index 1099893a3..82f0e9b4d 100644 --- a/API/API.csproj +++ b/API/API.csproj @@ -40,39 +40,39 @@ - - - - + + + + - - - + + + - + all runtime; build; native; contentfiles; analyzers; buildtransitive - + - + - + all runtime; build; native; contentfiles; analyzers; buildtransitive - - - + + + diff --git a/API/Controllers/AccountController.cs b/API/Controllers/AccountController.cs index cc0b66ec1..ab4ac6ccb 100644 --- a/API/Controllers/AccountController.cs +++ b/API/Controllers/AccountController.cs @@ -15,6 +15,7 @@ using API.Entities.Enums; using API.Errors; using API.Extensions; using API.Services; +using API.SignalR; using AutoMapper; using Kavita.Common; using Microsoft.AspNetCore.Authorization; @@ -40,13 +41,16 @@ namespace API.Controllers private readonly IAccountService _accountService; private readonly IEmailService _emailService; private readonly IHostEnvironment _environment; + private readonly IEventHub _eventHub; /// public AccountController(UserManager userManager, SignInManager signInManager, ITokenService tokenService, IUnitOfWork unitOfWork, ILogger logger, - IMapper mapper, IAccountService accountService, IEmailService emailService, IHostEnvironment environment) + IMapper mapper, IAccountService accountService, + IEmailService emailService, IHostEnvironment environment, + IEventHub eventHub) { _userManager = userManager; _signInManager = signInManager; @@ -57,6 +61,7 @@ namespace API.Controllers _accountService = accountService; _emailService = emailService; _environment = environment; + _eventHub = eventHub; } /// @@ -289,6 +294,7 @@ namespace API.Controllers { dto.Roles.Add(PolicyConstants.PlebRole); } + if (existingRoles.Except(dto.Roles).Any() || dto.Roles.Except(existingRoles).Any()) { var roles = dto.Roles; @@ -326,9 +332,9 @@ namespace API.Controllers lib.AppUsers.Add(user); } - if (!_unitOfWork.HasChanges()) return Ok(); - if (await _unitOfWork.CommitAsync()) + if (!_unitOfWork.HasChanges() || await _unitOfWork.CommitAsync()) { + await _eventHub.SendMessageToAsync(MessageFactory.UserUpdate, MessageFactory.UserUpdateEvent(user.Id, user.UserName), user.Id); return Ok(); } diff --git a/API/Controllers/ReaderController.cs b/API/Controllers/ReaderController.cs index 7ced84d6a..0385c3438 100644 --- a/API/Controllers/ReaderController.cs +++ b/API/Controllers/ReaderController.cs @@ -424,6 +424,7 @@ namespace API.Controllers /// /// This is built for Tachiyomi and is not expected to be called by any other place /// + [Obsolete("Deprecated. Use 'Tachiyomi/mark-chapter-until-as-read'")] [HttpPost("mark-chapter-until-as-read")] public async Task> MarkChaptersUntilAsRead(int seriesId, float chapterNumber) { @@ -497,7 +498,7 @@ namespace API.Controllers user.Bookmarks = user.Bookmarks.Where(bmk => bmk.SeriesId != dto.SeriesId).ToList(); _unitOfWork.UserRepository.Update(user); - if (await _unitOfWork.CommitAsync()) + if (!_unitOfWork.HasChanges() || await _unitOfWork.CommitAsync()) { try { diff --git a/API/Controllers/ServerController.cs b/API/Controllers/ServerController.cs index 222243fdc..7fabb2a82 100644 --- a/API/Controllers/ServerController.cs +++ b/API/Controllers/ServerController.cs @@ -1,19 +1,24 @@ using System; +using System.Collections; using System.Collections.Generic; using System.IO; +using System.Linq; using System.Threading.Tasks; +using API.DTOs.Jobs; using API.DTOs.Stats; using API.DTOs.Update; using API.Extensions; using API.Services; using API.Services.Tasks; using Hangfire; +using Hangfire.Storage; using Kavita.Common; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; +using TaskScheduler = System.Threading.Tasks.TaskScheduler; namespace API.Controllers { @@ -29,10 +34,11 @@ namespace API.Controllers private readonly IStatsService _statsService; private readonly ICleanupService _cleanupService; private readonly IEmailService _emailService; + private readonly IBookmarkService _bookmarkService; public ServerController(IHostApplicationLifetime applicationLifetime, ILogger logger, IConfiguration config, IBackupService backupService, IArchiveService archiveService, IVersionUpdaterService versionUpdaterService, IStatsService statsService, - ICleanupService cleanupService, IEmailService emailService) + ICleanupService cleanupService, IEmailService emailService, IBookmarkService bookmarkService) { _applicationLifetime = applicationLifetime; _logger = logger; @@ -43,6 +49,7 @@ namespace API.Controllers _statsService = statsService; _cleanupService = cleanupService; _emailService = emailService; + _bookmarkService = bookmarkService; } /// @@ -76,11 +83,10 @@ namespace API.Controllers /// /// [HttpPost("backup-db")] - public async Task BackupDatabase() + public ActionResult BackupDatabase() { _logger.LogInformation("{UserName} is backing up database of server from admin dashboard", User.GetUsername()); - await _backupService.BackupDatabase(); - + RecurringJob.Trigger("backup"); return Ok(); } @@ -94,6 +100,17 @@ namespace API.Controllers return Ok(await _statsService.GetServerInfo()); } + /// + /// Triggers the scheduling of the convert bookmarks job. Only one job will run at a time. + /// + /// + [HttpPost("convert-bookmarks")] + public ActionResult ScheduleConvertBookmarks() + { + BackgroundJob.Enqueue(() => _bookmarkService.ConvertAllBookmarkToWebP()); + return Ok(); + } + [HttpGet("logs")] public async Task GetLogs() { @@ -134,5 +151,24 @@ namespace API.Controllers { return await _emailService.CheckIfAccessible(Request.Host.ToString()); } + + [HttpGet("jobs")] + public ActionResult> GetJobs() + { + var recurringJobs = Hangfire.JobStorage.Current.GetConnection().GetRecurringJobs().Select( + dto => + new JobDto() { + Id = dto.Id, + Title = dto.Id.Replace('-', ' '), + Cron = dto.Cron, + CreatedAt = dto.CreatedAt, + LastExecution = dto.LastExecution, + }); + + // For now, let's just do something simple + //var enqueuedJobs = JobStorage.Current.GetMonitoringApi().EnqueuedJobs("default", 0, int.MaxValue); + return Ok(recurringJobs); + + } } } diff --git a/API/Controllers/SettingsController.cs b/API/Controllers/SettingsController.cs index 88207bf3c..660ce0d12 100644 --- a/API/Controllers/SettingsController.cs +++ b/API/Controllers/SettingsController.cs @@ -169,6 +169,13 @@ namespace API.Controllers _unitOfWork.SettingsRepository.Update(setting); } + if (setting.Key == ServerSettingKey.ConvertBookmarkToWebP && updateSettingsDto.ConvertBookmarkToWebP + string.Empty != setting.Value) + { + setting.Value = updateSettingsDto.ConvertBookmarkToWebP + string.Empty; + _unitOfWork.SettingsRepository.Update(setting); + } + + if (setting.Key == ServerSettingKey.BookmarkDirectory && bookmarkDirectory != setting.Value) { // Validate new directory can be used diff --git a/API/Controllers/TachiyomiController.cs b/API/Controllers/TachiyomiController.cs new file mode 100644 index 000000000..b9eed8021 --- /dev/null +++ b/API/Controllers/TachiyomiController.cs @@ -0,0 +1,79 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using API.Data; +using API.Data.Repositories; +using API.DTOs; +using API.Entities; +using API.Extensions; +using API.Services; +using Microsoft.AspNetCore.Mvc; + +namespace API.Controllers; + +public class TachiyomiController : BaseApiController +{ + private readonly IUnitOfWork _unitOfWork; + private readonly IReaderService _readerService; + + public TachiyomiController(IUnitOfWork unitOfWork, IReaderService readerService) + { + _unitOfWork = unitOfWork; + _readerService = readerService; + } + + [HttpGet("latest-chapter")] + public async Task> GetLatestChapter(int seriesId) + { + var userId = await _unitOfWork.UserRepository.GetUserIdByUsernameAsync(User.GetUsername()); + + var currentChapter = await _readerService.GetContinuePoint(seriesId, userId); + + var prevChapterId = + await _readerService.GetPrevChapterIdAsync(seriesId, currentChapter.VolumeId, currentChapter.Id, userId); + + if (prevChapterId == -1) return null; + + var prevChapter = await _unitOfWork.ChapterRepository.GetChapterDtoAsync(prevChapterId); + + return Ok(prevChapter); + } + + /// + /// Marks every chapter that is sorted below the passed number as Read. This will not mark any specials as read. + /// + /// This is built for Tachiyomi and is not expected to be called by any other place + /// + [HttpPost("mark-chapter-until-as-read")] + public async Task> MarkChaptersUntilAsRead(int seriesId, float chapterNumber) + { + var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername(), AppUserIncludes.Progress); + user.Progresses ??= new List(); + + switch (chapterNumber) + { + // Tachiyomi sends chapter 0.0f when there's no chapters read. + // Due to the encoding for volumes this marks all chapters in volume 0 (loose chapters) as read so we ignore it + case 0.0f: + return true; + case < 1.0f: + { + // This is a hack to track volume number. We need to map it back by x100 + var volumeNumber = int.Parse($"{chapterNumber * 100f}"); + await _readerService.MarkVolumesUntilAsRead(user, seriesId, volumeNumber); + break; + } + default: + await _readerService.MarkChaptersUntilAsRead(user, seriesId, chapterNumber); + break; + } + + + _unitOfWork.UserRepository.Update(user); + + if (!_unitOfWork.HasChanges()) return Ok(true); + if (await _unitOfWork.CommitAsync()) return Ok(true); + + await _unitOfWork.RollbackAsync(); + return Ok(false); + } +} diff --git a/API/Controllers/UsersController.cs b/API/Controllers/UsersController.cs index 97dae76a4..4a1871604 100644 --- a/API/Controllers/UsersController.cs +++ b/API/Controllers/UsersController.cs @@ -6,6 +6,7 @@ using API.Data.Repositories; using API.DTOs; using API.Entities.Enums; using API.Extensions; +using API.SignalR; using AutoMapper; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; @@ -17,11 +18,13 @@ namespace API.Controllers { private readonly IUnitOfWork _unitOfWork; private readonly IMapper _mapper; + private readonly IEventHub _eventHub; - public UsersController(IUnitOfWork unitOfWork, IMapper mapper) + public UsersController(IUnitOfWork unitOfWork, IMapper mapper, IEventHub eventHub) { _unitOfWork = unitOfWork; _mapper = mapper; + _eventHub = eventHub; } [Authorize(Policy = "RequireAdminRole")] @@ -69,7 +72,9 @@ namespace API.Controllers [HttpPost("update-preferences")] public async Task> UpdatePreferences(UserPreferencesDto preferencesDto) { - var existingPreferences = await _unitOfWork.UserRepository.GetPreferencesAsync(User.GetUsername()); + var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername(), + AppUserIncludes.UserPreferences); + var existingPreferences = user.UserPreferences; existingPreferences.ReadingDirection = preferencesDto.ReadingDirection; existingPreferences.ScalingOption = preferencesDto.ScalingOption; @@ -98,6 +103,7 @@ namespace API.Controllers if (await _unitOfWork.CommitAsync()) { + await _eventHub.SendMessageToAsync(MessageFactory.UserUpdate, MessageFactory.UserUpdateEvent(user.Id, user.UserName), user.Id); return Ok(preferencesDto); } diff --git a/API/DTOs/Jobs/JobDto.cs b/API/DTOs/Jobs/JobDto.cs new file mode 100644 index 000000000..5af700528 --- /dev/null +++ b/API/DTOs/Jobs/JobDto.cs @@ -0,0 +1,24 @@ +using System; + +namespace API.DTOs.Jobs; + +public class JobDto +{ + /// + /// Job Id + /// + public string Id { get; set; } + /// + /// Human Readable title for the Job + /// + public string Title { get; set; } + /// + /// When the job was created + /// + public DateTime? CreatedAt { get; set; } + /// + /// Last time the job was run + /// + public DateTime? LastExecution { get; set; } + public string Cron { get; set; } +} diff --git a/API/DTOs/Settings/ServerSettingDTO.cs b/API/DTOs/Settings/ServerSettingDTO.cs index d3abaa313..dbdcea877 100644 --- a/API/DTOs/Settings/ServerSettingDTO.cs +++ b/API/DTOs/Settings/ServerSettingDTO.cs @@ -38,5 +38,7 @@ namespace API.DTOs.Settings /// If null or empty string, will default back to default install setting aka public string EmailServiceUrl { get; set; } public string InstallVersion { get; set; } + + public bool ConvertBookmarkToWebP { get; set; } } } diff --git a/API/Data/Seed.cs b/API/Data/Seed.cs index 4ecd27da5..bd13f7715 100644 --- a/API/Data/Seed.cs +++ b/API/Data/Seed.cs @@ -100,6 +100,7 @@ namespace API.Data new() {Key = ServerSettingKey.InstallVersion, Value = BuildInfo.Version.ToString()}, new() {Key = ServerSettingKey.BookmarkDirectory, Value = directoryService.BookmarkDirectory}, new() {Key = ServerSettingKey.EmailServiceUrl, Value = EmailService.DefaultApiUrl}, + new() {Key = ServerSettingKey.ConvertBookmarkToWebP, Value = "false"}, }.ToArray()); foreach (var defaultSetting in DefaultSettings) diff --git a/API/Entities/Enums/ServerSettingKey.cs b/API/Entities/Enums/ServerSettingKey.cs index 1a1ab8073..3a7b4597d 100644 --- a/API/Entities/Enums/ServerSettingKey.cs +++ b/API/Entities/Enums/ServerSettingKey.cs @@ -76,5 +76,10 @@ namespace API.Entities.Enums /// [Description("CustomEmailService")] EmailServiceUrl = 13, + /// + /// If Kavita should save bookmarks as WebP images + /// + [Description("ConvertBookmarkToWebP")] + ConvertBookmarkToWebP = 14, } } diff --git a/API/Helpers/Converters/ServerSettingConverter.cs b/API/Helpers/Converters/ServerSettingConverter.cs index 3678ef7e5..7d898d0e7 100644 --- a/API/Helpers/Converters/ServerSettingConverter.cs +++ b/API/Helpers/Converters/ServerSettingConverter.cs @@ -48,6 +48,9 @@ namespace API.Helpers.Converters case ServerSettingKey.InstallVersion: destination.InstallVersion = row.Value; break; + case ServerSettingKey.ConvertBookmarkToWebP: + destination.ConvertBookmarkToWebP = bool.Parse(row.Value); + break; } } diff --git a/API/Services/BookmarkService.cs b/API/Services/BookmarkService.cs index 4468a79a1..15f943178 100644 --- a/API/Services/BookmarkService.cs +++ b/API/Services/BookmarkService.cs @@ -8,6 +8,7 @@ using API.DTOs.Reader; using API.Entities; using API.Entities.Enums; using API.SignalR; +using Hangfire; using Microsoft.Extensions.Logging; namespace API.Services; @@ -18,6 +19,7 @@ public interface IBookmarkService Task BookmarkPage(AppUser userWithBookmarks, BookmarkDto bookmarkDto, string imageToBookmark); Task RemoveBookmarkPage(AppUser userWithBookmarks, BookmarkDto bookmarkDto); Task> GetBookmarkFilesById(IEnumerable bookmarkIds); + Task ConvertAllBookmarkToWebP(); } @@ -26,12 +28,17 @@ public class BookmarkService : IBookmarkService private readonly ILogger _logger; private readonly IUnitOfWork _unitOfWork; private readonly IDirectoryService _directoryService; + private readonly IImageService _imageService; + private readonly IEventHub _eventHub; - public BookmarkService(ILogger logger, IUnitOfWork unitOfWork, IDirectoryService directoryService) + public BookmarkService(ILogger logger, IUnitOfWork unitOfWork, + IDirectoryService directoryService, IImageService imageService, IEventHub eventHub) { _logger = logger; _unitOfWork = unitOfWork; _directoryService = directoryService; + _imageService = imageService; + _eventHub = eventHub; } /// @@ -87,18 +94,28 @@ public class BookmarkService : IBookmarkService var targetFolderStem = BookmarkStem(userWithBookmarks.Id, bookmarkDto.SeriesId, bookmarkDto.ChapterId); var targetFilepath = Path.Join(bookmarkDirectory, targetFolderStem); - userWithBookmarks.Bookmarks ??= new List(); - userWithBookmarks.Bookmarks.Add(new AppUserBookmark() + var bookmark = new AppUserBookmark() { Page = bookmarkDto.Page, VolumeId = bookmarkDto.VolumeId, SeriesId = bookmarkDto.SeriesId, ChapterId = bookmarkDto.ChapterId, FileName = Path.Join(targetFolderStem, fileInfo.Name) - }); + }; + _directoryService.CopyFileToDirectory(imageToBookmark, targetFilepath); + userWithBookmarks.Bookmarks ??= new List(); + userWithBookmarks.Bookmarks.Add(bookmark); + _unitOfWork.UserRepository.Update(userWithBookmarks); await _unitOfWork.CommitAsync(); + + var convertToWebP = bool.Parse((await _unitOfWork.SettingsRepository.GetSettingAsync(ServerSettingKey.ConvertBookmarkToWebP)).Value); + if (convertToWebP) + { + // Enqueue a task to convert the bookmark to webP + BackgroundJob.Enqueue(() => ConvertBookmarkToWebP(bookmark.Id)); + } } catch (Exception ex) { @@ -153,6 +170,94 @@ public class BookmarkService : IBookmarkService b.FileName))); } + /// + /// This is a long-running job that will convert all bookmarks into WebP. Do not invoke anyway except via Hangfire. + /// + [DisableConcurrentExecution(timeoutInSeconds: 2 * 60 * 60), AutomaticRetry(Attempts = 0)] + public async Task ConvertAllBookmarkToWebP() + { + var bookmarkDirectory = + (await _unitOfWork.SettingsRepository.GetSettingAsync(ServerSettingKey.BookmarkDirectory)).Value; + + await _eventHub.SendMessageAsync(MessageFactory.NotificationProgress, + MessageFactory.ConvertBookmarksProgressEvent(0F, ProgressEventType.Started)); + var bookmarks = (await _unitOfWork.UserRepository.GetAllBookmarksAsync()) + .Where(b => !b.FileName.EndsWith(".webp")).ToList(); + + var count = 1F; + foreach (var bookmark in bookmarks) + { + await SaveBookmarkAsWebP(bookmarkDirectory, bookmark); + await _unitOfWork.CommitAsync(); + await _eventHub.SendMessageAsync(MessageFactory.NotificationProgress, + MessageFactory.ConvertBookmarksProgressEvent(count / bookmarks.Count, ProgressEventType.Started)); + count++; + } + + await _eventHub.SendMessageAsync(MessageFactory.NotificationProgress, + MessageFactory.ConvertBookmarksProgressEvent(1F, ProgressEventType.Ended)); + + _logger.LogInformation("[BookmarkService] Converted bookmarks to WebP"); + } + + /// + /// This is a job that runs after a bookmark is saved + /// + public async Task ConvertBookmarkToWebP(int bookmarkId) + { + var bookmarkDirectory = + (await _unitOfWork.SettingsRepository.GetSettingAsync(ServerSettingKey.BookmarkDirectory)).Value; + var convertBookmarkToWebP = + (await _unitOfWork.SettingsRepository.GetSettingsDtoAsync()).ConvertBookmarkToWebP; + + if (!convertBookmarkToWebP) return; + + // Validate the bookmark still exists + var bookmark = await _unitOfWork.UserRepository.GetBookmarkAsync(bookmarkId); + if (bookmark == null) return; + + await SaveBookmarkAsWebP(bookmarkDirectory, bookmark); + await _unitOfWork.CommitAsync(); + } + + /// + /// Converts bookmark file, deletes original, marks bookmark as dirty. Does not commit. + /// + /// + /// + private async Task SaveBookmarkAsWebP(string bookmarkDirectory, AppUserBookmark bookmark) + { + var fullSourcePath = _directoryService.FileSystem.Path.Join(bookmarkDirectory, bookmark.FileName); + var fullTargetDirectory = fullSourcePath.Replace(new FileInfo(bookmark.FileName).Name, string.Empty); + var targetFolderStem = BookmarkStem(bookmark.AppUserId, bookmark.SeriesId, bookmark.ChapterId); + + _logger.LogDebug("Converting {Source} bookmark into WebP at {Target}", fullSourcePath, fullTargetDirectory); + + try + { + // Convert target file to webp then delete original target file and update bookmark + + var originalFile = bookmark.FileName; + try + { + var targetFile = await _imageService.ConvertToWebP(fullSourcePath, fullTargetDirectory); + var targetName = new FileInfo(targetFile).Name; + bookmark.FileName = Path.Join(targetFolderStem, targetName); + _directoryService.DeleteFiles(new[] {fullSourcePath}); + } + catch (Exception ex) + { + _logger.LogError(ex, "Could not convert file {FilePath}", bookmark.FileName); + bookmark.FileName = originalFile; + } + _unitOfWork.UserRepository.Update(bookmark); + } + catch (Exception ex) + { + _logger.LogError(ex, "Could not convert bookmark to WebP"); + } + } + private static string BookmarkStem(int userId, int seriesId, int chapterId) { return Path.Join($"{userId}", $"{seriesId}", $"{chapterId}"); diff --git a/API/Services/ImageService.cs b/API/Services/ImageService.cs index 6578b6f63..c050b6abf 100644 --- a/API/Services/ImageService.cs +++ b/API/Services/ImageService.cs @@ -1,7 +1,9 @@ using System; using System.IO; +using System.Threading.Tasks; using Microsoft.Extensions.Logging; -using NetVips; +using SixLabors.ImageSharp; +using Image = NetVips.Image; namespace API.Services; @@ -19,6 +21,12 @@ public interface IImageService string CreateThumbnailFromBase64(string encodedImage, string fileName); string WriteCoverThumbnail(Stream stream, string fileName, string outputDirectory); + /// + /// Converts the passed image to webP and outputs it in the same directory + /// + /// Full path to the image to convert + /// File of written webp image + Task ConvertToWebP(string filePath, string outputPath); } public class ImageService : IImageService @@ -95,6 +103,18 @@ public class ImageService : IImageService return filename; } + public async Task ConvertToWebP(string filePath, string outputPath) + { + var file = _directoryService.FileSystem.FileInfo.FromFileName(filePath); + var fileName = file.Name.Replace(file.Extension, string.Empty); + var outputFile = Path.Join(outputPath, fileName + ".webp"); + + + using var sourceImage = await SixLabors.ImageSharp.Image.LoadAsync(filePath); + await sourceImage.SaveAsWebpAsync(outputFile); + return outputFile; + } + /// public string CreateThumbnailFromBase64(string encodedImage, string fileName) diff --git a/API/Services/TaskScheduler.cs b/API/Services/TaskScheduler.cs index 585bec476..705c1010a 100644 --- a/API/Services/TaskScheduler.cs +++ b/API/Services/TaskScheduler.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using API.Data; @@ -6,6 +7,7 @@ using API.Entities.Enums; using API.Helpers.Converters; using API.Services.Tasks; using Hangfire; +using Hangfire.Storage; using Microsoft.Extensions.Logging; namespace API.Services; @@ -23,6 +25,8 @@ public interface ITaskScheduler void CancelStatsTasks(); Task RunStatCollection(); void ScanSiteThemes(); + + } public class TaskScheduler : ITaskScheduler { diff --git a/API/Services/Tasks/CleanupService.cs b/API/Services/Tasks/CleanupService.cs index 647c0a066..8b015d568 100644 --- a/API/Services/Tasks/CleanupService.cs +++ b/API/Services/Tasks/CleanupService.cs @@ -1,4 +1,5 @@ using System; +using System.IO; using System.Linq; using System.Threading.Tasks; using API.Data; @@ -19,7 +20,6 @@ namespace API.Services.Tasks Task DeleteChapterCoverImages(); Task DeleteTagCoverImages(); Task CleanupBackups(); - Task CleanupBookmarks(); } /// /// Cleans up after operations on reoccurring basis @@ -65,7 +65,6 @@ namespace API.Services.Tasks await SendProgress(0.7F, "Cleaning deleted cover images"); await DeleteTagCoverImages(); await DeleteReadingListCoverImages(); - await SendProgress(0.8F, "Cleaning deleted cover images"); await SendProgress(1F, "Cleanup finished"); _logger.LogInformation("Cleanup finished"); } @@ -152,7 +151,7 @@ namespace API.Services.Tasks /// public async Task CleanupBackups() { - const int dayThreshold = 30; + const int dayThreshold = 30; // TODO: We can make this a config option _logger.LogInformation("Beginning cleanup of Database backups at {Time}", DateTime.Now); var backupDirectory = (await _unitOfWork.SettingsRepository.GetSettingAsync(ServerSettingKey.BackupDirectory)).Value; @@ -176,39 +175,5 @@ namespace API.Services.Tasks } _logger.LogInformation("Finished cleanup of Database backups at {Time}", DateTime.Now); } - - /// - /// Removes all files in the BookmarkDirectory that don't currently have bookmarks in the Database - /// - public Task CleanupBookmarks() - { - // TODO: 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 - // var bookmarkDirectory = - // (await _unitOfWork.SettingsRepository.GetSettingAsync(ServerSettingKey.BookmarkDirectory)).Value; - // var allBookmarkFiles = _directoryService.GetFiles(bookmarkDirectory, searchOption: SearchOption.AllDirectories).Select(Parser.Parser.NormalizePath); - // var bookmarks = (await _unitOfWork.UserRepository.GetAllBookmarksAsync()) - // .Select(b => Parser.Parser.NormalizePath(_directoryService.FileSystem.Path.Join(bookmarkDirectory, - // b.FileName))); - // - // - // var filesToDelete = allBookmarkFiles.AsEnumerable().Except(bookmarks).ToList(); - // _logger.LogDebug("[Bookmarks] Bookmark cleanup wants to delete {Count} files", filesToDelete.Count); - // - // if (filesToDelete.Count == 0) return; - // - // _directoryService.DeleteFiles(filesToDelete); - // - // // Clear all empty directories - // 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); - // } - // } - } } } diff --git a/API/Services/Tasks/ScannerService.cs b/API/Services/Tasks/ScannerService.cs index 4bd37d009..fc40081ab 100644 --- a/API/Services/Tasks/ScannerService.cs +++ b/API/Services/Tasks/ScannerService.cs @@ -789,7 +789,7 @@ public class ScannerService : IScannerService // Update all the metadata on the Chapters foreach (var chapter in volume.Chapters) { - var firstFile = chapter.Files.OrderBy(x => x.Chapter).FirstOrDefault(); + var firstFile = chapter.Files.MinBy(x => x.Chapter); if (firstFile == null || _cacheHelper.HasFileNotChangedSinceCreationOrLastScan(chapter, false, firstFile)) continue; try { @@ -923,6 +923,7 @@ public class ScannerService : IScannerService } if (comicInfo == null) return; + _logger.LogDebug("[ScannerService] Read ComicInfo for {File}", firstFile.FilePath); chapter.AgeRating = ComicInfo.ConvertAgeRatingToEnum(comicInfo.AgeRating); diff --git a/API/SignalR/EventHub.cs b/API/SignalR/EventHub.cs index 50ba20ccf..3beba3d24 100644 --- a/API/SignalR/EventHub.cs +++ b/API/SignalR/EventHub.cs @@ -11,6 +11,7 @@ namespace API.SignalR; public interface IEventHub { Task SendMessageAsync(string method, SignalRMessage message, bool onlyAdmins = true); + Task SendMessageToAsync(string method, SignalRMessage message, int userId); } public class EventHub : IEventHub @@ -42,4 +43,17 @@ public class EventHub : IEventHub await users.SendAsync(method, message); } + + /// + /// Sends a message directly to a user if they are connected + /// + /// + /// + /// + /// + public async Task SendMessageToAsync(string method, SignalRMessage message, int userId) + { + var user = await _unitOfWork.UserRepository.GetUserByIdAsync(userId); + await _messageHub.Clients.User(user.UserName).SendAsync(method, message); + } } diff --git a/API/SignalR/MessageFactory.cs b/API/SignalR/MessageFactory.cs index be3ab0acf..030c69c99 100644 --- a/API/SignalR/MessageFactory.cs +++ b/API/SignalR/MessageFactory.cs @@ -73,6 +73,7 @@ namespace API.SignalR /// A type of event that has progress (determinate or indeterminate). /// The underlying event will have a name to give details on how to handle. /// + /// This is not an Event Name, it is used as the method only public const string NotificationProgress = "NotificationProgress"; /// /// Event sent out when Scan Loop is parsing a file @@ -94,6 +95,14 @@ namespace API.SignalR /// A user's progress was modified /// public const string UserProgressUpdate = "UserProgressUpdate"; + /// + /// A user's account or preferences were updated and UI needs to refresh to stay in sync + /// + public const string UserUpdate = "UserUpdate"; + /// + /// When bulk bookmarks are being converted + /// + public const string ConvertBookmarksProgress = "ConvertBookmarksProgress"; @@ -387,5 +396,37 @@ namespace API.SignalR } }; } + + public static SignalRMessage UserUpdateEvent(int userId, string userName) + { + return new SignalRMessage() + { + Name = UserUpdate, + Title = "User Update", + Progress = ProgressType.None, + Body = new + { + UserId = userId, + UserName = userName + } + }; + } + + public static SignalRMessage ConvertBookmarksProgressEvent(float progress, string eventType) + { + return new SignalRMessage() + { + Name = ConvertBookmarksProgress, + Title = "Converting Bookmarks to WebP", + SubTitle = string.Empty, + EventType = eventType, + Progress = ProgressType.Determinate, + Body = new + { + Progress = progress, + EventTime = DateTime.Now + } + }; + } } } diff --git a/API/SignalR/MessageHub.cs b/API/SignalR/MessageHub.cs index d4508db17..dd2e2b768 100644 --- a/API/SignalR/MessageHub.cs +++ b/API/SignalR/MessageHub.cs @@ -9,7 +9,6 @@ using Microsoft.AspNetCore.SignalR; namespace API.SignalR { - /// /// Generic hub for sending messages to UI /// @@ -17,32 +16,14 @@ namespace API.SignalR public class MessageHub : Hub { private readonly IPresenceTracker _tracker; - private static readonly HashSet Connections = new HashSet(); public MessageHub(IPresenceTracker tracker) { _tracker = tracker; } - public static bool IsConnected - { - get - { - lock (Connections) - { - return Connections.Count != 0; - } - } - } - public override async Task OnConnectedAsync() { - - lock (Connections) - { - Connections.Add(Context.ConnectionId); - } - await _tracker.UserConnected(Context.User.GetUsername(), Context.ConnectionId); var currentUsers = await PresenceTracker.GetOnlineUsers(); @@ -54,11 +35,6 @@ namespace API.SignalR public override async Task OnDisconnectedAsync(Exception exception) { - lock (Connections) - { - Connections.Remove(Context.ConnectionId); - } - await _tracker.UserDisconnected(Context.User.GetUsername(), Context.ConnectionId); var currentUsers = await PresenceTracker.GetOnlineUsers(); diff --git a/API/SignalR/Presence/PresenceTracker.cs b/API/SignalR/Presence/PresenceTracker.cs index 2d71b7302..45118aa8d 100644 --- a/API/SignalR/Presence/PresenceTracker.cs +++ b/API/SignalR/Presence/PresenceTracker.cs @@ -89,6 +89,7 @@ namespace API.SignalR.Presence public Task GetOnlineAdmins() { + // TODO: This might end in stale data, we want to get the online users, query against DB to check if they are admins then return string[] onlineUsers; lock (OnlineUsers) { @@ -107,7 +108,7 @@ namespace API.SignalR.Presence connectionIds = OnlineUsers.GetValueOrDefault(username)?.ConnectionIds; } - return Task.FromResult(connectionIds); + return Task.FromResult(connectionIds ?? new List()); } } } diff --git a/Kavita.Common/Kavita.Common.csproj b/Kavita.Common/Kavita.Common.csproj index 87e857175..7a5fe8f1c 100644 --- a/Kavita.Common/Kavita.Common.csproj +++ b/Kavita.Common/Kavita.Common.csproj @@ -9,10 +9,10 @@ - + - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/UI/Web/src/app/_models/events/user-update-event.ts b/UI/Web/src/app/_models/events/user-update-event.ts new file mode 100644 index 000000000..95db9f50b --- /dev/null +++ b/UI/Web/src/app/_models/events/user-update-event.ts @@ -0,0 +1,4 @@ +export interface UserUpdateEvent { + userId: number; + userName: string; +} \ No newline at end of file diff --git a/UI/Web/src/app/_models/job/job.ts b/UI/Web/src/app/_models/job/job.ts new file mode 100644 index 000000000..00ce0dffa --- /dev/null +++ b/UI/Web/src/app/_models/job/job.ts @@ -0,0 +1,7 @@ +export interface Job { + id: string; + title: string; + cron: string; + createdAt: string; + lastExecution: string; +} \ No newline at end of file diff --git a/UI/Web/src/app/_services/account.service.ts b/UI/Web/src/app/_services/account.service.ts index 6238c7b6a..e15249e5b 100644 --- a/UI/Web/src/app/_services/account.service.ts +++ b/UI/Web/src/app/_services/account.service.ts @@ -1,14 +1,15 @@ import { HttpClient } from '@angular/common/http'; import { Injectable, OnDestroy } from '@angular/core'; import { Observable, of, ReplaySubject, Subject } from 'rxjs'; -import { map, takeUntil } from 'rxjs/operators'; +import { filter, map, switchMap, takeUntil } from 'rxjs/operators'; import { environment } from 'src/environments/environment'; import { Preferences } from '../_models/preferences/preferences'; import { User } from '../_models/user'; import { Router } from '@angular/router'; -import { MessageHubService } from './message-hub.service'; +import { EVENTS, MessageHubService } from './message-hub.service'; import { ThemeService } from './theme.service'; import { InviteUserResponse } from '../_models/invite-user-response'; +import { UserUpdateEvent } from '../_models/events/user-update-event'; @Injectable({ providedIn: 'root' @@ -32,7 +33,12 @@ export class AccountService implements OnDestroy { private readonly onDestroy = new Subject(); constructor(private httpClient: HttpClient, private router: Router, - private messageHub: MessageHubService, private themeService: ThemeService) {} + private messageHub: MessageHubService, private themeService: ThemeService) { + messageHub.messages$.pipe(filter(evt => evt.event === EVENTS.UserUpdate), + map(evt => evt.payload as UserUpdateEvent), + switchMap(() => this.refreshToken())) + .subscribe(() => {}); + } ngOnDestroy(): void { this.onDestroy.next(); @@ -211,6 +217,7 @@ export class AccountService implements OnDestroy { private refreshToken() { if (this.currentUser === null || this.currentUser === undefined) return of(); + console.log('refreshing token and updating user account'); return this.httpClient.post<{token: string, refreshToken: string}>(this.baseUrl + 'account/refresh-token', {token: this.currentUser.token, refreshToken: this.currentUser.refreshToken}).pipe(map(user => { if (this.currentUser) { @@ -218,8 +225,7 @@ export class AccountService implements OnDestroy { this.currentUser.refreshToken = user.refreshToken; } - this.currentUserSource.next(this.currentUser); - this.startRefreshTokenTimer(); + this.setCurrentUser(this.currentUser); return user; })); } diff --git a/UI/Web/src/app/_services/message-hub.service.ts b/UI/Web/src/app/_services/message-hub.service.ts index 852d8a906..4eca9203e 100644 --- a/UI/Web/src/app/_services/message-hub.service.ts +++ b/UI/Web/src/app/_services/message-hub.service.ts @@ -7,6 +7,7 @@ import { environment } from 'src/environments/environment'; import { LibraryModifiedEvent } from '../_models/events/library-modified-event'; import { NotificationProgressEvent } from '../_models/events/notification-progress-event'; import { ThemeProgressEvent } from '../_models/events/theme-progress-event'; +import { UserUpdateEvent } from '../_models/events/user-update-event'; import { User } from '../_models/user'; export enum EVENTS { @@ -58,6 +59,14 @@ export enum EVENTS { * A user updates an entities read progress */ UserProgressUpdate = 'UserProgressUpdate', + /** + * A user updates account or preferences + */ + UserUpdate = 'UserUpdate', + /** + * When bulk bookmarks are being converted + */ + ConvertBookmarksProgress = 'ConvertBookmarksProgress', } export interface Message { @@ -139,6 +148,13 @@ export class MessageHubService { }); }); + this.hubConnection.on(EVENTS.ConvertBookmarksProgress, resp => { + this.messagesSource.next({ + event: EVENTS.ConvertBookmarksProgress, + payload: resp.body + }); + }); + this.hubConnection.on(EVENTS.LibraryModified, resp => { this.messagesSource.next({ event: EVENTS.LibraryModified, @@ -175,6 +191,14 @@ export class MessageHubService { }); }); + this.hubConnection.on(EVENTS.UserUpdate, resp => { + console.log('got UserUpdate', resp); + this.messagesSource.next({ + event: EVENTS.UserUpdate, + payload: resp.body as UserUpdateEvent + }); + }); + this.hubConnection.on(EVENTS.Error, resp => { this.messagesSource.next({ event: EVENTS.Error, diff --git a/UI/Web/src/app/_services/server.service.ts b/UI/Web/src/app/_services/server.service.ts index fcc94435c..0d51a9120 100644 --- a/UI/Web/src/app/_services/server.service.ts +++ b/UI/Web/src/app/_services/server.service.ts @@ -3,6 +3,7 @@ import { Injectable } from '@angular/core'; import { environment } from 'src/environments/environment'; import { ServerInfo } from '../admin/_models/server-info'; import { UpdateVersionEvent } from '../_models/events/update-version-event'; +import { Job } from '../_models/job/job'; @Injectable({ providedIn: 'root' @@ -40,4 +41,12 @@ export class ServerService { isServerAccessible() { return this.httpClient.get(this.baseUrl + 'server/accessible'); } + + getReoccuringJobs() { + return this.httpClient.get(this.baseUrl + 'server/jobs'); + } + + convertBookmarks() { + return this.httpClient.post(this.baseUrl + 'server/convert-bookmarks', {}); + } } diff --git a/UI/Web/src/app/admin/_models/server-settings.ts b/UI/Web/src/app/admin/_models/server-settings.ts index 101434a43..6a493a0aa 100644 --- a/UI/Web/src/app/admin/_models/server-settings.ts +++ b/UI/Web/src/app/admin/_models/server-settings.ts @@ -9,4 +9,5 @@ export interface ServerSettings { baseUrl: string; bookmarksDirectory: string; emailServiceUrl: string; + convertBookmarkToWebP: boolean; } diff --git a/UI/Web/src/app/admin/admin.module.ts b/UI/Web/src/app/admin/admin.module.ts index a76eefdf5..2b30cadd6 100644 --- a/UI/Web/src/app/admin/admin.module.ts +++ b/UI/Web/src/app/admin/admin.module.ts @@ -20,6 +20,9 @@ import { LibrarySelectorComponent } from './library-selector/library-selector.co import { EditUserComponent } from './edit-user/edit-user.component'; import { UserSettingsModule } from '../user-settings/user-settings.module'; import { SidenavModule } from '../sidenav/sidenav.module'; +import { ManageMediaSettingsComponent } from './manage-media-settings/manage-media-settings.component'; +import { ManageEmailSettingsComponent } from './manage-email-settings/manage-email-settings.component'; +import { ManageTasksSettingsComponent } from './manage-tasks-settings/manage-tasks-settings.component'; @@ -39,6 +42,9 @@ import { SidenavModule } from '../sidenav/sidenav.module'; RoleSelectorComponent, LibrarySelectorComponent, EditUserComponent, + ManageMediaSettingsComponent, + ManageEmailSettingsComponent, + ManageTasksSettingsComponent, ], imports: [ CommonModule, diff --git a/UI/Web/src/app/admin/dashboard/dashboard.component.html b/UI/Web/src/app/admin/dashboard/dashboard.component.html index bfbad082b..cdd77fd7d 100644 --- a/UI/Web/src/app/admin/dashboard/dashboard.component.html +++ b/UI/Web/src/app/admin/dashboard/dashboard.component.html @@ -8,18 +8,30 @@
  • {{ tab.title | sentenceCase }} - + - + + + + + + + - + - + + + + + + Nothing here yet. This will be built out in a future update. +
  • diff --git a/UI/Web/src/app/admin/dashboard/dashboard.component.ts b/UI/Web/src/app/admin/dashboard/dashboard.component.ts index d84b3ba26..097a3674b 100644 --- a/UI/Web/src/app/admin/dashboard/dashboard.component.ts +++ b/UI/Web/src/app/admin/dashboard/dashboard.component.ts @@ -5,7 +5,16 @@ import { ServerService } from 'src/app/_services/server.service'; import { Title } from '@angular/platform-browser'; import { NavService } from '../../_services/nav.service'; - +enum TabID { + General = '', + Email = 'email', + Media = 'media', + Users = 'users', + Libraries = 'libraries', + System = 'system', + Plugins = 'plugins', + Tasks = 'tasks' +} @Component({ selector: 'app-dashboard', @@ -15,14 +24,22 @@ import { NavService } from '../../_services/nav.service'; export class DashboardComponent implements OnInit { tabs: Array<{title: string, fragment: string}> = [ - {title: 'General', fragment: ''}, - {title: 'Users', fragment: 'users'}, - {title: 'Libraries', fragment: 'libraries'}, - {title: 'System', fragment: 'system'}, + {title: 'General', fragment: TabID.General}, + {title: 'Users', fragment: TabID.Users}, + {title: 'Libraries', fragment: TabID.Libraries}, + {title: 'Media', fragment: TabID.Media}, + {title: 'Email', fragment: TabID.Email}, + //{title: 'Plugins', fragment: TabID.Plugins}, + {title: 'Tasks', fragment: TabID.Tasks}, + {title: 'System', fragment: TabID.System}, ]; counter = this.tabs.length + 1; active = this.tabs[0]; + get TabID() { + return TabID; + } + constructor(public route: ActivatedRoute, private serverService: ServerService, private toastr: ToastrService, private titleService: Title, public navService: NavService) { this.route.fragment.subscribe(frag => { diff --git a/UI/Web/src/app/admin/manage-email-settings/manage-email-settings.component.html b/UI/Web/src/app/admin/manage-email-settings/manage-email-settings.component.html new file mode 100644 index 000000000..62dd94ea5 --- /dev/null +++ b/UI/Web/src/app/admin/manage-email-settings/manage-email-settings.component.html @@ -0,0 +1,29 @@ +
    +
    +

    Email Services (SMTP)

    +

    Kavita comes out of the box with an email service to power flows like invite user, forgot password, etc. Emails sent via our service are deleted immediately. You can use your own + email service. Set the url of the email service and use the Test button to ensure it works. At any time you can reset to ours. There is no way to disable emails although you are not required to use a + valid email address for users. Confirmation links will always be saved to logs. Emails will not be sent if you are not accessing Kavita via a publically reachable url. +

    +
    +   + Use fully qualified url of the email service. Do not include ending slash. + +
    + + + +
    +
    + +
    + + + +
    +
    +
    \ No newline at end of file diff --git a/UI/Web/src/app/admin/manage-email-settings/manage-email-settings.component.scss b/UI/Web/src/app/admin/manage-email-settings/manage-email-settings.component.scss new file mode 100644 index 000000000..e69de29bb diff --git a/UI/Web/src/app/admin/manage-email-settings/manage-email-settings.component.ts b/UI/Web/src/app/admin/manage-email-settings/manage-email-settings.component.ts new file mode 100644 index 000000000..94be4b793 --- /dev/null +++ b/UI/Web/src/app/admin/manage-email-settings/manage-email-settings.component.ts @@ -0,0 +1,78 @@ +import { Component, OnInit } from '@angular/core'; +import { FormControl, FormGroup, Validators } from '@angular/forms'; +import { ToastrService } from 'ngx-toastr'; +import { take } from 'rxjs'; +import { SettingsService, EmailTestResult } from '../settings.service'; +import { ServerSettings } from '../_models/server-settings'; + +@Component({ + selector: 'app-manage-email-settings', + templateUrl: './manage-email-settings.component.html', + styleUrls: ['./manage-email-settings.component.scss'] +}) +export class ManageEmailSettingsComponent implements OnInit { + + serverSettings!: ServerSettings; + settingsForm: FormGroup = new FormGroup({}); + + constructor(private settingsService: SettingsService, private toastr: ToastrService) { } + + ngOnInit(): void { + this.settingsService.getServerSettings().pipe(take(1)).subscribe((settings: ServerSettings) => { + this.serverSettings = settings; + this.settingsForm.addControl('emailServiceUrl', new FormControl(this.serverSettings.emailServiceUrl, [Validators.required])); + }); + } + + resetForm() { + this.settingsForm.get('emailServiceUrl')?.setValue(this.serverSettings.emailServiceUrl); + } + + async saveSettings() { + const modelSettings = Object.assign({}, this.serverSettings); + modelSettings.emailServiceUrl = this.settingsForm.get('emailServiceUrl')?.value; + + this.settingsService.updateServerSettings(modelSettings).pipe(take(1)).subscribe(async (settings: ServerSettings) => { + this.serverSettings = settings; + this.resetForm(); + this.toastr.success('Server settings updated'); + }, (err: any) => { + console.error('error: ', err); + }); + } + + resetToDefaults() { + this.settingsService.resetServerSettings().pipe(take(1)).subscribe(async (settings: ServerSettings) => { + this.serverSettings = settings; + this.resetForm(); + this.toastr.success('Server settings updated'); + }, (err: any) => { + console.error('error: ', err); + }); + } + + resetEmailServiceUrl() { + this.settingsService.resetEmailServerSettings().pipe(take(1)).subscribe(async (settings: ServerSettings) => { + this.serverSettings.emailServiceUrl = settings.emailServiceUrl; + this.resetForm(); + this.toastr.success('Email Service Reset'); + }, (err: any) => { + console.error('error: ', err); + }); + } + + testEmailServiceUrl() { + this.settingsService.testEmailServerSettings(this.settingsForm.get('emailServiceUrl')?.value || '').pipe(take(1)).subscribe(async (result: EmailTestResult) => { + if (result.successful) { + this.toastr.success('Email Service Url validated'); + } else { + this.toastr.error('Email Service Url did not respond. ' + result.errorMessage); + } + + }, (err: any) => { + console.error('error: ', err); + }); + + } + +} diff --git a/UI/Web/src/app/admin/manage-media-settings/manage-media-settings.component.html b/UI/Web/src/app/admin/manage-media-settings/manage-media-settings.component.html new file mode 100644 index 000000000..da280aeb7 --- /dev/null +++ b/UI/Web/src/app/admin/manage-media-settings/manage-media-settings.component.html @@ -0,0 +1,21 @@ +
    +
    +
    +
    +   + When saving bookmarks, covert them to WebP. WebP is not supported on Safari devices and will not render at all. WebP can drastically reduce space requirements for files. + +
    + + +
    +
    +
    + +
    + + + +
    +
    +
    \ No newline at end of file diff --git a/UI/Web/src/app/admin/manage-media-settings/manage-media-settings.component.scss b/UI/Web/src/app/admin/manage-media-settings/manage-media-settings.component.scss new file mode 100644 index 000000000..e69de29bb diff --git a/UI/Web/src/app/admin/manage-media-settings/manage-media-settings.component.ts b/UI/Web/src/app/admin/manage-media-settings/manage-media-settings.component.ts new file mode 100644 index 000000000..8c6f6664c --- /dev/null +++ b/UI/Web/src/app/admin/manage-media-settings/manage-media-settings.component.ts @@ -0,0 +1,53 @@ +import { Component, OnInit } from '@angular/core'; +import { FormGroup, FormControl, Validators } from '@angular/forms'; +import { ToastrService } from 'ngx-toastr'; +import { take } from 'rxjs'; +import { SettingsService } from '../settings.service'; +import { ServerSettings } from '../_models/server-settings'; + +@Component({ + selector: 'app-manage-media-settings', + templateUrl: './manage-media-settings.component.html', + styleUrls: ['./manage-media-settings.component.scss'] +}) +export class ManageMediaSettingsComponent implements OnInit { + + serverSettings!: ServerSettings; + settingsForm: FormGroup = new FormGroup({}); + + constructor(private settingsService: SettingsService, private toastr: ToastrService) { } + + ngOnInit(): void { + this.settingsService.getServerSettings().pipe(take(1)).subscribe((settings: ServerSettings) => { + this.serverSettings = settings; + this.settingsForm.addControl('convertBookmarkToWebP', new FormControl(this.serverSettings.convertBookmarkToWebP, [Validators.required])); + }); + } + + resetForm() { + this.settingsForm.get('convertBookmarkToWebP')?.setValue(this.serverSettings.convertBookmarkToWebP); + } + + async saveSettings() { + const modelSettings = Object.assign({}, this.serverSettings); + modelSettings.convertBookmarkToWebP = this.settingsForm.get('convertBookmarkToWebP')?.value; + + this.settingsService.updateServerSettings(modelSettings).pipe(take(1)).subscribe(async (settings: ServerSettings) => { + this.serverSettings = settings; + this.resetForm(); + this.toastr.success('Server settings updated'); + }, (err: any) => { + console.error('error: ', err); + }); + } + + resetToDefaults() { + this.settingsService.resetServerSettings().pipe(take(1)).subscribe(async (settings: ServerSettings) => { + this.serverSettings = settings; + this.resetForm(); + this.toastr.success('Server settings updated'); + }, (err: any) => { + console.error('error: ', err); + }); + } +} diff --git a/UI/Web/src/app/admin/manage-settings/manage-settings.component.html b/UI/Web/src/app/admin/manage-settings/manage-settings.component.html index be06070a1..c6b999671 100644 --- a/UI/Web/src/app/admin/manage-settings/manage-settings.component.html +++ b/UI/Web/src/app/admin/manage-settings/manage-settings.component.html @@ -10,7 +10,7 @@
      - Location where bookmarks will be stored. Bookmarks are source files and can be large. Choose a location with adequate storage. Directory is managed, other files within directory will be deleted. + Location where bookmarks will be stored. Bookmarks are source files and can be large. Choose a location with adequate storage. Directory is managed, other files within directory will be deleted. If docker, mount an additional volume and use that.
    @@ -40,13 +40,14 @@
    -

    Send anonymous usage and error information to Kavita's servers. This includes information on your browser, error reporting as well as OS and runtime version. We will use this information to prioritize features, bug fixes, and preformance tuning. Requires restart to take effect. See wiki for what is collected.

    +

    Send anonymous usage data to Kavita's servers. This includes information on certain features used, number of files, OS version, kavita install version, cpu and memory. We will use this information to prioritize features, bug fixes, and preformance tuning. Requires restart to take effect. See wiki for what is collected.

    +

    OPDS support will allow all users to use OPDS to read and download content from the server. If OPDS is enabled, a user will not need download permissions to download media while using it.

    @@ -55,46 +56,6 @@
    - -

    Email Services (SMTP)

    -

    Kavita comes out of the box with an email service to power flows like invite user, forgot password, etc. Emails sent via our service are deleted immediately. You can use your own - email service. Set the url of the email service and use the Test button to ensure it works. At any time you can reset to ours. There is no way to disable emails although confirmation links will always - be saved to logs. -

    -
    -   - Use fully qualified url of the email service. Do not include ending slash. - -
    - - - -
    -
    - - -

    Reoccuring Tasks

    -
    -   - How often Kavita will scan and refresh metadata around manga files. - How often Kavita will scan and refresh metatdata around manga files. - -
    - -
    -   - How often Kavita will backup the database. - How often Kavita will backup the database. - -
    diff --git a/UI/Web/src/app/admin/manage-settings/manage-settings.component.ts b/UI/Web/src/app/admin/manage-settings/manage-settings.component.ts index 5de5a0ab5..6be0935d0 100644 --- a/UI/Web/src/app/admin/manage-settings/manage-settings.component.ts +++ b/UI/Web/src/app/admin/manage-settings/manage-settings.component.ts @@ -93,28 +93,28 @@ export class ManageSettingsComponent implements OnInit { }); } - resetEmailServiceUrl() { - this.settingsService.resetEmailServerSettings().pipe(take(1)).subscribe(async (settings: ServerSettings) => { - this.serverSettings.emailServiceUrl = settings.emailServiceUrl; - this.resetForm(); - this.toastr.success('Email Service Reset'); - }, (err: any) => { - console.error('error: ', err); - }); - } + // resetEmailServiceUrl() { + // this.settingsService.resetEmailServerSettings().pipe(take(1)).subscribe(async (settings: ServerSettings) => { + // this.serverSettings.emailServiceUrl = settings.emailServiceUrl; + // this.resetForm(); + // this.toastr.success('Email Service Reset'); + // }, (err: any) => { + // console.error('error: ', err); + // }); + // } - testEmailServiceUrl() { - this.settingsService.testEmailServerSettings(this.settingsForm.get('emailServiceUrl')?.value || '').pipe(take(1)).subscribe(async (result: EmailTestResult) => { - if (result.successful) { - this.toastr.success('Email Service Url validated'); - } else { - this.toastr.error('Email Service Url did not respond. ' + result.errorMessage); - } + // testEmailServiceUrl() { + // this.settingsService.testEmailServerSettings(this.settingsForm.get('emailServiceUrl')?.value || '').pipe(take(1)).subscribe(async (result: EmailTestResult) => { + // if (result.successful) { + // this.toastr.success('Email Service Url validated'); + // } else { + // this.toastr.error('Email Service Url did not respond. ' + result.errorMessage); + // } - }, (err: any) => { - console.error('error: ', err); - }); + // }, (err: any) => { + // console.error('error: ', err); + // }); - } + // } } diff --git a/UI/Web/src/app/admin/manage-system/manage-system.component.html b/UI/Web/src/app/admin/manage-system/manage-system.component.html index 47032dfb3..68840a8d2 100644 --- a/UI/Web/src/app/admin/manage-system/manage-system.component.html +++ b/UI/Web/src/app/admin/manage-system/manage-system.component.html @@ -1,31 +1,4 @@
    - -
    -
    - -
    - - - - -
    -
    -
    -

    About System


    diff --git a/UI/Web/src/app/admin/manage-system/manage-system.component.ts b/UI/Web/src/app/admin/manage-system/manage-system.component.ts index 86c1f1599..ecab34faf 100644 --- a/UI/Web/src/app/admin/manage-system/manage-system.component.ts +++ b/UI/Web/src/app/admin/manage-system/manage-system.component.ts @@ -1,10 +1,7 @@ import { Component, OnInit } from '@angular/core'; import { FormControl, FormGroup, Validators } from '@angular/forms'; -import { NgbModal } from '@ng-bootstrap/ng-bootstrap'; import { ToastrService } from 'ngx-toastr'; -import { finalize, take, takeWhile } from 'rxjs/operators'; -import { UpdateNotificationModalComponent } from 'src/app/shared/update-notification/update-notification-modal.component'; -import { DownloadService } from 'src/app/shared/_services/download.service'; +import { take } from 'rxjs/operators'; import { ServerService } from 'src/app/_services/server.service'; import { SettingsService } from '../settings.service'; import { ServerInfo } from '../_models/server-info'; @@ -21,14 +18,9 @@ export class ManageSystemComponent implements OnInit { serverSettings!: ServerSettings; serverInfo!: ServerInfo; - clearCacheInProgress: boolean = false; - backupDBInProgress: boolean = false; - isCheckingForUpdate: boolean = false; - downloadLogsInProgress: boolean = false; constructor(private settingsService: SettingsService, private toastr: ToastrService, - private serverService: ServerService, public downloadService: DownloadService, - private modalService: NgbModal) { } + private serverService: ServerService) { } ngOnInit(): void { @@ -67,45 +59,4 @@ export class ManageSystemComponent implements OnInit { console.error('error: ', err); }); } - - clearCache() { - this.clearCacheInProgress = true; - this.serverService.clearCache().subscribe(res => { - this.clearCacheInProgress = false; - this.toastr.success('Cache has been cleared'); - }); - } - - backupDB() { - this.backupDBInProgress = true; - this.serverService.backupDatabase().subscribe(res => { - this.backupDBInProgress = false; - this.toastr.success('Database has been backed up'); - }); - } - - checkForUpdates() { - this.isCheckingForUpdate = true; - this.serverService.checkForUpdate().subscribe((update) => { - this.isCheckingForUpdate = false; - if (update === null) { - this.toastr.info('No updates available'); - return; - } - const modalRef = this.modalService.open(UpdateNotificationModalComponent, { scrollable: true, size: 'lg' }); - modalRef.componentInstance.updateData = update; - }); - } - - downloadLogs() { - this.downloadLogsInProgress = true; - this.downloadService.downloadLogs().pipe( - takeWhile(val => { - return val.state != 'DONE'; - }), - finalize(() => { - this.downloadLogsInProgress = false; - })).subscribe(() => {/* No Operation */}); - } - } diff --git a/UI/Web/src/app/admin/manage-tasks-settings/manage-tasks-settings.component.html b/UI/Web/src/app/admin/manage-tasks-settings/manage-tasks-settings.component.html new file mode 100644 index 000000000..c6d22af31 --- /dev/null +++ b/UI/Web/src/app/admin/manage-tasks-settings/manage-tasks-settings.component.html @@ -0,0 +1,73 @@ +
    +
    +

    Reoccuring Tasks

    +
    +   + How often Kavita will scan and refresh metadata around manga files. + How often Kavita will scan and refresh metatdata around manga files. + +
    + +
    +   + How often Kavita will backup the database. + How often Kavita will backup the database. + +
    + +

    Ad-hoc Tasks

    + + + + + + + + + + + + + + + +
    Job TitleDescriptionAction
    + {{task.name}} + + {{task.description}} + + +
    + +

    Reoccuring Tasks

    + + + + + + + + + + + + + + + +
    Job TitleLast ExecutedCron
    + {{task.title | titlecase}} + {{task.lastExecution | date:'short' | defaultValue }}{{task.cron}}
    + + +
    + + + +
    +
    +
    \ No newline at end of file diff --git a/UI/Web/src/app/admin/manage-tasks-settings/manage-tasks-settings.component.scss b/UI/Web/src/app/admin/manage-tasks-settings/manage-tasks-settings.component.scss new file mode 100644 index 000000000..3fe4d1b01 --- /dev/null +++ b/UI/Web/src/app/admin/manage-tasks-settings/manage-tasks-settings.component.scss @@ -0,0 +1,3 @@ +.table { + background-color: lightgrey; +} \ No newline at end of file diff --git a/UI/Web/src/app/admin/manage-tasks-settings/manage-tasks-settings.component.ts b/UI/Web/src/app/admin/manage-tasks-settings/manage-tasks-settings.component.ts new file mode 100644 index 000000000..56c3edca4 --- /dev/null +++ b/UI/Web/src/app/admin/manage-tasks-settings/manage-tasks-settings.component.ts @@ -0,0 +1,151 @@ +import { Component, OnInit } from '@angular/core'; +import { FormGroup, FormControl, Validators } from '@angular/forms'; +import { ToastrService } from 'ngx-toastr'; +import { ConfirmService } from 'src/app/shared/confirm.service'; +import { SettingsService } from '../settings.service'; +import { ServerSettings } from '../_models/server-settings'; +import { catchError, finalize, shareReplay, take, takeWhile } from 'rxjs/operators'; +import { forkJoin, Observable, of } from 'rxjs'; +import { ServerService } from 'src/app/_services/server.service'; +import { Job } from 'src/app/_models/job/job'; +import { UpdateNotificationModalComponent } from 'src/app/shared/update-notification/update-notification-modal.component'; +import { NgbModal } from '@ng-bootstrap/ng-bootstrap'; +import { DownloadService } from 'src/app/shared/_services/download.service'; + +interface AdhocTask { + name: string; + description: string; + api: Observable; + successMessage: string; + successFunction?: (data: any) => void; +} + +@Component({ + selector: 'app-manage-tasks-settings', + templateUrl: './manage-tasks-settings.component.html', + styleUrls: ['./manage-tasks-settings.component.scss'] +}) +export class ManageTasksSettingsComponent implements OnInit { + + serverSettings!: ServerSettings; + settingsForm: FormGroup = new FormGroup({}); + taskFrequencies: Array = []; + logLevels: Array = []; + + reoccuringTasks$: Observable> = of([]); + adhocTasks: Array = [ + { + name: 'Convert Bookmarks to WebP', + description: 'Runs a long-running task which will convert all bookmarks to WebP. This is slow (especially on ARM devices).', + api: this.serverService.convertBookmarks(), + successMessage: 'Conversion of Bookmarks has been queued' + }, + { + name: 'Clear Cache', + description: 'Clears cached files for reading. Usefull when you\'ve just updated a file that you were previously reading within last 24 hours.', + api: this.serverService.clearCache(), + successMessage: 'Cache has been cleared' + }, + { + name: 'Backup Database', + description: 'Takes a backup of the database, bookmarks, themes, manually uploaded covers, and config files', + api: this.serverService.backupDatabase(), + successMessage: 'A job to backup the database has been queued' + }, + { + name: 'Download Logs', + description: 'Compiles all log files into a zip and downloads it', + api: this.downloadService.downloadLogs().pipe( + takeWhile(val => { + return val.state != 'DONE'; + })), + successMessage: '' + }, + { + name: 'Check for Updates', + description: 'See if there are any Stable releases ahead of your version', + api: this.serverService.checkForUpdate(), + successMessage: '', + successFunction: (update) => { + if (update === null) { + this.toastr.info('No updates available'); + return; + } + const modalRef = this.modalService.open(UpdateNotificationModalComponent, { scrollable: true, size: 'lg' }); + modalRef.componentInstance.updateData = update; + } + }, + ]; + + constructor(private settingsService: SettingsService, private toastr: ToastrService, + private serverService: ServerService, private modalService: NgbModal, + private downloadService: DownloadService) { } + + ngOnInit(): void { + forkJoin({ + frequencies: this.settingsService.getTaskFrequencies(), + levels: this.settingsService.getLoggingLevels(), + settings: this.settingsService.getServerSettings() + } + + ).subscribe(result => { + this.taskFrequencies = result.frequencies; + this.logLevels = result.levels; + this.serverSettings = result.settings; + this.settingsForm.addControl('taskScan', new FormControl(this.serverSettings.taskScan, [Validators.required])); + this.settingsForm.addControl('taskBackup', new FormControl(this.serverSettings.taskBackup, [Validators.required])); + }); + + this.reoccuringTasks$ = this.serverService.getReoccuringJobs().pipe(shareReplay()); + } + + resetForm() { + this.settingsForm.get('taskScan')?.setValue(this.serverSettings.taskScan); + this.settingsForm.get('taskBackup')?.setValue(this.serverSettings.taskBackup); + } + + async saveSettings() { + const modelSettings = Object.assign({}, this.serverSettings); + modelSettings.taskBackup = this.settingsForm.get('taskBackup')?.value; + modelSettings.taskScan = this.settingsForm.get('taskScan')?.value; + + this.settingsService.updateServerSettings(modelSettings).pipe(take(1)).subscribe(async (settings: ServerSettings) => { + this.serverSettings = settings; + this.resetForm(); + this.reoccuringTasks$ = this.serverService.getReoccuringJobs().pipe(shareReplay()); + this.toastr.success('Server settings updated'); + }, (err: any) => { + console.error('error: ', err); + }); + } + + resetToDefaults() { + this.settingsService.resetServerSettings().pipe(take(1)).subscribe(async (settings: ServerSettings) => { + this.serverSettings = settings; + this.resetForm(); + this.toastr.success('Server settings updated'); + }, (err: any) => { + console.error('error: ', err); + }); + } + + runAdhocConvert() { + this.serverService.convertBookmarks().subscribe(() => { + this.toastr.success('Conversion of Bookmarks has been queued.'); + }); + } + + runAdhoc(task: AdhocTask) { + task.api.subscribe((data: any) => { + if (task.successMessage.length > 0) { + this.toastr.success(task.successMessage); + } + + if (task.successFunction) { + task.successFunction(data); + } + }); + } + + +} diff --git a/UI/Web/src/app/cards/card-detail-layout/card-detail-layout.component.html b/UI/Web/src/app/cards/card-detail-layout/card-detail-layout.component.html index 8e7ae79f1..f412c16aa 100644 --- a/UI/Web/src/app/cards/card-detail-layout/card-detail-layout.component.html +++ b/UI/Web/src/app/cards/card-detail-layout/card-detail-layout.component.html @@ -39,11 +39,10 @@
    - -

    - -

    +

    + +

    diff --git a/UI/Web/src/app/pipe/default-value.pipe.ts b/UI/Web/src/app/pipe/default-value.pipe.ts new file mode 100644 index 000000000..1c9cf3761 --- /dev/null +++ b/UI/Web/src/app/pipe/default-value.pipe.ts @@ -0,0 +1,13 @@ +import { Pipe, PipeTransform } from '@angular/core'; + +@Pipe({ + name: 'defaultValue' +}) +export class DefaultValuePipe implements PipeTransform { + + transform(value: any): string { + if (value === null || value === undefined || value === '' || value === Infinity || value === NaN || value === {}) return '—'; + return value; + } + +} diff --git a/UI/Web/src/app/pipe/pipe.module.ts b/UI/Web/src/app/pipe/pipe.module.ts index 7e2a29659..c7befee48 100644 --- a/UI/Web/src/app/pipe/pipe.module.ts +++ b/UI/Web/src/app/pipe/pipe.module.ts @@ -6,6 +6,7 @@ import { SentenceCasePipe } from './sentence-case.pipe'; import { PersonRolePipe } from './person-role.pipe'; import { SafeHtmlPipe } from './safe-html.pipe'; import { RelationshipPipe } from './relationship.pipe'; +import { DefaultValuePipe } from './default-value.pipe'; @@ -16,7 +17,8 @@ import { RelationshipPipe } from './relationship.pipe'; PublicationStatusPipe, SentenceCasePipe, SafeHtmlPipe, - RelationshipPipe + RelationshipPipe, + DefaultValuePipe ], imports: [ CommonModule, @@ -27,7 +29,8 @@ import { RelationshipPipe } from './relationship.pipe'; PublicationStatusPipe, SentenceCasePipe, SafeHtmlPipe, - RelationshipPipe + RelationshipPipe, + DefaultValuePipe ] }) export class PipeModule { } diff --git a/UI/Web/src/app/user-settings/user-preferences/user-preferences.component.html b/UI/Web/src/app/user-settings/user-preferences/user-preferences.component.html index 40ba2c9ad..77cee3e9a 100644 --- a/UI/Web/src/app/user-settings/user-preferences/user-preferences.component.html +++ b/UI/Web/src/app/user-settings/user-preferences/user-preferences.component.html @@ -227,7 +227,8 @@ - + +

    Change your Password