fix(fonts): increase custom font upload limit to 50MB (#1862)

This commit is contained in:
lixiaoyu0123
2026-06-29 08:58:16 -04:00
committed by GitHub
parent be574d5b16
commit bda8db5e65
18 changed files with 72 additions and 22 deletions
@@ -11,4 +11,4 @@ public class PublicAppSetting {
private boolean remoteAuthEnabled;
private OidcProviderDetails oidcProviderDetails;
private boolean oidcForceOnlyMode;
}
}
@@ -2,6 +2,7 @@ package org.booklore.service.customfont;
import org.booklore.config.AppProperties;
import org.booklore.exception.APIException;
import org.booklore.exception.ApiError;
import org.booklore.mapper.CustomFontMapper;
import org.booklore.model.dto.CustomFontDto;
import org.booklore.model.entity.BookLoreUserEntity;
@@ -44,7 +45,8 @@ public class CustomFontService {
private final AppProperties appProperties;
private static final int MAX_FONTS_PER_USER = 10;
private static final long MAX_FILE_SIZE_BYTES = 5L * 1024 * 1024;
private static final int MAX_FILE_SIZE_MB = 50;
private static final long MAX_FILE_SIZE_BYTES = MAX_FILE_SIZE_MB * 1024L * 1024L;
private static final String CUSTOM_FONTS_DIR = "custom-fonts";
private static final int MAX_FONT_NAME_LENGTH = 100;
@@ -191,7 +193,7 @@ public class CustomFontService {
}
if (file.getSize() > MAX_FILE_SIZE_BYTES) {
throw new IllegalArgumentException("File size exceeds maximum limit of 5MB");
throw ApiError.FILE_TOO_LARGE.createException(MAX_FILE_SIZE_MB);
}
int currentFontCount = customFontRepository.countByUserId(userId);
@@ -1,6 +1,8 @@
package org.booklore.service.customfont;
import org.booklore.config.AppProperties;
import org.booklore.exception.APIException;
import org.booklore.exception.ApiError;
import org.booklore.mapper.CustomFontMapper;
import org.booklore.model.dto.CustomFontDto;
import org.booklore.model.entity.BookLoreUserEntity;
@@ -119,19 +121,65 @@ class CustomFontServiceTest {
void uploadFont_withOversizedFile_shouldThrowException() {
// Arrange
Long userId = 1L;
byte[] largeContent = new byte[6 * 1024 * 1024]; // 6MB (exceeds 5MB limit)
byte[] largeContent = new byte[51 * 1024 * 1024]; // 51MB (exceeds default 50MB limit)
MultipartFile file = new MockMultipartFile("font.ttf", "font.ttf", "font/ttf", largeContent);
when(customFontRepository.countByUserId(userId)).thenReturn(0);
// Act & Assert
assertThatThrownBy(() -> service.uploadFont(file, "Font", userId))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("File size exceeds maximum limit");
assertThatExceptionOfType(APIException.class)
.isThrownBy(() -> service.uploadFont(file, "Font", userId))
.satisfies(ex -> {
assertThat(ex.getStatus()).isEqualTo(ApiError.FILE_TOO_LARGE.getStatus());
assertThat(ex.getMessage()).contains("50");
});
verify(customFontRepository, never()).save(any());
}
@Test
@DisplayName("uploadFont_withFileAboveOldLimit_shouldAllowFileUnder50Mb")
void uploadFont_withFileAboveOldLimit_shouldAllowFileUnder50Mb() throws IOException {
// Arrange
Long userId = 1L;
String fontName = "Large Font";
byte[] fontContent = new byte[5 * 1024 * 1024 + 1];
fontContent[0] = 0x00;
fontContent[1] = 0x01;
fontContent[2] = 0x00;
fontContent[3] = 0x00;
MultipartFile file = new MockMultipartFile("font.ttf", "font.ttf", "font/ttf", fontContent);
BookLoreUserEntity user = new BookLoreUserEntity();
user.setId(userId);
CustomFontEntity savedEntity = CustomFontEntity.builder()
.id(1L)
.user(user)
.fontName(fontName)
.fileName("user_1_font_123.ttf")
.originalFileName("font.ttf")
.format(FontFormat.TTF)
.fileSize((long) fontContent.length)
.build();
CustomFontDto expectedDto = new CustomFontDto();
expectedDto.setId(1L);
expectedDto.setFontName(fontName);
when(userRepository.findById(userId)).thenReturn(Optional.of(user));
when(customFontRepository.countByUserId(userId)).thenReturn(0);
when(customFontRepository.save(any(CustomFontEntity.class))).thenReturn(savedEntity);
when(customFontMapper.toDto(savedEntity)).thenReturn(expectedDto);
// Act
CustomFontDto result = service.uploadFont(file, fontName, userId);
// Assert
assertThat(result.getFontName()).isEqualTo(fontName);
verify(customFontRepository).save(any(CustomFontEntity.class));
}
@Test
@DisplayName("uploadFont_withInvalidExtension_shouldThrowException")
void uploadFont_withInvalidExtension_shouldThrowException() {
@@ -30,9 +30,9 @@ export class FontUploadDialogComponent implements OnDestroy {
isLoadingPreview = false;
currentPreviewToken: number | null = null;
readonly maxFileSize = 5242880;
readonly maxFonts = 10;
readonly acceptedFormats = ['.ttf', '.otf', '.woff', '.woff2'];
readonly maxFileSize = 50 * 1024 * 1024;
onUploadZoneClick(): void {
if (this.isUploading) {
+1 -1
View File
@@ -163,7 +163,7 @@
"selectFile": "Schriftdatei auswählen",
"dragDrop": "Schriftdatei per Drag & Drop hierher ziehen",
"supportedFormats": "Unterstützte Formate: <strong>TTF</strong>, <strong>OTF</strong>, <strong>WOFF</strong>, <strong>WOFF2</strong>",
"maxSize": "Maximale Dateigröße: 5 MB",
"maxSize": "Maximale Dateigröße: 50 MB",
"fontPreview": "Schriftvorschau",
"displayName": "Anzeigename der Schriftart",
"displayNameDesc": "Dieser Name erscheint in der Schriftauswahl beim Lesen von EPUB-Büchern.",
+1 -1
View File
@@ -163,7 +163,7 @@
"selectFile": "Select Font File",
"dragDrop": "Drag and Drop Font File",
"supportedFormats": "Supported formats: <strong>TTF</strong>, <strong>OTF</strong>, <strong>WOFF</strong>, <strong>WOFF2</strong>",
"maxSize": "Maximum file size: 5 MB",
"maxSize": "Maximum file size: 50 MB",
"fontPreview": "Font Preview",
"displayName": "Font Display Name",
"displayNameDesc": "This name will appear in the font selector when reading EPUB books.",
+1 -1
View File
@@ -163,7 +163,7 @@
"selectFile": "Seleccionar archivo de fuente",
"dragDrop": "Arrastra y suelta el archivo de fuente",
"supportedFormats": "Formatos compatibles: <strong>TTF</strong>, <strong>OTF</strong>, <strong>WOFF</strong>, <strong>WOFF2</strong>",
"maxSize": "Tamaño máximo del archivo: 5 MB",
"maxSize": "Tamaño máximo del archivo: 50 MB",
"fontPreview": "Vista previa de la fuente",
"displayName": "Nombre de la fuente",
"displayNameDesc": "Este nombre aparecerá en el selector de fuentes al leer libros EPUB.",
+1 -1
View File
@@ -163,7 +163,7 @@
"selectFile": "Sélectionner un fichier de police",
"dragDrop": "Glisser-déposer un fichier de police",
"supportedFormats": "Formats pris en charge : <strong>TTF</strong>, <strong>OTF</strong>, <strong>WOFF</strong>, <strong>WOFF2</strong>",
"maxSize": "Taille maximale : 5 Mo",
"maxSize": "Taille maximale : 50 Mo",
"fontPreview": "Aperçu de la police",
"displayName": "Nom d'affichage de la police",
"displayNameDesc": "Ce nom apparaîtra dans le sélecteur de polices lors de la lecture de livres EPUB.",
+1 -1
View File
@@ -163,7 +163,7 @@
"selectFile": "Odaberite datoteku fonta",
"dragDrop": "Povucite i ispustite datoteku fonta",
"supportedFormats": "Podržani formati: <strong>TTF</strong>, <strong>OTF</strong>, <strong>WOFF</strong>, <strong>WOFF2</strong>",
"maxSize": "Maksimalna veličina datoteke: 5 MB",
"maxSize": "Maksimalna veličina datoteke: 50 MB",
"fontPreview": "Pregled fonta",
"displayName": "Prikazni naziv fonta",
"displayNameDesc": "Ovaj naziv će se pojaviti u biraču fontova prilikom čitanja EPUB knjiga.",
+1 -1
View File
@@ -163,7 +163,7 @@
"selectFile": "Seleziona file font",
"dragDrop": "Trascina e rilascia il file font",
"supportedFormats": "Formati supportati: <strong>TTF</strong>, <strong>OTF</strong>, <strong>WOFF</strong>, <strong>WOFF2</strong>",
"maxSize": "Dimensione massima: 5 MB",
"maxSize": "Dimensione massima: 50 MB",
"fontPreview": "Anteprima font",
"displayName": "Nome visualizzato font",
"displayNameDesc": "Questo nome apparirà nel selettore font durante la lettura di libri EPUB.",
+1 -1
View File
@@ -163,7 +163,7 @@
"selectFile": "フォントファイルを選択",
"dragDrop": "フォントファイルをドラッグ&ドロップ",
"supportedFormats": "対応形式: <strong>TTF</strong>、<strong>OTF</strong>、<strong>WOFF</strong>、<strong>WOFF2</strong>",
"maxSize": "最大ファイルサイズ: 5 MB",
"maxSize": "最大ファイルサイズ: 50 MB",
"fontPreview": "フォントプレビュー",
"displayName": "フォント表示名",
"displayNameDesc": "EPUB本を読む際のフォントセレクターに表示される名前です。",
+1 -1
View File
@@ -163,7 +163,7 @@
"selectFile": "Wybierz plik czcionki",
"dragDrop": "Przeciągnij i upuść plik czcionki",
"supportedFormats": "Obsługiwane formaty: <strong>TTF</strong>, <strong>OTF</strong>, <strong>WOFF</strong>, <strong>WOFF2</strong>",
"maxSize": "Maksymalny rozmiar pliku: 5 MB",
"maxSize": "Maksymalny rozmiar pliku: 50 MB",
"fontPreview": "Podgląd czcionki",
"displayName": "Nazwa wyświetlana czcionki",
"displayNameDesc": "Ta nazwa będzie widoczna w selektorze czcionek podczas czytania książek EPUB.",
+1 -1
View File
@@ -163,7 +163,7 @@
"selectFile": "Selecionar Arquivo de Fonte",
"dragDrop": "Arraste e Solte o Arquivo de Fonte",
"supportedFormats": "Formatos suportados: <strong>TTF</strong>, <strong>OTF</strong>, <strong>WOFF</strong>, <strong>WOFF2</strong>",
"maxSize": "Tamanho máximo do arquivo: 5 MB",
"maxSize": "Tamanho máximo do arquivo: 50 MB",
"fontPreview": "Prévia da Fonte",
"displayName": "Nome de Exibição da Fonte",
"displayNameDesc": "Este nome aparecerá no seletor de fontes ao ler livros EPUB.",
+1 -1
View File
@@ -163,7 +163,7 @@
"selectFile": "Выбрать файл шрифта",
"dragDrop": "Перетащите файл шрифта",
"supportedFormats": "Поддерживаемые форматы: <strong>TTF</strong>, <strong>OTF</strong>, <strong>WOFF</strong>, <strong>WOFF2</strong>",
"maxSize": "Максимальный размер файла: 5 МБ",
"maxSize": "Максимальный размер файла: 50 МБ",
"fontPreview": "Предпросмотр шрифта",
"displayName": "Отображаемое имя шрифта",
"displayNameDesc": "Это имя будет отображаться в выборе шрифтов при чтении EPUB-книг.",
+1 -1
View File
@@ -163,7 +163,7 @@
"selectFile": "Izberi datoteko pisave",
"dragDrop": "Povleci in spusti datoteko pisave sem",
"supportedFormats": "Podprti formati: <strong>TTF</strong>, <strong>OTF</strong>, <strong>WOFF</strong>, <strong>WOFF2</strong>",
"maxSize": "Največja velikost datoteke: 5 MB",
"maxSize": "Največja velikost datoteke: 50 MB",
"fontPreview": "Predogled pisave",
"displayName": "Prikazno ime pisave",
"displayNameDesc": "To ime se bo pojavilo v izbirniku pisav med branjem e-knjig.",
+1 -1
View File
@@ -163,7 +163,7 @@
"selectFile": "Välj typsnittsfil",
"dragDrop": "Dra och släpp typsnittsfil",
"supportedFormats": "Format som stöds: <strong>TTF</strong>, <strong>OTF</strong>, <strong>WOFF</strong>, <strong>WOFF2</strong>",
"maxSize": "Maximal filstorlek: 5 MB",
"maxSize": "Maximal filstorlek: 50 MB",
"fontPreview": "Förhandsvisning av typsnitt",
"displayName": "Visningsnamn",
"displayNameDesc": "Detta namn visas i typsnittsväljaren när du läser EPUB-böcker.",
+1 -1
View File
@@ -163,7 +163,7 @@
"selectFile": "Виберіть файл шрифту",
"dragDrop": "Перетягніть файл шрифту",
"supportedFormats": "Підтримувані формати: <strong>TTF</strong>, <strong>OTF</strong>, <strong>WOFF</strong>, <strong>WOFF2</strong>",
"maxSize": "Максимальний розмір файлу: 5 MB",
"maxSize": "Максимальний розмір файлу: 50 МБ",
"fontPreview": "Попередній перегляд шрифту",
"displayName": "Назва шрифту",
"displayNameDesc": "Ця назва відображатиметься у списку шрифтів під час читання EPUB-книг.",
+1 -1
View File
@@ -163,7 +163,7 @@
"selectFile": "选择字体文件",
"dragDrop": "拖放字体文件",
"supportedFormats": "支持的格式:<strong>TTF</strong>、<strong>OTF</strong>、<strong>WOFF</strong>、<strong>WOFF2</strong>",
"maxSize": "最大文件大小:5 MB",
"maxSize": "最大文件大小:50 MB",
"fontPreview": "字体预览",
"displayName": "字体显示名称",
"displayNameDesc": "此名称将在阅读 EPUB 图书时显示在字体选择器中。",