-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.js
56 lines (47 loc) · 1.19 KB
/
handler.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
const aws = require('aws-sdk');
const process = require('./src/process');
const processMeta = require('./src/processMeta');
const prepareMeta = require('./src/prepareMeta');
const s3 = new aws.S3({
apiVersion: '2006-03-01'
});
module.exports.processor = async ({
allow_webp: allowWebp = false,
bucket,
filename,
new_filename: newFilename,
operations = [],
quality = 82,
'return': output,
}) => {
const {
ACL: acl,
Body: body,
Metadata: metadata,
} = await s3.getObject({
Bucket: bucket,
Key: filename,
}).promise();
const { buffer, mime } = await process(body, operations, quality, allowWebp);
if (output === 'stream') {
return buffer.toString('base64');
}
const params = {
ACL: acl,
Body: buffer,
Bucket: bucket,
ContentType: mime,
Key: newFilename,
};
if (!operations.length) {
const meta = await processMeta(body);
if (meta !== null) {
params.Metadata = {
...metadata,
...prepareMeta(meta),
};
}
}
await s3.putObject(params).promise();
return { mime };
};