Skip to content

Commit

Permalink
Revert "add a method to get s3 object as file, add utils to parse pdf…
Browse files Browse the repository at this point in the history
… file to text."

This reverts commit 9226a22.
  • Loading branch information
Joel Balcaen committed Apr 3, 2024
1 parent a5f1563 commit c386178
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 80 deletions.
6 changes: 0 additions & 6 deletions lambdas/FormProcessor/FormRequestProcessorFunction/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,6 @@
<artifactId>jackson-datatype-joda</artifactId>
<version>2.15.3</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>3.0.2</version>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,13 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.joda.JodaModule;
import com.levio.awsdemo.formrequestprocessor.service.*;
import com.levio.awsdemo.formrequestprocessor.utils.PDF;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
import org.apache.pdfbox.cos.COSDocument;
import org.apache.pdfbox.io.RandomAccessRead;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

import java.io.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -73,14 +69,14 @@ public Void handleRequest(final SQSEvent input, final Context context) {
var formKey = formFillRequest.getFormKey();
var questionsMapper = retrieveDocumentMapper(formKey);

String email = s3Service.getObjectAsString(formKey + "/email/" + formFillRequest.getEmailId());
String email = s3Service.getFile(formKey + "/email/" + formFillRequest.getEmailId());
try {
MimeMessage message = mailService.getMimeMessage(new ByteArrayInputStream(email.getBytes(StandardCharsets.UTF_8)));
String emailBody = "Formulaire response";
String sender = ((InternetAddress) message.getFrom()[0]).getAddress();
String subject = message.getSubject();

String content = getS3ObjectContent(attachmentKey);
String content = s3Service.getFile(attachmentKey);

questionsMapper.entrySet().parallelStream()
.forEach(positionQuestionAnswerMapper -> {
Expand Down Expand Up @@ -122,24 +118,6 @@ private static Map<String, SQSEvent.MessageAttribute> getMessageAttributes(Strin
return messageAttributes;
}


private String getS3ObjectContent(String key) {
final var isPDF = key.endsWith(".pdf");

try {
if (isPDF) {
final var file = s3Service.getObjectAsFile(key);
return PDF.generateTextFromPDF(file);
} else {
return s3Service.getObjectAsString(key);
}
} catch(IOException e) {
System.out.print(e);
}

return s3Service.getObjectAsString(key);
}

private HashMap<Integer, Map<String, String>> retrieveDocumentMapper(String formKey) {
try {
return documentService.retrieveQuestionsMapper(formKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
import software.amazon.awssdk.services.s3.model.PutObjectResponse;

import java.io.*;
import java.io.InputStream;

public class S3Service {

Expand All @@ -18,27 +18,11 @@ public class S3Service {
private final S3Client s3 = S3Client.builder()
.region(Region.US_EAST_1)
.build();

public String getObjectAsString(String key) {
public String getFile(String key) {
ResponseBytes<GetObjectResponse> objectBytes = getObjectResponseBytes(key);
return new String(objectBytes.asByteArray());
}

public File getObjectAsFile(String key) {
try {
ResponseBytes<GetObjectResponse> objectBytes = getObjectResponseBytes(key);
final var file = new File("/tmp/"+key);
OutputStream os = new FileOutputStream(file);
os.write(objectBytes.asByteArray());
os.close();
return file;
} catch (IOException e) {
e.printStackTrace();
}

return null;
}

public InputStream getInputFileStream(String key) {
ResponseBytes<GetObjectResponse> objectBytes = getObjectResponseBytes(key);
return objectBytes.asInputStream();
Expand All @@ -52,10 +36,8 @@ private ResponseBytes<GetObjectResponse> getObjectResponseBytes(String key) {
.build();

return s3.getObjectAsBytes(objectRequest);

}


public String saveFile(String fileKey, byte[] fileContent) {
PutObjectResponse objectResponse = s3.putObject(
PutObjectRequest.builder()
Expand Down

This file was deleted.

0 comments on commit c386178

Please sign in to comment.