fix(api): use configurable upload limit for author photo / book cover (#783)

This commit is contained in:
James Ward
2026-04-24 03:04:48 -04:00
committed by GitHub
parent 1e197e2868
commit 8765a3e63c
6 changed files with 136 additions and 8 deletions
@@ -10,6 +10,7 @@ import org.booklore.model.entity.AuthorEntity;
import org.booklore.model.enums.AuditAction;
import org.booklore.model.enums.AuthorMetadataSource;
import org.booklore.repository.AuthorRepository;
import org.booklore.service.appsettings.AppSettingService;
import org.booklore.service.audit.AuditService;
import org.booklore.service.metadata.DuckDuckGoCoverService;
import org.booklore.service.metadata.parser.AuthorParser;
@@ -41,6 +42,7 @@ class AuthorMetadataServiceTest {
@Mock private FileService fileService;
@Mock private DuckDuckGoCoverService duckDuckGoCoverService;
@Mock private AuthenticationService authenticationService;
@Mock private AppSettingService appSettingService;
private AuthorMetadataService service;
@@ -49,7 +51,16 @@ class AuthorMetadataServiceTest {
Map<AuthorMetadataSource, AuthorParser> authorParserMap = Map.of(
AuthorMetadataSource.AUDNEXUS, authorParser
);
service = new AuthorMetadataService(authorRepository, authorParserMap, auditService, fileService, duckDuckGoCoverService, authenticationService);
service = new AuthorMetadataService(
authorRepository,
authorParserMap,
auditService,
fileService,
duckDuckGoCoverService,
authenticationService,
appSettingService
);
BookLoreUser.UserPermissions adminPermissions = new BookLoreUser.UserPermissions();
adminPermissions.setAdmin(true);
@@ -57,6 +57,7 @@ class BookCoverServiceTest {
@Mock private TransactionTemplate transactionTemplate;
@Mock private Executor taskExecutor;
@Mock private AuthenticationService authenticationService;
@Mock private AppSettings appSettings;
@InjectMocks
private BookCoverService service;
@@ -396,6 +397,9 @@ class BookCoverServiceTest {
@Test
void filtersOutLockedBooksForBulkOperations() {
when(appSettingService.getAppSettings()).thenReturn(appSettings);
when(appSettings.getMaxFileUploadSizeInMb()).thenReturn(5);
BookEntity unlocked = buildBook(1L, false);
BookEntity locked = buildBook(2L, true);
@@ -431,6 +435,9 @@ class BookCoverServiceTest {
@Test
void rejectsNonImageContentType() throws Exception {
when(appSettingService.getAppSettings()).thenReturn(appSettings);
when(appSettings.getMaxFileUploadSizeInMb()).thenReturn(5);
MultipartFile file = mock(MultipartFile.class);
when(file.isEmpty()).thenReturn(false);
when(file.getSize()).thenReturn(1024L);
@@ -442,7 +449,10 @@ class BookCoverServiceTest {
}
@Test
void rejectsFileLargerThan5MB() {
void rejectsFileLargerThanLimit() {
when(appSettingService.getAppSettings()).thenReturn(appSettings);
when(appSettings.getMaxFileUploadSizeInMb()).thenReturn(5);
MultipartFile file = mock(MultipartFile.class);
when(file.isEmpty()).thenReturn(false);
when(file.getSize()).thenReturn(6L * 1024 * 1024);
@@ -454,6 +464,9 @@ class BookCoverServiceTest {
@Test
void acceptsJpegFile() {
when(appSettingService.getAppSettings()).thenReturn(appSettings);
when(appSettings.getMaxFileUploadSizeInMb()).thenReturn(5);
MultipartFile file = mock(MultipartFile.class);
when(file.isEmpty()).thenReturn(false);
when(file.getSize()).thenReturn(1024L);
@@ -469,6 +482,9 @@ class BookCoverServiceTest {
@Test
void acceptsPngFile() {
when(appSettingService.getAppSettings()).thenReturn(appSettings);
when(appSettings.getMaxFileUploadSizeInMb()).thenReturn(5);
MultipartFile file = mock(MultipartFile.class);
when(file.isEmpty()).thenReturn(false);
when(file.getSize()).thenReturn(1024L);
@@ -484,6 +500,9 @@ class BookCoverServiceTest {
@Test
void rejectsIOExceptionOnRead() throws Exception {
when(appSettingService.getAppSettings()).thenReturn(appSettings);
when(appSettings.getMaxFileUploadSizeInMb()).thenReturn(5);
MultipartFile file = mock(MultipartFile.class);
when(file.isEmpty()).thenReturn(false);
when(file.getSize()).thenReturn(1024L);
@@ -712,6 +731,9 @@ class BookCoverServiceTest {
@Test
void processesOnlyUnlockedBooks() throws Exception {
when(appSettingService.getAppSettings()).thenReturn(appSettings);
when(appSettings.getMaxFileUploadSizeInMb()).thenReturn(5);
BookEntity unlocked = buildBook(1L, false);
BookEntity locked = buildBook(2L, true);
@@ -959,7 +981,6 @@ class BookCoverServiceTest {
book.setLibrary(LibraryEntity.builder().build());
book.setLibraryPath(LibraryPathEntity.builder().path("/lib").build());
AppSettings appSettings = mock(AppSettings.class);
MetadataPersistenceSettings persistSettings = mock(MetadataPersistenceSettings.class);
when(appSettingService.getAppSettings()).thenReturn(appSettings);
when(appSettings.getMetadataPersistenceSettings()).thenReturn(persistSettings);
@@ -972,6 +972,12 @@ class FileServiceTest {
@Test
void validJpegFile_succeeds() throws IOException {
when(appSettingService.getAppSettings()).thenReturn(
AppSettings.builder()
.maxFileUploadSizeInMb(5)
.build()
);
BufferedImage image = createTestImage(300, 400);
byte[] imageBytes = imageToBytes(image);
MockMultipartFile file = new MockMultipartFile(
@@ -984,6 +990,12 @@ class FileServiceTest {
@Test
void validPngFile_succeeds() throws IOException {
when(appSettingService.getAppSettings()).thenReturn(
AppSettings.builder()
.maxFileUploadSizeInMb(5)
.build()
);
BufferedImage image = createTestImage(300, 400);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "PNG", baos);
@@ -1021,6 +1033,12 @@ class FileServiceTest {
@Test
void fileTooLarge_throwsRuntimeException() {
when(appSettingService.getAppSettings()).thenReturn(
AppSettings.builder()
.maxFileUploadSizeInMb(5)
.build()
);
byte[] largeData = new byte[6 * 1024 * 1024]; // 6MB
MockMultipartFile largeFile = new MockMultipartFile(
"file", "large.jpg", "image/jpeg", largeData);
@@ -1035,6 +1053,12 @@ class FileServiceTest {
@Test
void fileExactlyAtSizeLimit_succeeds() throws IOException {
when(appSettingService.getAppSettings()).thenReturn(
AppSettings.builder()
.maxFileUploadSizeInMb(5)
.build()
);
BufferedImage image = createTestImage(100, 100);
byte[] imageBytes = imageToBytes(image);
// Ensure it's under 5MB
@@ -1049,6 +1073,12 @@ class FileServiceTest {
@Test
void caseInsensitiveMimeType_succeeds() throws IOException {
when(appSettingService.getAppSettings()).thenReturn(
AppSettings.builder()
.maxFileUploadSizeInMb(5)
.build()
);
BufferedImage image = createTestImage(100, 100);
byte[] imageBytes = imageToBytes(image);
MockMultipartFile file = new MockMultipartFile(
@@ -1083,6 +1113,12 @@ class FileServiceTest {
@Test
void mimeTypeWithExtraParameters_succeeds() throws IOException {
when(appSettingService.getAppSettings()).thenReturn(
AppSettings.builder()
.maxFileUploadSizeInMb(5)
.build()
);
BufferedImage image = createTestImage(100, 100);
byte[] imageBytes = imageToBytes(image);
MockMultipartFile file = new MockMultipartFile(