-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhandler.js
60 lines (55 loc) · 1.68 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
57
58
59
60
'use strict';
const {spawnSync} = require('child_process');
const {readFileSync, writeFileSync, unlinkSync} = require('fs');
const AWS = require('aws-sdk');
const s3 = new AWS.S3();
module.exports.gifmaker = async event => {
if (!event.Records) {
console.log('not an s3 invocation!');
return;
}
for (const record of event.Records) {
if (!record.s3) {
console.log('not an s3 invocation!');
continue;
}
if (record.s3.object.key.endsWith('.gif')) {
console.log('already a gif');
continue;
}
// get the file
const s3Object = await s3
.getObject({
Bucket: record.s3.bucket.name,
Key: record.s3.object.key,
})
.promise();
// write file to disk
writeFileSync(`/tmp/${record.s3.object.key}`, s3Object.Body);
// convert to gif!
spawnSync(
'/opt/ffmpeg/ffmpeg',
[
'-i',
`/tmp/${record.s3.object.key}`,
'-f',
'gif',
`/tmp/${record.s3.object.key}.gif`,
],
{stdio: 'inherit'}
);
// read gif from disk
const gifFile = readFileSync(`/tmp/${record.s3.object.key}.gif`);
// delete the temp files
unlinkSync(`/tmp/${record.s3.object.key}.gif`);
unlinkSync(`/tmp/${record.s3.object.key}`);
// upload gif to s3
await s3
.putObject({
Bucket: record.s3.bucket.name,
Key: `${record.s3.object.key}.gif`,
Body: gifFile,
})
.promise();
}
};