fix(fonts): increase custom font upload limit to 50MB (#1862)
This commit is contained in:
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user