|
| 1 | +package dev.ancaghenade.shipmentpicturelambdavalidator; |
| 2 | + |
| 3 | +import com.amazonaws.services.lambda.runtime.Context; |
| 4 | +import com.amazonaws.services.lambda.runtime.LambdaLogger; |
| 5 | +import com.amazonaws.services.lambda.runtime.RequestHandler; |
| 6 | +import com.amazonaws.services.lambda.runtime.events.S3Event; |
| 7 | +import com.amazonaws.services.s3.AmazonS3; |
| 8 | +import com.amazonaws.services.s3.event.S3EventNotification.S3EventNotificationRecord; |
| 9 | +import com.amazonaws.services.s3.model.ObjectMetadata; |
| 10 | +import com.amazonaws.services.s3.model.PutObjectRequest; |
| 11 | +import com.amazonaws.services.s3.model.S3Object; |
| 12 | +import java.io.IOException; |
| 13 | +import java.io.InputStream; |
| 14 | +import java.util.Arrays; |
| 15 | +import java.util.List; |
| 16 | +import org.apache.http.entity.ContentType; |
| 17 | + |
| 18 | +// Should be something more interesting |
| 19 | + |
| 20 | +public class ServiceHandler implements RequestHandler<S3Event, Void> { |
| 21 | + |
| 22 | + public Void handleRequest(S3Event event, Context context) { |
| 23 | + |
| 24 | + AmazonS3 s3Client = null; |
| 25 | + try { |
| 26 | + s3Client = S3ClientHelper.getS3Client(); |
| 27 | + } catch (IOException e) { |
| 28 | + throw new RuntimeException(e); |
| 29 | + } |
| 30 | + |
| 31 | + LambdaLogger logger = context.getLogger(); |
| 32 | + boolean isValid = true; |
| 33 | + |
| 34 | + // check if record is there |
| 35 | + if (event.getRecords().isEmpty()) { |
| 36 | + logger.log("No records received."); |
| 37 | + return null; |
| 38 | + } |
| 39 | + |
| 40 | + for (S3EventNotificationRecord record : event.getRecords()) { |
| 41 | + String bucketName = record.getS3().getBucket().getName(); |
| 42 | + String objectKey = record.getS3().getObject().getKey(); |
| 43 | + |
| 44 | + S3Object s3Object = s3Client.getObject(bucketName, objectKey); |
| 45 | + |
| 46 | + if (!List.of(ContentType.IMAGE_JPEG.getMimeType(), |
| 47 | + ContentType.IMAGE_PNG.getMimeType(), |
| 48 | + ContentType.IMAGE_BMP.getMimeType()) |
| 49 | + .contains(s3Object.getObjectMetadata().getUserMetadata().get("content-type"))) { |
| 50 | + |
| 51 | + isValid = false; |
| 52 | + logger.log( |
| 53 | + "File format not accepted. This will be replaced with a standard placeholder."); |
| 54 | + } |
| 55 | + if (isValid) { |
| 56 | + byte[] magicNumbers = new byte[4]; |
| 57 | + InputStream objectData = s3Object.getObjectContent(); |
| 58 | + try { |
| 59 | + objectData.read(magicNumbers, 0, 4); |
| 60 | + } catch (IOException e) { |
| 61 | + throw new RuntimeException(e); |
| 62 | + } |
| 63 | + if (Arrays.equals(magicNumbers, new byte[]{(byte) 0x7f, 'E', 'L', 'F'})) { |
| 64 | + logger.log("The object is an ELF executable file."); |
| 65 | + isValid = false; |
| 66 | + |
| 67 | + } else if (Arrays.equals(magicNumbers, new byte[]{'M', 'Z'})) { |
| 68 | + logger.log("The object is a Windows executable file."); |
| 69 | + isValid = false; |
| 70 | + |
| 71 | + } |
| 72 | + } |
| 73 | + if (!isValid) { |
| 74 | + s3Client.deleteObject(bucketName, objectKey); |
| 75 | + |
| 76 | + InputStream is = ServiceHandler.class.getResourceAsStream("/resources/placeholder.jpg"); |
| 77 | + if (is == null) { |
| 78 | + is = ServiceHandler.class.getClassLoader().getResourceAsStream("placeholder.jpg"); |
| 79 | + } |
| 80 | + |
| 81 | + s3Client.putObject(new PutObjectRequest(bucketName, objectKey, is, new ObjectMetadata())); |
| 82 | + |
| 83 | + } else { |
| 84 | + logger.log( |
| 85 | + "Found image with content type: " + s3Object.getObjectMetadata().getUserMetadata() |
| 86 | + .get("content-type") + " that is correct."); |
| 87 | + } |
| 88 | + |
| 89 | + } |
| 90 | + return null; |
| 91 | + } |
| 92 | +} |
0 commit comments