From d973cde2550d9d3b81036edce5b5c749d72b8c0c Mon Sep 17 00:00:00 2001 From: Pedro Fonseca Date: Fri, 11 Apr 2025 11:08:08 +0100 Subject: [PATCH] Create the global upload limit controller This controller will allow for every page and template to load the variables used for upload limit displaying and enforcing --- .../web/GlobalUploadLimitWebController.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/main/java/stirling/software/SPDF/controller/web/GlobalUploadLimitWebController.java 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); + } +}