-
Notifications
You must be signed in to change notification settings - Fork 104
/
test.js
84 lines (69 loc) · 2.05 KB
/
test.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* This file is the working implementation of image-resizer used for
* development purposes. It is a very close example of what is generated by
* the cli script when provisioning a new instance. `image-resizer new`
*/
'use strict';
var express = require('express'),
app = express(),
ir = require('./index'),
env = ir.env,
Img = ir.img,
streams = ir.streams,
chalk = require('chalk'),
exec = require('child_process').exec;
// check to see if vips is installed
exec ('vips --version', function (err, stdout, stderr) {
if (err || stderr) {
console.error(
chalk.red('\nMissing dependency:'),
chalk.red.bold('libvips')
);
console.log(
chalk.cyan(' to install vips on your system run:'),
chalk.bold('./node_modules/sharp/preinstall.sh\n')
);
}
});
app.directory = __dirname;
ir.expressConfig(app);
app.get('/favicon.ico', function (request, response) {
response.sendStatus(404);
});
/**
* Return the modifiers map as a documentation endpoint
*/
app.get('/modifiers.json', function(request, response){
response.json(ir.modifiers);
});
/**
* Some helper endpoints when in development
*/
if (env.development){
// Show a test page of the image options
app.get('/test-page', function(request, response){
response.render('index.html');
});
// Show the environment variables and their current values
app.get('/env', function(request, response){
response.status(200).json(env);
});
}
/**
* Return an image modified to the requested parameters
* - request format:
* /:modifers/path/to/image.format:metadata
* eg: https://doapv6pcsx1wa.cloudfront.net/s50/sample/test.png
*/
app.get('/*?', function(request, response){
var image = new Img(request);
var stream = image.getFile().pipe(new streams.identify());
stream = stream.pipe(new streams.resize());
stream = stream.pipe(new streams.filter());
stream = stream.pipe(new streams.optimize());
stream.pipe(streams.response(request, response));
});
/**
Start the app on the listed port
*/
app.listen(app.get('port'));