diff --git a/src/main/java/stirling/software/SPDF/config/AppConfig.java b/src/main/java/stirling/software/SPDF/config/AppConfig.java index f741a05a4..b9bbba6f4 100644 --- a/src/main/java/stirling/software/SPDF/config/AppConfig.java +++ b/src/main/java/stirling/software/SPDF/config/AppConfig.java @@ -20,6 +20,8 @@ import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; import org.thymeleaf.spring6.SpringTemplateEngine; +import com.posthog.java.shaded.kotlin.text.Regex; + import lombok.extern.slf4j.Slf4j; import stirling.software.SPDF.model.ApplicationProperties; @@ -107,6 +109,32 @@ public class AppConfig { 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") public boolean runningInDocker() { return Files.exists(Paths.get("/.dockerenv")); diff --git a/src/main/java/stirling/software/SPDF/model/ApplicationProperties.java b/src/main/java/stirling/software/SPDF/model/ApplicationProperties.java index 6956a28fc..71c403872 100644 --- a/src/main/java/stirling/software/SPDF/model/ApplicationProperties.java +++ b/src/main/java/stirling/software/SPDF/model/ApplicationProperties.java @@ -290,12 +290,19 @@ public class ApplicationProperties { private Boolean disableSanitize; private Boolean enableUrlToPDF; private CustomPaths customPaths = new CustomPaths(); + private UploadLimit uploadLimit; public boolean isAnalyticsEnabled() { return this.getEnableAnalytics() != null && this.getEnableAnalytics(); } } + @Data + public static class UploadLimit { + private Boolean enabled; + private String maxSize; + } + @Data public static class CustomPaths { private Pipeline pipeline = new Pipeline(); diff --git a/src/main/resources/settings.yml.template b/src/main/resources/settings.yml.template index b7306861c..57a5441eb 100644 --- a/src/main/resources/settings.yml.template +++ b/src/main/resources/settings.yml.template @@ -107,6 +107,9 @@ system: operations: weasyprint: "" #Defaults to /opt/venv/bin/weasyprint 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".