From 33a6a7869c19ec4e56d628c7d0f8dd0070b07ee0 Mon Sep 17 00:00:00 2001 From: Anthony Stirling <77850077+Frooodle@users.noreply.github.com> Date: Wed, 26 Jul 2023 22:08:19 +0100 Subject: [PATCH] Further Fixes --- build.gradle | 2 +- .../converters/ConvertImgPDFController.java | 4 +- .../controller/web/GeneralWebController.java | 79 ++++++------------- src/main/resources/static/js/downloader.js | 18 ++++- .../templates/convert/img-to-pdf.html | 2 +- 5 files changed, 45 insertions(+), 60 deletions(-) diff --git a/build.gradle b/build.gradle index bf19d3c6..688bee00 100644 --- a/build.gradle +++ b/build.gradle @@ -8,7 +8,7 @@ plugins { } group = 'stirling.software' -version = '0.11.1' +version = '0.11.2' sourceCompatibility = '17' repositories { diff --git a/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertImgPDFController.java b/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertImgPDFController.java index d19a24b6..d4964196 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertImgPDFController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertImgPDFController.java @@ -43,7 +43,7 @@ public class ConvertImgPDFController { @Parameter(description = "Choose between a single image containing all pages or separate images for each page", schema = @Schema(allowableValues = {"single", "multiple"})) String singleOrMultiple, @RequestParam("colorType") - @Parameter(description = "The color type of the output image(s)", schema = @Schema(allowableValues = {"rgb", "greyscale", "blackwhite"})) + @Parameter(description = "The color type of the output image(s)", schema = @Schema(allowableValues = {"color", "greyscale", "blackwhite"})) String colorType, @RequestParam("dpi") @Parameter(description = "The DPI (dots per inch) for the output image(s)") @@ -94,7 +94,7 @@ public class ConvertImgPDFController { @Parameter(description = "Whether to stretch the images to fit the PDF page or maintain the aspect ratio", example = "false") boolean stretchToFit, @RequestParam("colorType") - @Parameter(description = "The color type of the output image(s)", schema = @Schema(allowableValues = {"rgb", "greyscale", "blackwhite"})) + @Parameter(description = "The color type of the output image(s)", schema = @Schema(allowableValues = {"color", "greyscale", "blackwhite"})) String colorType, @RequestParam(defaultValue = "false", name = "autoRotate") @Parameter(description = "Whether to automatically rotate the images to better fit the PDF page", example = "true") diff --git a/src/main/java/stirling/software/SPDF/controller/web/GeneralWebController.java b/src/main/java/stirling/software/SPDF/controller/web/GeneralWebController.java index 8290f76b..75d67401 100644 --- a/src/main/java/stirling/software/SPDF/controller/web/GeneralWebController.java +++ b/src/main/java/stirling/software/SPDF/controller/web/GeneralWebController.java @@ -1,35 +1,25 @@ package stirling.software.SPDF.controller.web; - -import java.io.File; import java.io.IOException; -import java.net.URI; -import java.net.URISyntaxException; -import java.net.URL; import java.nio.charset.StandardCharsets; -import java.nio.file.FileSystem; -import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; +import java.util.stream.Stream; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.Resource; +import org.springframework.core.io.ResourceLoader; +import org.springframework.core.io.support.ResourcePatternUtils; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.ArrayList; -import java.util.List; -import java.util.HashMap; -import java.util.stream.Collectors; -import java.util.stream.Stream; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.ModelAttribute; - import com.fasterxml.jackson.databind.ObjectMapper; import io.swagger.v3.oas.annotations.Hidden; @@ -128,45 +118,28 @@ public class GeneralWebController { model.addAttribute("fonts", getFontNames()); return "sign"; } + + @Autowired + private ResourceLoader resourceLoader; + private List getFontNames() { - List fontNames = new ArrayList<>(); - try { - // Get the directory URL from classpath - URL dirURL = getClass().getClassLoader().getResource("static/fonts"); - - if (dirURL != null && dirURL.getProtocol().equals("file")) { - // If running from the file system (e.g., IDE) - fontNames.addAll( - Files.list(Paths.get(dirURL.toURI())) - .map(java.nio.file.Path::getFileName) - .map(java.nio.file.Path::toString) - .filter(name -> name.endsWith(".woff2")) - .map(name -> name.substring(0, name.length() - 6)) // Remove .woff2 extension - .collect(Collectors.toList()) - ); - } else { - // If running from a JAR file - // Resources in JAR go through a different URL protocol. - // In this case, we'll use a different approach to list them. - - // Create a link to the resource. This assumes resources are at the root of the JAR. - URI uri = getClass().getResource("/").toURI(); - FileSystem fileSystem = FileSystems.newFileSystem(uri, new HashMap<>()); - Path myPath = fileSystem.getPath("/static/fonts/"); - Files.walk(myPath, 1) - .filter(path -> !Files.isDirectory(path)) - .map(path -> path.getFileName().toString()) - .filter(name -> name.endsWith(".woff2")) - .map(name -> name.substring(0, name.length() - 6)) // Remove .woff2 extension - .forEach(fontNames::add); - fileSystem.close(); - } - } catch (IOException | URISyntaxException e) { + Resource[] resources = ResourcePatternUtils.getResourcePatternResolver(resourceLoader) + .getResources("classpath:static/fonts/*.woff2"); + + return Arrays.stream(resources) + .map(resource -> { + try { + String filename = resource.getFilename(); + return filename.substring(0, filename.length() - 6); // Remove .woff2 extension + } catch (Exception e) { + throw new RuntimeException("Error processing filename", e); + } + }) + .collect(Collectors.toList()); + } catch (Exception e) { throw new RuntimeException("Failed to read font directory", e); } - - return fontNames; } diff --git a/src/main/resources/static/js/downloader.js b/src/main/resources/static/js/downloader.js index e9abdf38..d06d82dc 100644 --- a/src/main/resources/static/js/downloader.js +++ b/src/main/resources/static/js/downloader.js @@ -154,11 +154,22 @@ async function submitMultiPdfForm(url, files) { if (zipFiles) { jszip = new JSZip(); } - + + + // Get the form with the method attribute set to POST + let postForm = document.querySelector('form[method="POST"]'); + // Get existing form data - let formData = new FormData($('form')[0]); + let formData; + if (postForm) { + formData = new FormData($(postForm)[0]); // Convert the form to a jQuery object and get the raw DOM element + } else { + console.log("No form with POST method found."); + } + //Remove file to reuse parameters for other runs formData.delete('fileInput'); + const CONCURRENCY_LIMIT = 8; const chunks = []; for (let i = 0; i < Array.from(files).length; i += CONCURRENCY_LIMIT) { @@ -169,10 +180,11 @@ async function submitMultiPdfForm(url, files) { const promises = chunk.map(async file => { let fileFormData = new FormData(); fileFormData.append('fileInput', file); - + console.log(fileFormData); // Add other form data for (let pair of formData.entries()) { fileFormData.append(pair[0], pair[1]); + console.log(pair[0]+ ', ' + pair[1]); } try { diff --git a/src/main/resources/templates/convert/img-to-pdf.html b/src/main/resources/templates/convert/img-to-pdf.html index b64f43f1..3182eb9d 100644 --- a/src/main/resources/templates/convert/img-to-pdf.html +++ b/src/main/resources/templates/convert/img-to-pdf.html @@ -31,7 +31,7 @@
-