diff --git a/src/main/java/stirling/software/SPDF/config/AppConfig.java b/src/main/java/stirling/software/SPDF/config/AppConfig.java index f741a05a4..146f97626 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,33 @@ public class AppConfig { return (rateLimit != null) ? Boolean.valueOf(rateLimit) : false; } + @Bean(name = "uploadLimit") + public long uploadLimit() { + String maxUploadSize = + applicationProperties.getSystem().getFileUploadLimit() != null + ? applicationProperties.getSystem().getFileUploadLimit() + : ""; + + if (maxUploadSize.isEmpty()) { + return 0; + } else if (!new Regex("^[1-9][0-9]{0,2}[KMGkmg][Bb]$").matches(maxUploadSize)) { + log.error( + "Invalid maxUploadSize format. Expected format: [1-9][0-9]{0,2}[KMGkmg][Bb], but got: {}", + maxUploadSize); + return 0; + } else { + String unit = maxUploadSize.replaceAll("[1-9][0-9]{0,2}", "").toUpperCase(); + String number = maxUploadSize.replaceAll("[KMGkmg][Bb]", ""); + 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/controller/web/GlobalUploadLimitWebController.java b/src/main/java/stirling/software/SPDF/controller/web/GlobalUploadLimitWebController.java new file mode 100644 index 000000000..1c8b2b3ac --- /dev/null +++ b/src/main/java/stirling/software/SPDF/controller/web/GlobalUploadLimitWebController.java @@ -0,0 +1,30 @@ +package stirling.software.SPDF.controller.web; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ModelAttribute; + +@Component +@ControllerAdvice +public class GlobalUploadLimitWebController { + + @Autowired() private long uploadLimit; + + @ModelAttribute("uploadLimit") + public long populateUploadLimit() { + return uploadLimit; + } + + @ModelAttribute("uploadLimitReadable") + public String populateReadableLimit() { + return humanReadableByteCount(uploadLimit); + } + + private String humanReadableByteCount(long bytes) { + if (bytes < 1024) return bytes + " B"; + int exp = (int) (Math.log(bytes) / Math.log(1024)); + String pre = "KMGTPE".charAt(exp - 1) + "B"; + return String.format("%.1f %s", bytes / Math.pow(1024, exp), pre); + } +} diff --git a/src/main/java/stirling/software/SPDF/model/ApplicationProperties.java b/src/main/java/stirling/software/SPDF/model/ApplicationProperties.java index 6c2960832..d42429619 100644 --- a/src/main/java/stirling/software/SPDF/model/ApplicationProperties.java +++ b/src/main/java/stirling/software/SPDF/model/ApplicationProperties.java @@ -290,6 +290,7 @@ public class ApplicationProperties { private Boolean disableSanitize; private Boolean enableUrlToPDF; private CustomPaths customPaths = new CustomPaths(); + private String fileUploadLimit; public boolean isAnalyticsEnabled() { return this.getEnableAnalytics() != null && this.getEnableAnalytics(); diff --git a/src/main/resources/messages_en_GB.properties b/src/main/resources/messages_en_GB.properties index 9e8d5bc2a..5b7efbab4 100644 --- a/src/main/resources/messages_en_GB.properties +++ b/src/main/resources/messages_en_GB.properties @@ -10,6 +10,9 @@ multiPdfPrompt=Select PDFs (2+) multiPdfDropPrompt=Select (or drag & drop) all PDFs you require imgPrompt=Select Image(s) genericSubmit=Submit +uploadLimit=Maximum file size: +uploadLimitExceededSingular=is too large. Maximum allowed size is +uploadLimitExceededPlural=are too large. Maximum allowed size is processTimeWarning=Warning: This process can take up to a minute depending on file-size pageOrderPrompt=Custom Page Order (Enter a comma-separated list of page numbers or Functions like 2n+1) : pageSelectionPrompt=Custom Page Selection (Enter a comma-separated list of page numbers 1,5,6 or Functions like 2n+1) : diff --git a/src/main/resources/messages_en_US.properties b/src/main/resources/messages_en_US.properties index 69a6cf74f..e2a2eeb75 100644 --- a/src/main/resources/messages_en_US.properties +++ b/src/main/resources/messages_en_US.properties @@ -10,6 +10,9 @@ multiPdfPrompt=Select PDFs (2+) multiPdfDropPrompt=Select (or drag & drop) all PDFs you require imgPrompt=Select Image(s) genericSubmit=Submit +uploadLimit=Maximum file size: +uploadLimitExceededSingular=is too large. Maximum allowed size is +uploadLimitExceededPlural=are too large. Maximum allowed size is processTimeWarning=Warning: This process can take up to a minute depending on file-size pageOrderPrompt=Custom Page Order (Enter a comma-separated list of page numbers or Functions like 2n+1) : pageSelectionPrompt=Custom Page Selection (Enter a comma-separated list of page numbers 1,5,6 or Functions like 2n+1) : diff --git a/src/main/resources/messages_pt_PT.properties b/src/main/resources/messages_pt_PT.properties index 842d6e310..88de5abf7 100644 --- a/src/main/resources/messages_pt_PT.properties +++ b/src/main/resources/messages_pt_PT.properties @@ -10,6 +10,9 @@ multiPdfPrompt=Selecione PDFs (2+) multiPdfDropPrompt=Selecione (ou arraste e solte) todos os PDFs necessários imgPrompt=Selecione Imagem(ns) genericSubmit=Submeter +uploadLimit=Tamanho máximo de ficheiro: +uploadLimitExceededSingular=é muito grande. O tamanho máximo permitido é +uploadLimitExceededPlural=são muito grandes. O tamanho máximo permitido é processTimeWarning=Aviso: Este processo pode demorar até um minuto dependendo do tamanho do ficheiro pageOrderPrompt=Ordem Personalizada de Páginas (Insira uma lista de números de página separados por vírgulas ou Funções como 2n+1): pageSelectionPrompt=Seleção Personalizada de Páginas (Insira uma lista de números de página separados por vírgulas 1,5,6 ou Funções como 2n+1): diff --git a/src/main/resources/settings.yml.template b/src/main/resources/settings.yml.template index 827fec967..1c3ee32ae 100644 --- a/src/main/resources/settings.yml.template +++ b/src/main/resources/settings.yml.template @@ -110,6 +110,7 @@ system: operations: weasyprint: '' #Defaults to /opt/venv/bin/weasyprint unoconvert: '' #Defaults to /opt/venv/bin/unoconvert + fileUploadLimit: '' # Defaults to "". No limit when string is empty. Set a number, between 0 and 999, followed by one of the following strings to set a limit. "KB", "MB", "GB". ui: appName: '' # application's visible name diff --git a/src/main/resources/static/js/downloader.js b/src/main/resources/static/js/downloader.js index 900e2539a..3b325b597 100644 --- a/src/main/resources/static/js/downloader.js +++ b/src/main/resources/static/js/downloader.js @@ -43,6 +43,20 @@ firstErrorOccurred = false; const url = this.action; let files = $('#fileInput-input')[0].files; + const uploadLimit = window.stirlingPDF?.uploadLimit ?? 0; + if (uploadLimit > 0) { + const oversizedFiles = Array.from(files).filter(f => f.size > uploadLimit); + if (oversizedFiles.length > 0) { + const names = oversizedFiles.map(f => `"${f.name}"`).join(', '); + if (names.length === 1) { + alert(`${names} ${window.stirlingPDF.uploadLimitExceededSingular} ${window.stirlingPDF.uploadLimitReadable}.`); + } else { + alert(`${names} ${window.stirlingPDF.uploadLimitExceededPlural} ${window.stirlingPDF.uploadLimitReadable}.`); + } + files = Array.from(files).filter(f => f.size <= uploadLimit); + if (files.length === 0) return; + } + } const formData = new FormData(this); const submitButton = document.getElementById('submitBtn'); const showGameBtn = document.getElementById('show-game-btn'); diff --git a/src/main/resources/static/js/fileInput.js b/src/main/resources/static/js/fileInput.js index 32922390b..a67ff1fd6 100644 --- a/src/main/resources/static/js/fileInput.js +++ b/src/main/resources/static/js/fileInput.js @@ -196,6 +196,28 @@ function setupFileInput(chooser) { await checkZipFile(); + const uploadLimit = window.stirlingPDF?.uploadLimit ?? 0; + if (uploadLimit > 0) { + const oversizedFiles = allFiles.filter(f => f.size > uploadLimit); + if (oversizedFiles.length > 0) { + const names = oversizedFiles.map(f => `"${f.name}"`).join(', '); + if (names.length === 1) { + alert(`${names} ${window.stirlingPDF.uploadLimitExceededSingular} ${window.stirlingPDF.uploadLimitReadable}.`); + } else { + alert(`${names} ${window.stirlingPDF.uploadLimitExceededPlural} ${window.stirlingPDF.uploadLimitReadable}.`); + } + allFiles = allFiles.filter(f => f.size <= uploadLimit); + const dataTransfer = new DataTransfer(); + allFiles.forEach(f => dataTransfer.items.add(f)); + input.files = dataTransfer.files; + + if (allFiles.length === 0) { + inputContainer.querySelector('#fileInputText').innerHTML = originalText; + return; + } + } + } + allFiles = await Promise.all( allFiles.map(async (file) => { let decryptedFile = file; diff --git a/src/main/resources/templates/fragments/common.html b/src/main/resources/templates/fragments/common.html index dced4be3d..6ba5e710b 100644 --- a/src/main/resources/templates/fragments/common.html +++ b/src/main/resources/templates/fragments/common.html @@ -241,6 +241,10 @@ window.stirlingPDF.sessionExpired = /*[[#{session.expired}]]*/ ''; window.stirlingPDF.refreshPage = /*[[#{session.refreshPage}]]*/ 'Refresh Page'; window.stirlingPDF.error = /*[[#{error}]]*/ "Error"; + window.stirlingPDF.uploadLimit = /*[[${uploadLimit}]]*/ 0; + window.stirlingPDF.uploadLimitReadable = /*[[${uploadLimitReadable}]]*/ 'Unlimited'; + window.stirlingPDF.uploadLimitExceededSingular = /*[[#{uploadLimitExceededSingular}]]*/ 'is too large. Maximum allowed size is'; + window.stirlingPDF.uploadLimitExceededPlural = /*[[#{uploadLimitExceededPlural}]]*/ 'are too large. Maximum allowed size is'; })(); @@ -289,8 +293,11 @@
+
+ Maximum file size: + +
-