forked from koyeb-community/image-resize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage-resize.js
105 lines (84 loc) · 2.82 KB
/
image-resize.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/** @format */
"use strict";
const S3 = require("aws-sdk/clients/s3");
const Jimp = require("jimp");
const resizedImageFormats = {
jpeg: Jimp.MIME_JPEG,
png: Jimp.MIME_PNG,
bmp: Jimp.MIME_BMP,
};
const prefix = process.env.IMAGE_RESIZE_PATH || "resized-images/";
const imageResize = async (s3Instance, bucket, key, watermarkImageUrl) => {
try {
let width = process.env.IMAGE_RESIZE_WIDTH
? parseInt(process.env.IMAGE_RESIZE_WIDTH)
: Jimp.AUTO;
let height = process.env.IMAGE_RESIZE_HEIGHT
? parseInt(process.env.IMAGE_RESIZE_HEIGHT)
: Jimp.AUTO;
const resizedImageFormat = process.env.IMAGE_RESIZE_FORMAT || "jpeg";
if (!(resizedImageFormat in resizedImageFormats)) {
throw new Error("Unsupported MIME type.");
}
const response = await s3Instance
.getObject({
Bucket: bucket,
Key: key,
})
.promise();
const image = await Jimp.read(response.Body);
image.resize(width, height);
const buffer = await image.getBufferAsync(
resizedImageFormats[resizedImageFormat]
);
await s3Instance
.upload({
Bucket: bucket,
Key: `${prefix}${key}`,
Body: buffer,
ContentType: resizedImageFormats[resizedImageFormat],
})
.promise();
} catch (error) {
throw error;
}
};
const getS3Configuration = (sourceBucket) => {
return {
accessKeyId: process.env[`KOYEB_STORE_${sourceBucket}_ACCESS_KEY`],
secretAccessKey: process.env[`KOYEB_STORE_${sourceBucket}_SECRET_KEY`],
region: process.env[`KOYEB_STORE_${sourceBucket}_REGION`],
endpoint: process.env[`KOYEB_STORE_${sourceBucket}_ENDPOINT`],
};
};
const validateEnvironment = (sourceBucket) => {
if (!sourceBucket) {
throw new Error("Bucket name not present in event payload.");
}
if (
!process.env?.[`KOYEB_STORE_${sourceBucket}_ACCESS_KEY`] ||
!process.env?.[`KOYEB_STORE_${sourceBucket}_SECRET_KEY`] ||
!process.env?.[`KOYEB_STORE_${sourceBucket}_REGION`] ||
!process.env?.[`KOYEB_STORE_${sourceBucket}_ENDPOINT`]
) {
throw new Error(
`One of the following environment variables are missing: KOYEB_STORE_${sourceBucket}_ACCESS_KEY, KOYEB_STORE_${sourceBucket}_SECRET_KEY, KOYEB_STORE_${sourceBucket}_ENDPOINT, KOYEB_STORE_${sourceBucket}_REGION.`
);
}
if (!process.env?.IMAGE_RESIZE_WIDTH && !process.env?.IMAGE_RESIZE_HEIGHT) {
throw new Error(
`One of the following environment variables are missing: IMAGE_RESIZE_WIDTH, IMAGE_RESIZE_HEIGHT.`
);
}
};
const handler = async (event) => {
const bucket = event?.bucket?.name;
const key = event?.object?.key;
if (key.startsWith(prefix)) {
return;
}
validateEnvironment(bucket);
const s3Instance = new S3(getS3Configuration(bucket));
await imageResize(s3Instance, bucket, key);
};
module.exports.handler = handler;