|
| 1 | +/*global exports*/ |
| 2 | +const path = require('path'), |
| 3 | + os = require('os'), |
| 4 | + ApiBuilder = require('claudia-api-builder'), |
| 5 | + fs = require('./fs-promise'), |
| 6 | + childProcess = require('./child-process-promise'), |
| 7 | + api = new ApiBuilder(); |
| 8 | + |
| 9 | +module.exports = api; |
| 10 | + |
| 11 | +//api.setBinaryMediaTypes(['image/*']); |
| 12 | + |
| 13 | +api.get('/img', () => { |
| 14 | + 'use strict'; |
| 15 | + return fs.readFilePromise(path.join(__dirname, 'img.png')); |
| 16 | +}, { success: { contentType: 'image/png', contentHandling: 'CONVERT_TO_BINARY'}}); |
| 17 | + |
| 18 | + |
| 19 | +api.post('/info', (request) => { |
| 20 | + 'use strict'; |
| 21 | + const tempFileName = path.join(os.tmpdir(), request.lambdaContext.awsRequestId); |
| 22 | + let result; |
| 23 | + return fs.writeFilePromise(tempFileName, request.body) |
| 24 | + .then(() => childProcess.spawn('/usr/bin/identify', [tempFileName])) |
| 25 | + .then(picInfo => result = picInfo.replace(/[^\s]*\s/, '')) |
| 26 | + .then(() => fs.unlinkPromise(tempFileName)) |
| 27 | + .then(() => result); |
| 28 | +}, { success: { contentType: 'text/plain' } }); |
| 29 | + |
| 30 | +api.post('/thumb', (request) => { |
| 31 | + 'use strict'; |
| 32 | + const tempFileName = path.join(os.tmpdir(), request.lambdaContext.awsRequestId), |
| 33 | + thumbFileName = tempFileName + '-thumb.png'; |
| 34 | + let result; |
| 35 | + return fs.writeFilePromise(tempFileName, request.body) |
| 36 | + .then(() => childProcess.spawn('/usr/bin/convert', ['-resize', '150x', tempFileName, thumbFileName])) |
| 37 | + .then(() => fs.readFilePromise(thumbFileName)) |
| 38 | + .then(fileContents => result = fileContents) |
| 39 | + .then(() => fs.unlinkPromise(tempFileName)) |
| 40 | + .then(() => fs.unlinkPromise(thumbFileName)) |
| 41 | + .then(() => result); |
| 42 | +}, { success: { contentType: 'image/png', contentHandling: 'CONVERT_TO_BINARY' } }); |
0 commit comments