-
Notifications
You must be signed in to change notification settings - Fork 235
/
Copy pathmain.js
42 lines (36 loc) · 1.53 KB
/
main.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
/*global exports*/
const path = require('path'),
os = require('os'),
ApiBuilder = require('claudia-api-builder'),
fs = require('./fs-promise'),
childProcess = require('./child-process-promise'),
api = new ApiBuilder();
module.exports = api;
//api.setBinaryMediaTypes(['image/*']);
api.get('/img', () => {
'use strict';
return fs.readFilePromise(path.join(__dirname, 'img.png'));
}, { success: { contentType: 'image/png', contentHandling: 'CONVERT_TO_BINARY'}});
api.post('/info', (request) => {
'use strict';
const tempFileName = path.join(os.tmpdir(), request.lambdaContext.awsRequestId);
let result;
return fs.writeFilePromise(tempFileName, request.body)
.then(() => childProcess.spawn('/usr/bin/identify', [tempFileName]))
.then(picInfo => result = picInfo.replace(/[^\s]*\s/, ''))
.then(() => fs.unlinkPromise(tempFileName))
.then(() => result);
}, { success: { contentType: 'text/plain' } });
api.post('/thumb', (request) => {
'use strict';
const tempFileName = path.join(os.tmpdir(), request.lambdaContext.awsRequestId),
thumbFileName = tempFileName + '-thumb.png';
let result;
return fs.writeFilePromise(tempFileName, request.body)
.then(() => childProcess.spawn('/usr/bin/convert', ['-resize', '150x', tempFileName, thumbFileName]))
.then(() => fs.readFilePromise(thumbFileName))
.then(fileContents => result = fileContents)
.then(() => fs.unlinkPromise(tempFileName))
.then(() => fs.unlinkPromise(thumbFileName))
.then(() => result);
}, { success: { contentType: 'image/png', contentHandling: 'CONVERT_TO_BINARY' } });