Create the upload limit config

This commit is contained in:
Pedro Fonseca 2025-04-08 00:19:24 +01:00
parent b024fb3ede
commit 56a8fea5d3
3 changed files with 38 additions and 0 deletions

View File

@ -20,6 +20,8 @@ import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.ResourceLoader;
import org.thymeleaf.spring6.SpringTemplateEngine; import org.thymeleaf.spring6.SpringTemplateEngine;
import com.posthog.java.shaded.kotlin.text.Regex;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import stirling.software.SPDF.model.ApplicationProperties; import stirling.software.SPDF.model.ApplicationProperties;
@ -107,6 +109,32 @@ public class AppConfig {
return (rateLimit != null) ? Boolean.valueOf(rateLimit) : false; return (rateLimit != null) ? Boolean.valueOf(rateLimit) : false;
} }
@Bean(name = "uploadLimit")
public long uploadLimit() {
boolean uploadLimit = applicationProperties.getSystem().getUploadLimit().getEnabled() != null
? applicationProperties.getSystem().getUploadLimit().getEnabled()
: false;
String maxUploadSize = applicationProperties.getSystem().getUploadLimit().getMaxSize();
if (new Regex("^[1-9][0-9]{0,2}[K|M|G]B$").matches(maxUploadSize)) {
log.error("Invalid maxUploadSize format. Expected format: {1,3}[0-9][K|M|G]B, but got: {}",
maxUploadSize);
return 0;
}
if (!uploadLimit || maxUploadSize == null || maxUploadSize.isEmpty()) {
return 0;
} else {
String number = maxUploadSize.replaceAll("[^0-9]", "");
String unit = maxUploadSize.replaceAll("[KB|MB|GB]", "");
long size = Long.parseLong(number);
return switch (unit) {
case "KB" -> size * 1024;
case "MB" -> size * 1024 * 1024;
case "GB" -> size * 1024 * 1024 * 1024;
default -> 0;
};
}
}
@Bean(name = "RunningInDocker") @Bean(name = "RunningInDocker")
public boolean runningInDocker() { public boolean runningInDocker() {
return Files.exists(Paths.get("/.dockerenv")); return Files.exists(Paths.get("/.dockerenv"));

View File

@ -290,12 +290,19 @@ public class ApplicationProperties {
private Boolean disableSanitize; private Boolean disableSanitize;
private Boolean enableUrlToPDF; private Boolean enableUrlToPDF;
private CustomPaths customPaths = new CustomPaths(); private CustomPaths customPaths = new CustomPaths();
private UploadLimit uploadLimit;
public boolean isAnalyticsEnabled() { public boolean isAnalyticsEnabled() {
return this.getEnableAnalytics() != null && this.getEnableAnalytics(); return this.getEnableAnalytics() != null && this.getEnableAnalytics();
} }
} }
@Data
public static class UploadLimit {
private Boolean enabled;
private String maxSize;
}
@Data @Data
public static class CustomPaths { public static class CustomPaths {
private Pipeline pipeline = new Pipeline(); private Pipeline pipeline = new Pipeline();

View File

@ -107,6 +107,9 @@ system:
operations: operations:
weasyprint: "" #Defaults to /opt/venv/bin/weasyprint weasyprint: "" #Defaults to /opt/venv/bin/weasyprint
unoconvert: "" #Defaults to /opt/venv/bin/unoconvert unoconvert: "" #Defaults to /opt/venv/bin/unoconvert
uploadLimit:
enableUploadSizeLimit: false # Set to 'true' to enforce a size limit on files uploaded
limit: "" # Defaults to '0MB'. Set a number, between 0 and 999, followed by one of the following strings to set a limit. "KB", "MB", "GB".