Refactored how system fonts were loaded (at least covered for local development) so that instead of going through the api, they are instead resolved by using assets/fonts/{fontName}/{fontFile}.
This commit is contained in:
parent
9fae799c63
commit
58800c0b4e
88 changed files with 177 additions and 97 deletions
|
|
@ -6,18 +6,15 @@ using System.Threading.Tasks;
|
|||
using API.Constants;
|
||||
using API.Data;
|
||||
using API.DTOs.Font;
|
||||
using API.Extensions;
|
||||
using API.Entities.Enums.Font;
|
||||
using API.Services;
|
||||
using API.Services.Tasks;
|
||||
using API.Services.Tasks.Scanner.Parser;
|
||||
using AutoMapper;
|
||||
using Kavita.Common;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using MimeTypes;
|
||||
using Serilog;
|
||||
|
||||
namespace API.Controllers;
|
||||
|
||||
|
|
@ -25,72 +22,98 @@ public class FontController : BaseApiController
|
|||
{
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly IDirectoryService _directoryService;
|
||||
private readonly ITaskScheduler _taskScheduler;
|
||||
private readonly IFontService _fontService;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
private readonly Regex _fontFileExtensionRegex = new(Parser.FontFileExtensions, RegexOptions.IgnoreCase, Parser.RegexTimeout);
|
||||
|
||||
public FontController(IUnitOfWork unitOfWork, ITaskScheduler taskScheduler, IDirectoryService directoryService,
|
||||
public FontController(IUnitOfWork unitOfWork, IDirectoryService directoryService,
|
||||
IFontService fontService, IMapper mapper)
|
||||
{
|
||||
_unitOfWork = unitOfWork;
|
||||
_directoryService = directoryService;
|
||||
_taskScheduler = taskScheduler;
|
||||
_fontService = fontService;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
[ResponseCache(CacheProfileName = "10Minute")]
|
||||
/// <summary>
|
||||
/// List out the fonts
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[ResponseCache(CacheProfileName = ResponseCacheProfiles.TenMinute)]
|
||||
[HttpGet("all")]
|
||||
public async Task<ActionResult<IEnumerable<EpubFontDto>>> GetFonts()
|
||||
{
|
||||
return Ok(await _unitOfWork.EpubFontRepository.GetFontDtosAsync());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a font
|
||||
/// </summary>
|
||||
/// <param name="fontId"></param>
|
||||
/// <param name="apiKey"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
[AllowAnonymous]
|
||||
public async Task<IActionResult> GetFont(int fontId, string apiKey)
|
||||
{
|
||||
var userId = await _unitOfWork.UserRepository.GetUserIdByApiKeyAsync(apiKey);
|
||||
|
||||
if (userId == 0) return BadRequest();
|
||||
|
||||
var font = await _unitOfWork.EpubFontRepository.GetFontAsync(fontId);
|
||||
|
||||
if (font == null) return NotFound();
|
||||
|
||||
// var fontDirectory = _directoryService.EpubFontDirectory;
|
||||
// if (font.Provider == FontProvider.System)
|
||||
// {
|
||||
// fontDirectory = _directoryService.
|
||||
// }
|
||||
|
||||
var contentType = MimeTypeMap.GetMimeType(Path.GetExtension(font.FileName));
|
||||
var path = Path.Join(_directoryService.EpubFontDirectory, font.FileName);
|
||||
|
||||
return PhysicalFile(path, contentType);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes a font from the system
|
||||
/// </summary>
|
||||
/// <param name="fontId"></param>
|
||||
/// <param name="confirmed">If the font is in use by other users and an admin wants it deleted, they must confirm to force delete it</param>
|
||||
/// <returns></returns>
|
||||
[HttpDelete]
|
||||
public async Task<IActionResult> DeleteFont(int fontId)
|
||||
public async Task<IActionResult> DeleteFont(int fontId, bool confirmed = false)
|
||||
{
|
||||
// TODO: We need to check if this font is used by anyone else and if so, need to inform the user
|
||||
// Need to check if this is a system font as well
|
||||
var forceDelete = User.IsInRole(PolicyConstants.AdminRole) && confirmed;
|
||||
await _fontService.Delete(fontId);
|
||||
return Ok();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Manual upload
|
||||
/// </summary>
|
||||
/// <param name="formFile"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost("upload")]
|
||||
public async Task<ActionResult<EpubFontDto>> UploadFont(IFormFile formFile)
|
||||
{
|
||||
if (!_fontFileExtensionRegex.IsMatch(Path.GetExtension(formFile.FileName)))
|
||||
return BadRequest("Invalid file");
|
||||
if (!_fontFileExtensionRegex.IsMatch(Path.GetExtension(formFile.FileName))) return BadRequest("Invalid file");
|
||||
|
||||
if (formFile.FileName.Contains("..")) return BadRequest("Invalid file");
|
||||
|
||||
if (formFile.FileName.Contains(".."))
|
||||
return BadRequest("Invalid file");
|
||||
|
||||
var tempFile = await UploadToTemp(formFile);
|
||||
var font = await _fontService.CreateFontFromFileAsync(tempFile);
|
||||
return Ok(_mapper.Map<EpubFontDto>(font));
|
||||
}
|
||||
|
||||
[HttpPost("upload-url")]
|
||||
public async Task<ActionResult<EpubFontDto>> UploadFontByUrl(string url)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
// [HttpPost("upload-url")]
|
||||
// public async Task<ActionResult<EpubFontDto>> UploadFontByUrl(string url)
|
||||
// {
|
||||
// throw new NotImplementedException();
|
||||
// }
|
||||
|
||||
private async Task<string> UploadToTemp(IFormFile file)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -40,7 +40,7 @@ public class ThemeController : BaseApiController
|
|||
_mapper = mapper;
|
||||
}
|
||||
|
||||
[ResponseCache(CacheProfileName = "10Minute")]
|
||||
[ResponseCache(CacheProfileName = ResponseCacheProfiles.TenMinute)]
|
||||
[AllowAnonymous]
|
||||
[HttpGet]
|
||||
public async Task<ActionResult<IEnumerable<SiteThemeDto>>> GetThemes()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue