Skip to content

Commit

Permalink
update for merge large pdf
Browse files Browse the repository at this point in the history
  • Loading branch information
barais committed Nov 3, 2023
1 parent b1101b9 commit 5241a29
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 116 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.29</version>
<version>3.0.0</version>
</dependency>

<dependency>
Expand Down
255 changes: 141 additions & 114 deletions src/main/java/fr/istic/service/ScanService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -44,7 +52,6 @@
import java.util.List;
import java.util.Map;


@ApplicationScoped
@Transactional
public class ScanService {
Expand Down Expand Up @@ -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;

}
Expand Down Expand Up @@ -148,31 +159,37 @@ public Optional<ScanDTO> findOne(Long id) {
Optional<Scan> scanop = Scan.findByIdOptional(id);
Optional<ScanDTO> 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;
}

Expand Down Expand Up @@ -200,32 +217,37 @@ public Paged<ScanDTO> findbyName(String name, Page page) {
Paged<ScanDTO> 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;
}

Expand All @@ -237,26 +259,23 @@ public void uploadFile(MultipartFormDataInput input, long examId, boolean merge)
String fileName = null;
for (InputPart inputPart : inputParts) {
try {
MultivaluedMap<String, String> header =
inputPart.getHeaders();
MultivaluedMap<String, String> 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<String, String> 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("=");
Expand All @@ -267,85 +286,93 @@ protected String getFileName(MultivaluedMap<String, String> 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);
}

}

}
3 changes: 2 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5241a29

Please sign in to comment.