diff --git a/pom.xml b/pom.xml
index 37bdb8c..ccc94a8 100644
--- a/pom.xml
+++ b/pom.xml
@@ -60,7 +60,7 @@
org.apache.pdfbox
pdfbox
- 2.0.29
+ 3.0.0
diff --git a/src/main/java/fr/istic/service/ScanService.java b/src/main/java/fr/istic/service/ScanService.java
index 7eeb323..169853e 100644
--- a/src/main/java/fr/istic/service/ScanService.java
+++ b/src/main/java/fr/istic/service/ScanService.java
@@ -13,12 +13,20 @@
import io.minio.errors.ServerException;
import io.minio.errors.XmlParserException;
+import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.pdfbox.io.MemoryUsageSetting;
+import org.apache.pdfbox.io.RandomAccessReadBuffer;
+import org.apache.pdfbox.io.RandomAccessReadBufferedFile;
+import org.apache.pdfbox.io.RandomAccessStreamCache;
+import org.apache.pdfbox.io.ScratchFile;
+import org.apache.pdfbox.io.RandomAccessStreamCache.StreamCacheCreateFunction;
import org.apache.pdfbox.multipdf.PDFMergerUtility;
import org.apache.pdfbox.multipdf.PDFMergerUtility.DocumentMergeMode;
+import org.apache.pdfbox.pdmodel.PDDocument;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput;
+import org.simpleframework.xml.Path;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -27,13 +35,13 @@
import javax.transaction.Transactional;
import java.io.ByteArrayOutputStream;
+import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Optional;
-
import org.jboss.resteasy.plugins.providers.multipart.InputPart;
import javax.ws.rs.core.MultivaluedMap;
@@ -44,7 +52,6 @@
import java.util.List;
import java.util.Map;
-
@ApplicationScoped
@Transactional
public class ScanService {
@@ -88,20 +95,24 @@ public ScanDTO persistOrUpdate(ScanDTOContent scanDTO) {
// byte[] bytes = scanDTO.content;
scan.content = null;
scan = Scan.persistOrUpdate(scan);
- /*try {
-
- fichierS3Service.putObject("scan/" + scan.id + ".pdf", bytes, scanDTO.contentContentType);
- } catch (InvalidKeyException | NoSuchAlgorithmException | IllegalArgumentException | IOException e) {
- e.printStackTrace();
- }*/
+ /*
+ * try {
+ *
+ * fichierS3Service.putObject("scan/" + scan.id + ".pdf", bytes,
+ * scanDTO.contentContentType);
+ * } catch (InvalidKeyException | NoSuchAlgorithmException |
+ * IllegalArgumentException | IOException e) {
+ * e.printStackTrace();
+ * }
+ */
ScanDTO dto = scanMapper.toDto(scan);
- // dto.content = null;
+ // dto.content = null;
return dto;
} else {
scan.content = null;
scan = Scan.persistOrUpdate(scan);
ScanDTO dto = scanMapper.toDto(scan);
- // dto.content = null;
+ // dto.content = null;
return dto;
}
@@ -148,31 +159,37 @@ public Optional findOne(Long id) {
Optional scanop = Scan.findByIdOptional(id);
Optional dto = scanop
.map(scan -> scanMapper.toDto((Scan) scan));
- /* if (this.uses3) {
- if (dto.isPresent()) {
- ScanDTOContent scan = dto.get();
-
- if (this.fichierS3Service.isObjectExist("scan/" + scan.id + ".pdf")) {
- byte[] bytes;
- try {
- bytes = IOUtils.toByteArray(this.fichierS3Service.getObject("scan/" + scan.id + ".pdf"));
- scan.content = bytes;
-
- } catch (InvalidKeyException | NoSuchAlgorithmException | IllegalArgumentException
- | IOException e) {
- e.printStackTrace();
- }
- } else {
- byte[] bytes = scan.content;
- try {
- fichierS3Service.putObject("scan/" + scan.id + ".pdf", bytes, scan.contentContentType);
- } catch (InvalidKeyException | NoSuchAlgorithmException | IllegalArgumentException
- | IOException e) {
- e.printStackTrace();
- }
- }
- }
- } */
+ /*
+ * if (this.uses3) {
+ * if (dto.isPresent()) {
+ * ScanDTOContent scan = dto.get();
+ *
+ * if (this.fichierS3Service.isObjectExist("scan/" + scan.id + ".pdf")) {
+ * byte[] bytes;
+ * try {
+ * bytes = IOUtils.toByteArray(this.fichierS3Service.getObject("scan/" + scan.id
+ * + ".pdf"));
+ * scan.content = bytes;
+ *
+ * } catch (InvalidKeyException | NoSuchAlgorithmException |
+ * IllegalArgumentException
+ * | IOException e) {
+ * e.printStackTrace();
+ * }
+ * } else {
+ * byte[] bytes = scan.content;
+ * try {
+ * fichierS3Service.putObject("scan/" + scan.id + ".pdf", bytes,
+ * scan.contentContentType);
+ * } catch (InvalidKeyException | NoSuchAlgorithmException |
+ * IllegalArgumentException
+ * | IOException e) {
+ * e.printStackTrace();
+ * }
+ * }
+ * }
+ * }
+ */
return dto;
}
@@ -200,32 +217,37 @@ public Paged findbyName(String name, Page page) {
Paged dtos = scans.map(scan -> scanMapper.toDto((Scan) scan));
/*
- if (this.uses3) {
-
- for (ScanDTOContent scan : dtos.content) {
-
- if (this.fichierS3Service.isObjectExist("scan/" + scan.id + ".pdf")) {
- byte[] bytes;
- try {
- bytes = IOUtils.toByteArray(this.fichierS3Service.getObject("scan/" + scan.id + ".pdf"));
- scan.content = bytes;
-
- } catch (InvalidKeyException | NoSuchAlgorithmException | IllegalArgumentException
- | IOException e) {
- e.printStackTrace();
- }
- } else {
- byte[] bytes = scan.content;
- try {
- fichierS3Service.putObject("scan/" + scan.id + ".pdf", bytes, scan.contentContentType);
- } catch (InvalidKeyException | NoSuchAlgorithmException | IllegalArgumentException
- | IOException e) {
- e.printStackTrace();
- }
-
- }
- }
- }*/
+ * if (this.uses3) {
+ *
+ * for (ScanDTOContent scan : dtos.content) {
+ *
+ * if (this.fichierS3Service.isObjectExist("scan/" + scan.id + ".pdf")) {
+ * byte[] bytes;
+ * try {
+ * bytes = IOUtils.toByteArray(this.fichierS3Service.getObject("scan/" + scan.id
+ * + ".pdf"));
+ * scan.content = bytes;
+ *
+ * } catch (InvalidKeyException | NoSuchAlgorithmException |
+ * IllegalArgumentException
+ * | IOException e) {
+ * e.printStackTrace();
+ * }
+ * } else {
+ * byte[] bytes = scan.content;
+ * try {
+ * fichierS3Service.putObject("scan/" + scan.id + ".pdf", bytes,
+ * scan.contentContentType);
+ * } catch (InvalidKeyException | NoSuchAlgorithmException |
+ * IllegalArgumentException
+ * | IOException e) {
+ * e.printStackTrace();
+ * }
+ *
+ * }
+ * }
+ * }
+ */
return dtos;
}
@@ -237,26 +259,23 @@ public void uploadFile(MultipartFormDataInput input, long examId, boolean merge)
String fileName = null;
for (InputPart inputPart : inputParts) {
try {
- MultivaluedMap header =
- inputPart.getHeaders();
+ MultivaluedMap header = inputPart.getHeaders();
fileName = getFileName(header);
fileNames.add(fileName);
InputStream inputStream = inputPart.getBody(InputStream.class, null);
- if (this.hasScanFile(examId) && merge){
- mergeFile(inputStream,"application/pdf", examId);
+ if (this.hasScanFile(examId) && merge) {
+ mergeFile(inputStream, "application/pdf", examId);
} else {
- writeFile(inputStream,"application/pdf", examId);
- }
- } catch (Exception e) {
- e.printStackTrace();
+ writeFile(inputStream, "application/pdf", examId);
}
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
}
}
-
protected String getFileName(MultivaluedMap header) {
- String[] contentDisposition = header.
- getFirst("Content-Disposition").split(";");
+ String[] contentDisposition = header.getFirst("Content-Disposition").split(";");
for (String filename : contentDisposition) {
if ((filename.trim().startsWith("filename"))) {
String[] name = filename.split("=");
@@ -267,85 +286,93 @@ protected String getFileName(MultivaluedMap header) {
return "";
}
-
- protected boolean hasScanFile(long scanId){
- String fileName = "scan/" + scanId + ".pdf";
+ protected boolean hasScanFile(long scanId) {
+ String fileName = "scan/" + scanId + ".pdf";
return this.fichierS3Service.isObjectExist(fileName);
}
- protected InputStream getScanFile(long scanId) throws InvalidKeyException, NoSuchAlgorithmException, IllegalArgumentException, IOException{
- String fileName = "scan/" + scanId + ".pdf";
+ protected InputStream getScanFile(long scanId)
+ throws InvalidKeyException, NoSuchAlgorithmException, IllegalArgumentException, IOException {
+ String fileName = "scan/" + scanId + ".pdf";
return this.fichierS3Service.getObject(fileName);
}
protected void mergeFile(InputStream inputStream, String contenttype, long scanId)
throws IOException {
+ try {
+ PDFMergerUtility merger = new PDFMergerUtility();
+ java.nio.file.Path toadd = Files.createTempFile("toadd" + scanId, ".pdf");
+ java.nio.file.Path scan = Files.createTempFile("scan" + scanId, ".pdf");
+ java.nio.file.Path res = Files.createTempFile("res" + scanId, ".pdf");
+ FileOutputStream fo = new FileOutputStream(res.toFile());
+ FileUtils.copyInputStreamToFile(inputStream, toadd.toFile());
+ FileUtils.copyInputStreamToFile(this.getScanFile(scanId), scan.toFile());
+
+ PDDocument src = org.apache.pdfbox.Loader.loadPDF(new RandomAccessReadBufferedFile(scan.toFile()),
+ org.apache.pdfbox.io.IOUtils.createTempFileOnlyStreamCache());
+ PDDocument dst = org.apache.pdfbox.Loader.loadPDF(new RandomAccessReadBufferedFile(toadd.toFile()),
+ org.apache.pdfbox.io.IOUtils.createTempFileOnlyStreamCache());
+ merger.setDocumentMergeMode(DocumentMergeMode.PDFBOX_LEGACY_MODE);
+ merger.appendDocument(src, dst);
+ src.save(fo);
+ fo.close();
+
+ byte[] bytes = IOUtils.toByteArray(new FileInputStream(res.toFile()));
+ if (this.uses3) {
+ String fileName = "scan/" + scanId + ".pdf";
try {
- PDFMergerUtility merger = new PDFMergerUtility();
- // FileOutputStream fo = new FileOutputStream(Files.createTempFile("id"+scanId,"pdf").toFile());
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- merger.setDestinationStream(out);
- merger.setDocumentMergeMode(DocumentMergeMode.PDFBOX_LEGACY_MODE);
- merger.addSource(this.getScanFile(scanId));
- merger.addSource(inputStream);
- merger.mergeDocuments(MemoryUsageSetting.setupTempFileOnly());
-
- byte[] bytes = out.toByteArray();
- if (this.uses3){
- String fileName = "scan/" + scanId + ".pdf";
- try {
-
- this.putObject(fileName, bytes,contenttype);
- } catch (InvalidKeyException | NoSuchAlgorithmException | IllegalArgumentException e) {
- e.printStackTrace();
- }
- } else {
- this.updateContent(scanId, bytes);
-
- }
+ this.putObject(fileName, bytes, contenttype);
} catch (InvalidKeyException | NoSuchAlgorithmException | IllegalArgumentException e) {
e.printStackTrace();
}
+ } else {
+ this.updateContent(scanId, bytes);
+ }
+ scan.toFile().delete();
+ toadd.toFile().delete();
+ res.toFile().delete();
- // Base64.Decoder encoder = Base64.getDecoder();
- // byte[] b64bytes = encoder.decode(bytes);
+ } catch (InvalidKeyException | NoSuchAlgorithmException | IllegalArgumentException e) {
+ e.printStackTrace();
+ }
- }
+ // Base64.Decoder encoder = Base64.getDecoder();
+ // byte[] b64bytes = encoder.decode(bytes);
+ }
protected void writeFile(InputStream inputStream, String contenttype, long scanId)
throws IOException {
byte[] bytes = IOUtils.toByteArray(inputStream);
- // Base64.Decoder encoder = Base64.getDecoder();
- // byte[] b64bytes = encoder.decode(bytes);
+ // Base64.Decoder encoder = Base64.getDecoder();
+ // byte[] b64bytes = encoder.decode(bytes);
- if (this.uses3){
- String fileName = "scan/" + scanId + ".pdf";
+ if (this.uses3) {
+ String fileName = "scan/" + scanId + ".pdf";
try {
- this.putObject(fileName, bytes,contenttype);
+ this.putObject(fileName, bytes, contenttype);
} catch (InvalidKeyException | NoSuchAlgorithmException | IllegalArgumentException e) {
e.printStackTrace();
}
} else {
- this.updateContent(scanId, bytes);
+ this.updateContent(scanId, bytes);
}
}
@Transactional
- protected void updateContent(long scanId, byte[] b64bytes){
- Scan s = Scan.findById(scanId);
+ protected void updateContent(long scanId, byte[] b64bytes) {
+ Scan s = Scan.findById(scanId);
s.content = b64bytes;
s.persistOrUpdate();
}
-
- protected void putObject(String name, byte[] bytes, String contenttype) throws InvalidKeyException, NoSuchAlgorithmException, IllegalArgumentException, IOException {
+ protected void putObject(String name, byte[] bytes, String contenttype)
+ throws InvalidKeyException, NoSuchAlgorithmException, IllegalArgumentException, IOException {
this.fichierS3Service.putObject(name, bytes, contenttype);
-}
-
+ }
}
diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties
index bd9dbe7..00fe0c3 100644
--- a/src/main/resources/application.properties
+++ b/src/main/resources/application.properties
@@ -167,7 +167,8 @@ quarkus.swagger-ui.path=/swagger-ui
quarkus.native.additional-build-args=\
-H:ResourceConfigurationFiles=resources-config.json,\
- --initialize-at-run-time=fr.istic.security.RandomUtil
+ --initialize-at-run-time=fr.istic.security.RandomUtil,\
+ --trace-object-instantiation=org.apache.fontbox.ttf.BufferedRandomAccessFile
quarkus.http.limits.max-body-size=10G
upload.directory= /tmp/files