index.js
2.49 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
const imports = require('./import')['import'];
const Busboy = imports.Busboy;
const UUID = imports.UUID;
const aws = require('aws-sdk');
const s3 = new aws.S3();
exports.handler = async (event, context) => {
if(!event.body || !/^multipart\/form\-data/.test(event.headers['content-type'])) {
return {
statusCode: 400
}
}
const formData = await parse(event);
if(!formData['gif']) {
return {
statusCode: 400
}
}
const id = await upload(formData['gif']);
return {
statusCode: 200,
headers:{
"Content-Type":"json"
},
body: JSON.stringify({
id
}),
}
}
const parse = (event) => new Promise((resolve, reject) => {
const bodyBuffer = new Buffer(event.body.toString(), "base64");
const busboy = new Busboy({
headers: {
'content-type': event.headers['content-type']
}
});
const formData = {};
busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
console.log('File [%s]: filename=%j; encoding=%j; mimetype=%j', fieldname, filename, encoding, mimetype);
const chunks = [];
file.on('data', data => {
chunks.push(data);
}).on('end', () => {
formData[fieldname] = {
name:filename,
data:Buffer.concat(chunks),
mimetype:mimetype
};
console.log("File [%s] finished.", filename);
});
});
busboy.on('field', (fieldname, value) => {
console.log("[" + fieldname + "] >> " + value);
formData[fieldname] = value;
});
busboy.on('error', error => {
reject(error);
});
busboy.on('finish', () => {
resolve(formData);
});
busboy.write(bodyBuffer, event.isBase64Encoded ? 'base64' : 'binary');
busboy.end();
});
const upload = ({data, mimetype}) => new Promise((resolve, reject) => {
const bucket = 'gif-generator';
const path = '/gif';
const id = UUID().replace(/\-/g, '');
const fileFullName = path + '/' + id + '.gif';
const params = {
Bucket: bucket,
Key: fileFullName,
Body: data,
ContentType: mimetype
};
s3.upload(params, (err, data) => {
if(err){
console.log("upload err");
console.log(err);
reject(err);
}else{
console.log("upload success");
console.log(data);
resolve(id);
}
});
});