fix(api): use configurable upload limit for author photo / book cover (#783)
This commit is contained in:
@@ -12,11 +12,13 @@ import org.booklore.model.dto.CoverImage;
|
||||
import org.booklore.model.dto.Library;
|
||||
import org.booklore.model.dto.request.AuthorMatchRequest;
|
||||
import org.booklore.model.dto.request.AuthorUpdateRequest;
|
||||
import org.booklore.model.dto.settings.AppSettings;
|
||||
import org.booklore.model.entity.AuthorEntity;
|
||||
import org.booklore.model.entity.BookMetadataEntity;
|
||||
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;
|
||||
@@ -52,6 +54,7 @@ public class AuthorMetadataService {
|
||||
private final FileService fileService;
|
||||
private final DuckDuckGoCoverService duckDuckGoCoverService;
|
||||
private final AuthenticationService authenticationService;
|
||||
private final AppSettingService appSettingService;
|
||||
|
||||
public List<AuthorSummary> getAllAuthors() {
|
||||
BookLoreUser user = authenticationService.getAuthenticatedUser();
|
||||
@@ -211,11 +214,37 @@ public class AuthorMetadataService {
|
||||
}
|
||||
}
|
||||
|
||||
private long getMaxFileUploadSizeMb() {
|
||||
AppSettings appSettings = this.appSettingService.getAppSettings();
|
||||
|
||||
Integer maxFileUploadSizeMb = appSettings.getMaxFileUploadSizeInMb();
|
||||
|
||||
if (maxFileUploadSizeMb == null) {
|
||||
log.warn("Max File Upload Size is unset, defaulting to 0");
|
||||
return 0L;
|
||||
}
|
||||
|
||||
return maxFileUploadSizeMb.longValue();
|
||||
}
|
||||
|
||||
private void validatePhoto(MultipartFile file) {
|
||||
if (file.isEmpty()) {
|
||||
throw ApiError.INVALID_INPUT.createException("Uploaded file is empty");
|
||||
}
|
||||
long maxSizeMb = getMaxFileUploadSizeMb();
|
||||
long maxFileSize = maxSizeMb * 1024 * 1024;
|
||||
if (file.getSize() > maxFileSize) {
|
||||
throw ApiError.FILE_TOO_LARGE.createException(maxSizeMb);
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void uploadAuthorPhoto(Long authorId, MultipartFile file) {
|
||||
AuthorEntity author = authorRepository.findById(authorId)
|
||||
.orElseThrow(() -> ApiError.AUTHOR_NOT_FOUND.createException(authorId));
|
||||
|
||||
validatePhoto(file);
|
||||
|
||||
try {
|
||||
java.awt.image.BufferedImage image = FileService.readImage(file.getInputStream());
|
||||
if (image == null) {
|
||||
|
||||
@@ -3,6 +3,7 @@ package org.booklore.service.metadata;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.booklore.exception.ApiError;
|
||||
import org.booklore.model.dto.settings.AppSettings;
|
||||
import org.booklore.model.dto.settings.MetadataPersistenceSettings;
|
||||
import org.booklore.model.entity.AuthorEntity;
|
||||
import org.booklore.model.entity.BookEntity;
|
||||
@@ -502,13 +503,27 @@ public class BookCoverService {
|
||||
// SECTION: INTERNAL HELPERS
|
||||
// =========================
|
||||
|
||||
private long getMaxFileUploadSizeMb() {
|
||||
AppSettings appSettings = this.appSettingService.getAppSettings();
|
||||
|
||||
Integer maxFileUploadSizeMb = appSettings.getMaxFileUploadSizeInMb();
|
||||
|
||||
if (maxFileUploadSizeMb == null) {
|
||||
log.warn("Max File Upload Size is unset, cannot continue");
|
||||
throw ApiError.INTERNAL_SERVER_ERROR.createException("Max File Upload Size is Unset");
|
||||
}
|
||||
|
||||
return maxFileUploadSizeMb.longValue();
|
||||
}
|
||||
|
||||
private void validateCoverFile(MultipartFile file) {
|
||||
if (file.isEmpty()) {
|
||||
throw ApiError.INVALID_INPUT.createException("Uploaded file is empty");
|
||||
}
|
||||
long maxFileSize = 5L * 1024 * 1024;
|
||||
long maxSizeMb = getMaxFileUploadSizeMb();
|
||||
long maxFileSize = maxSizeMb * 1024 * 1024;
|
||||
if (file.getSize() > maxFileSize) {
|
||||
throw ApiError.FILE_TOO_LARGE.createException(5);
|
||||
throw ApiError.FILE_TOO_LARGE.createException(maxSizeMb);
|
||||
}
|
||||
// Detect MIME from content byte never trust the client-supplied Content-Type header
|
||||
try (var inputStream = file.getInputStream()) {
|
||||
|
||||
@@ -2,6 +2,7 @@ package org.booklore.util;
|
||||
|
||||
import org.booklore.config.AppProperties;
|
||||
import org.booklore.exception.ApiError;
|
||||
import org.booklore.model.dto.settings.AppSettings;
|
||||
import org.booklore.model.dto.settings.CoverCroppingSettings;
|
||||
import org.booklore.model.entity.BookMetadataEntity;
|
||||
import org.booklore.service.appsettings.AppSettingService;
|
||||
@@ -187,7 +188,20 @@ public class FileService {
|
||||
// VALIDATION
|
||||
// ========================================
|
||||
|
||||
private static void validateCoverFile(MultipartFile file) {
|
||||
private long getMaxFileUploadSizeMb() {
|
||||
AppSettings appSettings = this.appSettingService.getAppSettings();
|
||||
|
||||
Integer maxFileUploadSizeMb = appSettings.getMaxFileUploadSizeInMb();
|
||||
|
||||
if (maxFileUploadSizeMb == null) {
|
||||
log.warn("Max File Upload Size is unset, cannot continue");
|
||||
throw ApiError.INTERNAL_SERVER_ERROR.createException("Max File Upload Size is Unset");
|
||||
}
|
||||
|
||||
return maxFileUploadSizeMb.longValue();
|
||||
}
|
||||
|
||||
private void validateCoverFile(MultipartFile file) {
|
||||
if (file.isEmpty()) {
|
||||
throw new IllegalArgumentException("Uploaded file is empty");
|
||||
}
|
||||
@@ -199,8 +213,10 @@ public class FileService {
|
||||
if (!lowerType.startsWith(JPEG_MIME_TYPE) && !lowerType.startsWith(PNG_MIME_TYPE)) {
|
||||
throw new IllegalArgumentException("Only JPEG and PNG files are allowed");
|
||||
}
|
||||
if (file.getSize() > MAX_FILE_SIZE_BYTES) {
|
||||
throw new IllegalArgumentException("File size must not exceed 5 MB");
|
||||
long maxSizeMb = getMaxFileUploadSizeMb();
|
||||
long maxFileSize = maxSizeMb * 1024 * 1024;
|
||||
if (file.getSize() > maxFileSize) {
|
||||
throw ApiError.FILE_TOO_LARGE.createException(maxSizeMb);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user