Showing
1 changed file
with
101 additions
and
0 deletions
lambda/gif-upload/src/index.js
0 → 100644
1 | +const imports = require('./import')['import']; | ||
2 | + | ||
3 | +const Busboy = imports.Busboy; | ||
4 | +const UUID = imports.UUID; | ||
5 | +const aws = require('aws-sdk'); | ||
6 | +const s3 = new aws.S3(); | ||
7 | + | ||
8 | +exports.handler = async (event, context) => { | ||
9 | + if(!event.body || !/^multipart\/form\-data/.test(event.headers['content-type'])) { | ||
10 | + return { | ||
11 | + statusCode: 400 | ||
12 | + } | ||
13 | + } | ||
14 | + | ||
15 | + const formData = await parse(event); | ||
16 | + | ||
17 | + if(!formData['gif']) { | ||
18 | + return { | ||
19 | + statusCode: 400 | ||
20 | + } | ||
21 | + } | ||
22 | + | ||
23 | + const id = await upload(formData['gif']); | ||
24 | + return { | ||
25 | + statusCode: 200, | ||
26 | + headers:{ | ||
27 | + "Content-Type":"json" | ||
28 | + }, | ||
29 | + body: JSON.stringify({ | ||
30 | + id | ||
31 | + }), | ||
32 | + } | ||
33 | +} | ||
34 | + | ||
35 | +const parse = (event) => new Promise((resolve, reject) => { | ||
36 | + const bodyBuffer = new Buffer(event.body.toString(), "base64"); | ||
37 | + | ||
38 | + const busboy = new Busboy({ | ||
39 | + headers: { | ||
40 | + 'content-type': event.headers['content-type'] | ||
41 | + } | ||
42 | + }); | ||
43 | + const formData = {}; | ||
44 | + | ||
45 | + busboy.on('file', (fieldname, file, filename, encoding, mimetype) => { | ||
46 | + console.log('File [%s]: filename=%j; encoding=%j; mimetype=%j', fieldname, filename, encoding, mimetype); | ||
47 | + const chunks = []; | ||
48 | + | ||
49 | + file.on('data', data => { | ||
50 | + chunks.push(data); | ||
51 | + }).on('end', () => { | ||
52 | + formData[fieldname] = { | ||
53 | + name:filename, | ||
54 | + data:Buffer.concat(chunks), | ||
55 | + mimetype:mimetype | ||
56 | + }; | ||
57 | + console.log("File [%s] finished.", filename); | ||
58 | + }); | ||
59 | + }); | ||
60 | + | ||
61 | + busboy.on('field', (fieldname, value) => { | ||
62 | + console.log("[" + fieldname + "] >> " + value); | ||
63 | + formData[fieldname] = value; | ||
64 | + }); | ||
65 | + | ||
66 | + busboy.on('error', error => { | ||
67 | + reject(error); | ||
68 | + }); | ||
69 | + | ||
70 | + busboy.on('finish', () => { | ||
71 | + resolve(formData); | ||
72 | + }); | ||
73 | + | ||
74 | + busboy.write(bodyBuffer, event.isBase64Encoded ? 'base64' : 'binary'); | ||
75 | + busboy.end(); | ||
76 | +}); | ||
77 | + | ||
78 | +const upload = ({data, mimetype}) => new Promise((resolve, reject) => { | ||
79 | + const bucket = 'gif-generator'; | ||
80 | + const path = '/gif'; | ||
81 | + const id = UUID().replace(/\-/g, ''); | ||
82 | + const fileFullName = path + '/' + id + '.gif'; | ||
83 | + const params = { | ||
84 | + Bucket: bucket, | ||
85 | + Key: fileFullName, | ||
86 | + Body: data, | ||
87 | + ContentType: mimetype | ||
88 | + }; | ||
89 | + | ||
90 | + s3.upload(params, (err, data) => { | ||
91 | + if(err){ | ||
92 | + console.log("upload err"); | ||
93 | + console.log(err); | ||
94 | + reject(err); | ||
95 | + }else{ | ||
96 | + console.log("upload success"); | ||
97 | + console.log(data); | ||
98 | + resolve(id); | ||
99 | + } | ||
100 | + }); | ||
101 | +}); | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or login to post a comment