-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimize.ts
41 lines (34 loc) · 1.06 KB
/
optimize.ts
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
import * as AWS from 'aws-sdk';
import * as sharp from 'sharp';
import { basename, extname } from 'path';
const S3 = new AWS.S3();
export default async function handle ({ Payload }: AWS.S3.RecordsEvent) {
try {
const records: any = Payload
await Promise.all(
records.map(async record => {
const key: string = record.s3.object.key;
const bucket: string = process.env.bucket || '';
const image: AWS.S3.GetObjectOutput = await S3.getObject({
Bucket: bucket,
Key: key
}).promise();
const optimized = await sharp(<sharp.SharpOptions>image.Body)
.resize(1280, 720, { fit: 'inside', withoutEnlargement: true })
.toFormat('jpeg', { progressive: true, quality: 50 })
.toBuffer();
await S3.putObject({
Body: optimized,
Bucket: bucket,
ContentType: 'image/jpeg',
Key: `compressed/${basename(key, extname(key))}.jpg`
}).promise();
})
);
return {
statusCode: 204,
};
} catch (err) {
return err;
}
};