-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.js
73 lines (57 loc) · 1.68 KB
/
app.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
var express = require('express');
var _ = require("underscore");
var easyImage = require("easyimage"),
fs = require("fs"),
io = require("./modules/io");
var resize = require("./modules/resizer");
//Create express app
var app = express();
app.set('title',"NodeSizer");
app.get('/', function(req, res){
res.send('<p>Welcome to NodeSizer</p>');
});
//Converter
app.get('/convert', function(req, res){
// Using static methods:
var start = Date.now();
var validRequest = resize.queryHasValidParams(req.query);
if(!validRequest){
// Invalid request
res.send(400,"<h3>Invalid Request</h3><p>You require, 'source' and 'size' GET queries minimum</p><p>If you are check your parameters. 'size' must be greater than 0!");
return;
}
// Go get the image
resize.getOrginalImage(req.query, function(sucess){
if(!sucess){
res.send(404, "<h3>File not found - "+req.query.source+"</h3>");
return;
}else{
//res.setEncoding('binary');
res.set('Content-Type', 'image/jpeg');
resize.changeImage(req.query, function(err, path){
if(err){
}else{
var end = Date.now();
var elapsed = end - start; // time in milliseconds
console.log("Convert - elapsed time: " + elapsed + "ms");
res.set({
'Content-Type' : 'image/'+ resize.getImageExt(path),
'Content-Creation-Time' : elapsed
});
fs.createReadStream(path).pipe(res);
}
//res.send(orginalImage);
});
//res.end(new Buffer(orginalImage), 'binary');
}
});
});
//Clean the Image cache
io.removeImagesPath();
//Set up which port to listen too
var port = 8080;
if(process.env.NODE_PROD == "true"){
port = 80;
}
app.listen(port);
console.log("NodeSizer started on port " + port);