Theme Viewer + Theme Updater (#2952)

This commit is contained in:
Joe Milazzo 2024-05-13 17:00:13 -05:00 committed by GitHub
parent 24302d4fcc
commit 38e7c1c131
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
35 changed files with 4563 additions and 284 deletions

View file

@ -1,13 +1,21 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using API.Constants;
using API.Data;
using API.DTOs.Theme;
using API.Entities;
using API.Extensions;
using API.Services;
using API.Services.Tasks;
using AutoMapper;
using Kavita.Common;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
namespace API.Controllers;
@ -17,16 +25,19 @@ public class ThemeController : BaseApiController
{
private readonly IUnitOfWork _unitOfWork;
private readonly IThemeService _themeService;
private readonly ITaskScheduler _taskScheduler;
private readonly ILocalizationService _localizationService;
private readonly IDirectoryService _directoryService;
private readonly IMapper _mapper;
public ThemeController(IUnitOfWork unitOfWork, IThemeService themeService, ITaskScheduler taskScheduler,
ILocalizationService localizationService)
public ThemeController(IUnitOfWork unitOfWork, IThemeService themeService,
ILocalizationService localizationService, IDirectoryService directoryService, IMapper mapper)
{
_unitOfWork = unitOfWork;
_themeService = themeService;
_taskScheduler = taskScheduler;
_localizationService = localizationService;
_directoryService = directoryService;
_mapper = mapper;
}
[ResponseCache(CacheProfileName = "10Minute")]
@ -37,13 +48,6 @@ public class ThemeController : BaseApiController
return Ok(await _unitOfWork.SiteThemeRepository.GetThemeDtos());
}
[Authorize("RequireAdminRole")]
[HttpPost("scan")]
public ActionResult Scan()
{
_taskScheduler.ScanSiteThemes();
return Ok();
}
[Authorize("RequireAdminRole")]
[HttpPost("update-default")]
@ -78,4 +82,68 @@ public class ThemeController : BaseApiController
return BadRequest(await _localizationService.Get("en", ex.Message));
}
}
/// <summary>
/// Browse themes that can be used on this server
/// </summary>
/// <returns></returns>
[ResponseCache(CacheProfileName = ResponseCacheProfiles.Hour)]
[HttpGet("browse")]
public async Task<ActionResult<IEnumerable<DownloadableSiteThemeDto>>> BrowseThemes()
{
var themes = await _themeService.GetDownloadableThemes();
return Ok(themes.Where(t => !t.AlreadyDownloaded));
}
/// <summary>
/// Attempts to delete a theme. If already in use by users, will not allow
/// </summary>
/// <param name="themeId"></param>
/// <returns></returns>
[HttpDelete]
public async Task<ActionResult<IEnumerable<DownloadableSiteThemeDto>>> DeleteTheme(int themeId)
{
await _themeService.DeleteTheme(themeId);
return Ok();
}
/// <summary>
/// Downloads a SiteTheme from upstream
/// </summary>
/// <param name="dto"></param>
/// <returns></returns>
[HttpPost("download-theme")]
public async Task<ActionResult<SiteThemeDto>> DownloadTheme(DownloadableSiteThemeDto dto)
{
return Ok(_mapper.Map<SiteThemeDto>(await _themeService.DownloadRepoTheme(dto)));
}
/// <summary>
/// Uploads a new theme file
/// </summary>
/// <param name="formFile"></param>
/// <returns></returns>
[HttpPost("upload-theme")]
public async Task<ActionResult<SiteThemeDto>> DownloadTheme(IFormFile formFile)
{
if (!formFile.FileName.EndsWith(".css")) return BadRequest("Invalid file");
if (formFile.FileName.Contains("..")) return BadRequest("Invalid file");
var tempFile = await UploadToTemp(formFile);
// Set summary as "Uploaded by User.GetUsername() on DATE"
var theme = await _themeService.CreateThemeFromFile(tempFile, User.GetUsername());
return Ok(_mapper.Map<SiteThemeDto>(theme));
}
private async Task<string> UploadToTemp(IFormFile file)
{
var outputFile = Path.Join(_directoryService.TempDirectory, file.FileName);
await using var stream = System.IO.File.Create(outputFile);
await file.CopyToAsync(stream);
stream.Close();
return outputFile;
}
}

View file

@ -391,4 +391,6 @@ public class UploadController : BaseApiController
return BadRequest(await _localizationService.Translate(User.GetUserId(), "reset-chapter-lock"));
}
}

View file

@ -122,9 +122,10 @@ public class UsersController : BaseApiController
existingPreferences.PdfScrollMode = preferencesDto.PdfScrollMode;
existingPreferences.PdfSpreadMode = preferencesDto.PdfSpreadMode;
if (existingPreferences.Theme.Id != preferencesDto.Theme?.Id)
if (preferencesDto.Theme != null && existingPreferences.Theme.Id != preferencesDto.Theme?.Id)
{
existingPreferences.Theme = preferencesDto.Theme ?? await _unitOfWork.SiteThemeRepository.GetDefaultTheme();
var theme = await _unitOfWork.SiteThemeRepository.GetTheme(preferencesDto.Theme!.Id);
existingPreferences.Theme = theme ?? await _unitOfWork.SiteThemeRepository.GetDefaultTheme();
}