using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using AutoMapper;
using Kavita.API.Attributes;
using Kavita.API.Database;
using Kavita.API.Services;
using Kavita.Common;
using Kavita.Models.Constants;
using Kavita.Models.DTOs.Font;
using Kavita.Models.Entities.Enums.Font;
using Kavita.Server.Attributes;
using Kavita.Services.Scanner;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace Kavita.Server.Controllers;
[Authorize]
public class FontController(
IUnitOfWork unitOfWork,
IDirectoryService directoryService,
IFontService fontService,
IMapper mapper,
ILocalizationService localizationService)
: BaseApiController
{
private readonly Regex _fontFileExtensionRegex = new(Parser.FontFileExtensions, RegexOptions.IgnoreCase, Parser.RegexTimeout);
///
/// List out the fonts
///
///
[HttpGet("all")]
public async Task>> GetFonts()
{
return Ok(await unitOfWork.EpubFontRepository.GetFontDtosAsync());
}
///
/// Returns a font file
///
///
///
[HttpGet]
[SkipDeviceTracking]
public async Task GetFont(int fontId)
{
var font = await unitOfWork.EpubFontRepository.GetFontAsync(fontId);
if (font == null) return NotFound();
if (font.Provider == FontProvider.System) return BadRequest("System provided fonts are not loaded by API");
var path = Path.Join(directoryService.EpubFontDirectory, font.FileName);
return CachedFile(path);
}
///
/// Removes a font from the system
///
///
/// If the font is in use by other users and an admin wants it deleted, they must confirm to force delete it. This is prompted in the UI.
///
[HttpDelete]
[DisallowRole(PolicyConstants.ReadOnlyRole)]
public async Task DeleteFont(int fontId, bool force = false)
{
var forceDelete = User.IsInRole(PolicyConstants.AdminRole) && force;
var fontInUse = await fontService.IsFontInUse(fontId);
if (!fontInUse || forceDelete)
{
await fontService.Delete(fontId);
}
return Ok();
}
///
/// Returns if the given font is in use by any other user. System provided fonts will always return true.
///
///
///
[HttpGet("in-use")]
public async Task> IsFontInUse(int fontId)
{
return Ok(await fontService.IsFontInUse(fontId));
}
///
/// Manual upload
///
///
///
[HttpPost("upload")]
[DisallowRole(PolicyConstants.ReadOnlyRole)]
public async Task> UploadFont(IFormFile formFile)
{
if (!_fontFileExtensionRegex.IsMatch(Path.GetExtension(formFile.FileName))) 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(font));
}
[HttpPost("upload-by-url")]
[DisallowRole(PolicyConstants.ReadOnlyRole)]
public async Task UploadFontByUrl([FromQuery]string url)
{
// Validate url
try
{
var font = await fontService.CreateFontFromUrl(url);
return Ok(mapper.Map(font));
}
catch (KavitaException ex)
{
return BadRequest(localizationService.Translate(UserId, ex.Message));
}
}
private async Task 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;
}
}