-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
148 lines (131 loc) · 4.27 KB
/
index.js
File metadata and controls
148 lines (131 loc) · 4.27 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// dependencies
const AWS = require("aws-sdk");
const util = require("util");
const sharp = require("sharp");
// get reference to S3 client
const s3 = new AWS.S3();
async function getOriginalImage(srcKey, srcBucket) {
// Download the image from the S3 source bucket.
try {
const params = {
Bucket: srcBucket,
Key: srcKey
};
let origimage = await s3.getObject(params).promise();
return origimage;
} catch (error) {
console.log(error);
return;
}
}
// image size in pixel (based on wordpress predetermined image sizes)
const imageSize = {
thumbnail: 150,
md: 300,
lg: 1024,
xlg: 1500
};
async function resizeImage(origimage) {
// set thumbnail width. Resize will set the height automatically to maintain aspect ratio.
// const width = 200;
return await Promise.all(
Object.keys(imageSize).map(async key => {
// Use the sharp module to resize the image and save in a buffer.
try {
let buffer = await sharp(origimage.Body)
.resize(imageSize[key])
.jpeg({
quality: 80
})
.toBuffer();
return buffer;
} catch (error) {
console.log(error);
return;
}
})
).catch(err => {
console.error(err);
});
}
async function uploadResizedImage(
buffers,
{ fileName, srcKey, srcBucket, imagesPathParam, avatarPathParam, dstBucket }
) {
const imageSizeKeys = Object.keys(imageSize)
let dstKey;
await Promise.all(
buffers.map(async (buffer, index) => {
const imgSize = imageSizeKeys[index];
dstKey = `${avatarPathParam}/${imagesPathParam}/compressed/${imgSize}/${fileName}-${imgSize}`
try {
const destparams = {
Bucket: dstBucket,
Key: dstKey,
Body: buffer,
ContentType: "image",
ACL: 'public-read'
};
const putResult = await s3.putObject(destparams).promise();
console.log('putResult', putResult);
} catch (error) {
console.log(error);
return;
}
})
).catch(err => {
console.error(err);
});
console.log(
"Successfully resized " +
srcBucket +
"/" +
srcKey +
" and uploaded to " +
dstBucket +
"/" +
dstKey
);
}
// avatar/images/raw
// avatar/images/compressed
// avatar/images/compressed/{sm/lg/md/xl}
exports.handler = async (event, context, callback) => {
// Read options from the event parameter.
console.log(
"Reading options from event:\n",
util.inspect(event, { depth: 5 })
);
const srcBucket = event.Records[0].s3.bucket.name;
// Object key may have spaces or unicode non-ASCII characters.
const srcKey = decodeURIComponent(
event.Records[0].s3.object.key.replace(/\+/g, " ")
);
console.log('srcKey', srcKey)
const dstBucket = srcBucket;
const keyPath = srcKey.split('/');
console.log(keyPath);
const avatarPathParam = keyPath[0];
const imagesPathParam = keyPath[1];
const rawPathParam = keyPath[2];
const filePathParam = keyPath[3];
// const dstKey = "resized-" + srcKey;
// Extract filename
const fileName = filePathParam.replace(/\.([^.]*)$/, "");
// Infer the image type from the file suffix.
// const typeMatch = srcKey.match(/\.([^.]*)$/);
// Check that the image type is supported
// const imageType = typeMatch[1].toLowerCase();
// if (imageType !== "jpg" && imageType !== "jpeg" && imageType !== "png") {
// console.log(`Unsupported image type: ${imageType}`);
// return;
// }
// Get the original Image
const origimage = await getOriginalImage(srcKey, srcBucket);
if (!origimage) return;
// Resize image
const buffer = await resizeImage(origimage);
if (!buffer) return;
// Upload the resized image to the destination bucket
await uploadResizedImage(buffer, { fileName, srcKey, srcBucket, dstBucket, rawPathParam, imagesPathParam, avatarPathParam });
};