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
@@ -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() {