Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Bring your own font #3020

Draft
wants to merge 22 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions API/API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@
</ItemGroup>

<ItemGroup>
<Folder Include="config\fonts\" />
<Folder Include="config\themes" />
<Content Include="EmailTemplates\**">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
Expand Down
72 changes: 72 additions & 0 deletions API/Controllers/FontController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using API.Data;
using API.DTOs.Font;
using API.Services;
using API.Services.Tasks;
using Kavita.Common;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace API.Controllers;

public class FontController : BaseApiController
{
private readonly IUnitOfWork _unitOfWork;
private readonly IFontService _fontService;
private readonly ITaskScheduler _taskScheduler;

public FontController(IUnitOfWork unitOfWork, IFontService fontService, ITaskScheduler taskScheduler)
{
_unitOfWork = unitOfWork;
_fontService = fontService;
_taskScheduler = taskScheduler;
}

[ResponseCache(CacheProfileName = "10Minute")]
[AllowAnonymous]
[HttpGet("GetFonts")]
Fesaa marked this conversation as resolved.
Show resolved Hide resolved
public async Task<ActionResult<IEnumerable<EpubFontDto>>> GetFonts()
{
return Ok(await _unitOfWork.EpubFontRepository.GetFontDtos());
}

[AllowAnonymous]
[HttpGet("download-font")]
Fesaa marked this conversation as resolved.
Show resolved Hide resolved
public async Task<IActionResult> GetFont(int fontId)
{
try
{
var font = await _unitOfWork.EpubFontRepository.GetFont(fontId);
Fesaa marked this conversation as resolved.
Show resolved Hide resolved
if (font == null) return NotFound();
Fesaa marked this conversation as resolved.
Show resolved Hide resolved
var contentType = GetContentType(font.FileName);
return File(await _fontService.GetContent(fontId), contentType, font.FileName);
Fesaa marked this conversation as resolved.
Show resolved Hide resolved
Fesaa marked this conversation as resolved.
Show resolved Hide resolved
}
catch (KavitaException ex)
{
return BadRequest(ex.Message);
Fesaa marked this conversation as resolved.
Show resolved Hide resolved
}
}

[AllowAnonymous]
Fesaa marked this conversation as resolved.
Show resolved Hide resolved
[HttpPost("scan")]
public IActionResult Scan()
{
_taskScheduler.ScanEpubFonts();
return Ok();
}

private string GetContentType(string fileName)
Fesaa marked this conversation as resolved.
Show resolved Hide resolved
{
var extension = Path.GetExtension(fileName).ToLowerInvariant();
return extension switch
{
".ttf" => "application/font-tff",
".otf" => "application/font-otf",
".woff" => "application/font-woff",
".woff2" => "application/font-woff2",
_ => "application/octet-stream",
};
}
}
13 changes: 13 additions & 0 deletions API/DTOs/Font/EpubFontDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using API.Entities.Enums.Font;

namespace API.DTOs.Font;

public class EpubFontDto
{
public int Id { get; set; }
public string Name { get; set; }
public FontProvider Provider { get; set; }
public DateTime Created { get; set; }
public DateTime LastModified { get; set; }
}
1 change: 1 addition & 0 deletions API/Data/DataContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public DataContext(DbContextOptions options) : base(options)
public DbSet<ManualMigrationHistory> ManualMigrationHistory { get; set; } = null!;
public DbSet<SeriesBlacklist> SeriesBlacklist { get; set; } = null!;
public DbSet<AppUserCollection> AppUserCollection { get; set; } = null!;
public DbSet<EpubFont> EpubFont { get; set; } = null!;


protected override void OnModelCreating(ModelBuilder builder)
Expand Down
Loading