From 145e8006f0773f884e67d45928b0b9b91622c0a6 Mon Sep 17 00:00:00 2001 From: Anthony Stirling <77850077+Frooodle@users.noreply.github.com> Date: Sat, 9 Sep 2023 18:51:43 +0100 Subject: [PATCH] metadata --- .../api/misc/ExtractImagesController.java | 12 ++-- .../api/misc/FakeScanControllerWIP.java | 8 +-- .../api/misc/MetadataController.java | 58 +++++++------------ .../model/api/PDFWithImageFormatRequest.java | 12 ++++ .../SPDF/model/api/misc/MetadataRequest.java | 44 ++++++++++++++ 5 files changed, 86 insertions(+), 48 deletions(-) create mode 100644 src/main/java/stirling/software/SPDF/model/api/PDFWithImageFormatRequest.java create mode 100644 src/main/java/stirling/software/SPDF/model/api/misc/MetadataRequest.java diff --git a/src/main/java/stirling/software/SPDF/controller/api/misc/ExtractImagesController.java b/src/main/java/stirling/software/SPDF/controller/api/misc/ExtractImagesController.java index 206c78a2..91bbff09 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/misc/ExtractImagesController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/misc/ExtractImagesController.java @@ -20,6 +20,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestPart; @@ -30,6 +31,7 @@ import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.tags.Tag; +import stirling.software.SPDF.model.api.PDFWithImageFormatRequest; import stirling.software.SPDF.utils.WebResponseUtils; @RestController @Tag(name = "Other", description = "Other APIs") @@ -40,13 +42,9 @@ public class ExtractImagesController { @PostMapping(consumes = "multipart/form-data", value = "/extract-images") @Operation(summary = "Extract images from a PDF file", description = "This endpoint extracts images from a given PDF file and returns them in a zip file. Users can specify the output image format. Input:PDF Output:IMAGE/ZIP Type:SIMO") - public ResponseEntity extractImages( - @RequestPart(required = true, value = "fileInput") - @Parameter(description = "The input PDF file containing images") - MultipartFile file, - @RequestParam("format") - @Parameter(description = "The output image format e.g., 'png', 'jpeg', or 'gif'", schema = @Schema(allowableValues = {"png", "jpeg", "gif"})) - String format) throws IOException { + public ResponseEntity extractImages(@ModelAttribute PDFWithImageFormatRequest request) throws IOException { + MultipartFile file = request.getFileInput(); + String format = request.getFormat(); System.out.println(System.currentTimeMillis() + "file=" + file.getName() + ", format=" + format); PDDocument document = PDDocument.load(file.getBytes()); diff --git a/src/main/java/stirling/software/SPDF/controller/api/misc/FakeScanControllerWIP.java b/src/main/java/stirling/software/SPDF/controller/api/misc/FakeScanControllerWIP.java index e3ec8d5e..70fc2e88 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/misc/FakeScanControllerWIP.java +++ b/src/main/java/stirling/software/SPDF/controller/api/misc/FakeScanControllerWIP.java @@ -30,6 +30,7 @@ import org.apache.pdfbox.rendering.PDFRenderer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestPart; import org.springframework.web.bind.annotation.RestController; @@ -39,6 +40,7 @@ import io.swagger.v3.oas.annotations.Hidden; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.tags.Tag; +import stirling.software.SPDF.model.api.PDFFile; import stirling.software.SPDF.utils.WebResponseUtils; @RestController @@ -54,10 +56,8 @@ public class FakeScanControllerWIP { summary = "Repair a PDF file", description = "This endpoint repairs a given PDF file by running Ghostscript command. The PDF is first saved to a temporary location, repaired, read back, and then returned as a response." ) - public ResponseEntity repairPdf( - @RequestPart(required = true, value = "fileInput") - @Parameter(description = "The input PDF file to be repaired", required = true) - MultipartFile inputFile) throws IOException, InterruptedException { + public ResponseEntity repairPdf(@ModelAttribute PDFFile request) throws IOException { + MultipartFile inputFile = request.getFileInput(); PDDocument document = PDDocument.load(inputFile.getBytes()); PDFRenderer pdfRenderer = new PDFRenderer(document); diff --git a/src/main/java/stirling/software/SPDF/controller/api/misc/MetadataController.java b/src/main/java/stirling/software/SPDF/controller/api/misc/MetadataController.java index 059db19d..aa9b5395 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/misc/MetadataController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/misc/MetadataController.java @@ -11,6 +11,7 @@ import org.apache.pdfbox.cos.COSName; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDDocumentInformation; import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestPart; @@ -20,6 +21,7 @@ import org.springframework.web.multipart.MultipartFile; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.tags.Tag; +import stirling.software.SPDF.model.api.misc.MetadataRequest; import stirling.software.SPDF.utils.WebResponseUtils; @RestController @@ -41,43 +43,25 @@ public class MetadataController { @PostMapping(consumes = "multipart/form-data", value = "/update-metadata") @Operation(summary = "Update metadata of a PDF file", description = "This endpoint allows you to update the metadata of a given PDF file. You can add, modify, or delete standard and custom metadata fields. Input:PDF Output:PDF Type:SISO") - public ResponseEntity metadata( - @RequestPart(required = true, value = "fileInput") - @Parameter(description = "The input PDF file to update metadata") - MultipartFile pdfFile, - @RequestParam(value = "deleteAll", required = false, defaultValue = "false") - @Parameter(description = "Delete all metadata if set to true") - Boolean deleteAll, - @RequestParam(value = "author", required = false) - @Parameter(description = "The author of the document") - String author, - @RequestParam(value = "creationDate", required = false) - @Parameter(description = "The creation date of the document (format: yyyy/MM/dd HH:mm:ss)") - String creationDate, - @RequestParam(value = "creator", required = false) - @Parameter(description = "The creator of the document") - String creator, - @RequestParam(value = "keywords", required = false) - @Parameter(description = "The keywords for the document") - String keywords, - @RequestParam(value = "modificationDate", required = false) - @Parameter(description = "The modification date of the document (format: yyyy/MM/dd HH:mm:ss)") - String modificationDate, - @RequestParam(value = "producer", required = false) - @Parameter(description = "The producer of the document") - String producer, - @RequestParam(value = "subject", required = false) - @Parameter(description = "The subject of the document") - String subject, - @RequestParam(value = "title", required = false) - @Parameter(description = "The title of the document") - String title, - @RequestParam(value = "trapped", required = false) - @Parameter(description = "The trapped status of the document") - String trapped, - @Parameter(description = "Map list of key and value of custom parameters, note these must start with customKey and customValue if they are non standard") - @RequestParam Map allRequestParams) - throws IOException { + public ResponseEntity metadata(@ModelAttribute MetadataRequest request) throws IOException { + + // Extract PDF file from the request object + MultipartFile pdfFile = request.getFileInput(); + + // Extract metadata information + Boolean deleteAll = request.getDeleteAll(); + String author = request.getAuthor(); + String creationDate = request.getCreationDate(); + String creator = request.getCreator(); + String keywords = request.getKeywords(); + String modificationDate = request.getModificationDate(); + String producer = request.getProducer(); + String subject = request.getSubject(); + String title = request.getTitle(); + String trapped = request.getTrapped(); + + // Extract additional custom parameters + Map allRequestParams = request.getAllRequestParams(); // Load the PDF file into a PDDocument PDDocument document = PDDocument.load(pdfFile.getBytes()); diff --git a/src/main/java/stirling/software/SPDF/model/api/PDFWithImageFormatRequest.java b/src/main/java/stirling/software/SPDF/model/api/PDFWithImageFormatRequest.java new file mode 100644 index 00000000..9766fdbf --- /dev/null +++ b/src/main/java/stirling/software/SPDF/model/api/PDFWithImageFormatRequest.java @@ -0,0 +1,12 @@ +package stirling.software.SPDF.model.api; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +@Data +public class PDFWithImageFormatRequest extends PDFFile { + + @Schema(description = "The output image format e.g., 'png', 'jpeg', or 'gif'", + allowableValues = { "png", "jpeg", "gif" }) + private String format; +} diff --git a/src/main/java/stirling/software/SPDF/model/api/misc/MetadataRequest.java b/src/main/java/stirling/software/SPDF/model/api/misc/MetadataRequest.java new file mode 100644 index 00000000..586f83f5 --- /dev/null +++ b/src/main/java/stirling/software/SPDF/model/api/misc/MetadataRequest.java @@ -0,0 +1,44 @@ +package stirling.software.SPDF.model.api.misc; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; +import stirling.software.SPDF.model.api.PDFFile; + +import java.util.Map; + +@Data +public class MetadataRequest extends PDFFile { + + @Schema(description = "Delete all metadata if set to true") + private Boolean deleteAll; + + @Schema(description = "The author of the document") + private String author; + + @Schema(description = "The creation date of the document (format: yyyy/MM/dd HH:mm:ss)") + private String creationDate; + + @Schema(description = "The creator of the document") + private String creator; + + @Schema(description = "The keywords for the document") + private String keywords; + + @Schema(description = "The modification date of the document (format: yyyy/MM/dd HH:mm:ss)") + private String modificationDate; + + @Schema(description = "The producer of the document") + private String producer; + + @Schema(description = "The subject of the document") + private String subject; + + @Schema(description = "The title of the document") + private String title; + + @Schema(description = "The trapped status of the document") + private String trapped; + + @Schema(description = "Map list of key and value of custom parameters. Note these must start with customKey and customValue if they are non-standard") + private Map allRequestParams; +}