This repository has been archived by the owner on May 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfunction.js
65 lines (56 loc) · 2.07 KB
/
function.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
61
62
63
64
65
const { exec } = require('child_process');
const { readFile, writeFile } = require('fs');
const { promisify } = require('util');
const request = require('request-promise-native');
const asyncExec = promisify(exec);
const asyncReadFile = promisify(readFile);
const asyncWriteFile = promisify(writeFile);
const randomString = () => Math.random().toString(36).substring(2, 15);
exports.handler = async (body, context) => {
// Get an image URL from the request body or query
// string (useful for invoking directly from a browser)
const uri = body.url || context.request.query.url;
if (uri === undefined || uri === '') {
// This will be caught by Binaris and returned as
// a HTTP 500 status code
throw new Error('No URL');
}
// Get a target size for the image. Image will be
// resized to {size} by {size} pixels. If neither
// request body nor query string contains a size
// value then default to 32
const size = parseInt(body.sz, 10) ||
parseInt(context.request.query.sz, 10) ||
32;
const srcFile = `/tmp/${randomString()}`;
const dstFile = `/tmp/${randomString()}.png`;
// Load the image from the specified url and save
// to file in /tmp
console.log(`Loading: ${uri}`);
const srcImage = await request({ uri, encoding: null });
console.log(`Image has ${srcImage.length} bytes`);
await asyncWriteFile(srcFile, srcImage);
// Use ImageMagick to resize and convert to PNG.
// Always returning a PNG simplifies the response
// encoding code below
try {
console.log(`Resiging to ${size}x${size} pixels`);
await asyncExec(`convert ${srcFile} -resize ${size}x${size} ${dstFile}`);
} catch (e) {
console.error('Error resizing image:');
console.error(e.stderr.toString());
console.error(e.stdout.toString());
throw e;
}
// Read the resized image file
const dstImage = await asyncReadFile(dstFile);
// Return an HTTP response with PNG encoded binary
// image
return new context.HTTPResponse({
statusCode: 200,
headers: {
'Content-Type': 'image/png',
},
body: dstImage,
});
};