Skip to content

Commit

Permalink
adding extension to media in S3 upload and clean up switchCase before…
Browse files Browse the repository at this point in the history
… sending to S3
  • Loading branch information
Sachin Karve committed Jul 1, 2024
1 parent 7323639 commit 0bfacf3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
28 changes: 21 additions & 7 deletions src/main/java/com/meta/cp4m/S3PreProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

package com.meta.cp4m;
import com.meta.cp4m.message.Message;
import com.meta.cp4m.message.Payload;
import com.meta.cp4m.message.ThreadState;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -30,10 +31,20 @@ public class S3PreProcessor<T extends Message> implements PreProcessor<T> {

@Override
public ThreadState<T> run(ThreadState<T> in) {
if(in.tail().payload().getClass().getName().contains("Image") || in.tail().payload().getClass().getName().contains("Document")) {
this.sendRequest((byte[]) in.tail().payload().value(), in.tail().senderId().toString());

switch (in.tail().payload()) {
case Payload.Image i -> {
this.sendRequest(i.value(), in.userId().toString(), i.extension());
}
case Payload.Document i -> {
this.sendRequest(i.value(), in.userId().toString(), i.extension());
}
default -> {
System.out.println("Here");
return in;
}
}
return null;
return in; // TODO: remove last message
}

public S3PreProcessor(String awsAccessKeyID, String awsSecretAccessKey, String region, String bucket) {
Expand All @@ -56,13 +67,16 @@ public S3PreProcessor(String awsAccessKeyID, String awsSecretAccessKey, String r
.build();
}

public void sendRequest(byte[] media, String senderID) {
String key = senderID + "_" + Instant.now().toEpochMilli();
PutObjectResponse res = s3Client.putObject(PutObjectRequest.builder().bucket(this.bucket).key(key).contentType("application/*")
public void sendRequest(byte[] media, String senderID, String extension) {
String key = senderID +
'_' +
Instant.now().toEpochMilli() +
'.' +
extension;
PutObjectResponse res = s3Client.putObject(PutObjectRequest.builder().bucket(this.bucket).key(key).contentType("application/" + extension)
.build(),
RequestBody.fromBytes(media));
s3Client.close();
LOGGER.info("Media uploaded to AWS S3");

}
}
5 changes: 2 additions & 3 deletions src/main/java/com/meta/cp4m/S3PreProcessorConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import com.meta.cp4m.message.Message;
import java.util.Objects;

public class S3PreProcessorConfig implements PreProcessorConfig{
public class S3PreProcessorConfig implements PreProcessorConfig {
private final String name;
private final String awsAccessKeyId;
private final String awsSecretAccessKey;
Expand All @@ -26,7 +26,7 @@ public S3PreProcessorConfig(
@JsonProperty("aws_access_key_id") String awsAccessKeyId,
@JsonProperty("aws_secret_access_key") String awsSecretAccessKey,
@JsonProperty("region") String region,
@JsonProperty("bucket") String bucket){
@JsonProperty("bucket") String bucket) {
this.name = Objects.requireNonNull(name, "name is a required parameter");
this.awsAccessKeyId = Objects.requireNonNull(awsAccessKeyId, "aws access key is a required parameter");
this.awsSecretAccessKey = Objects.requireNonNull(awsSecretAccessKey, "aws secret access key is a required parameter");
Expand Down Expand Up @@ -60,4 +60,3 @@ public <T extends Message> PreProcessor<T> toPreProcessor() {
return new S3PreProcessor<>(awsAccessKeyId, awsSecretAccessKey, region, bucket);
}
}

0 comments on commit 0bfacf3

Please sign in to comment.