forked from DMarby/picsum-photos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
163 lines (128 loc) · 4.69 KB
/
server.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
module.exports = function (callback) {
var fs = require('fs')
var path = require('path')
var express = require('express')
var cors = require('cors')
var compress = require('compression')
var sharp = require('sharp')
var config = require('./config')()
var packageinfo = require('./package.json')
var imageProcessor = require('./imageProcessor')(sharp, path, config, fs)
var app = express()
sharp.cache(0)
try {
var images = require(config.image_store_path)
} catch (e) {
var images = []
}
process.addListener('uncaughtException', function (err) {
console.log('Uncaught exception: ')
console.trace(err)
})
app.use(compress());
app.use(cors());
app.use(express.static(path.join(__dirname, 'public')));
app.get('/info', function (req, res, next) {
res.jsonp({ name: packageinfo.name, version: packageinfo.version, author: packageinfo.author })
})
app.get('/list', function (req, res, next) {
var newImages = []
for (var i in images) {
var item = images[i]
var image = {
format: item.format,
width: item.width,
height: item.height,
filename: path.basename(item.filename),
id: item.id,
author: item.author,
author_url: item.author_url,
post_url: item.post_url
}
newImages.push(image)
}
res.jsonp(newImages)
})
app.get('/:size', function (req, res, next) {
serveImage(req, res, true, false)
})
app.get('/g/:size', function (req, res, next) {
serveImage(req, res, true, true)
})
app.get('/:width/:height', function (req, res, next) {
serveImage(req, res, false, false)
})
app.get('/g/:width/:height', function (req, res, next) {
serveImage(req, res, false, true)
})
app.all('*', function (req, res, next) {
res.status(404)
res.send({ error: 'Resource not found' })
})
var serveImage = function(req, res, square, gray) {
checkParameters(req.params, req.query, square, function (error, code, message) {
if (error) {
return displayError(res, code, message)
}
imageProcessor.getWidthAndHeight(req.params, square, function (width, height) {
var filePath
var blur = false
if (req.query.image) {
var matchingImage = findMatchingImage(req.query.image)
if (matchingImage) {
filePath = matchingImage.filename
if (parseInt(width) == 0) {
width = matchingImage.width
}
if (parseInt(height) == 0) {
height = matchingImage.height
}
} else {
return displayError(res, 404, 'Invalid image id')
}
} else {
filePath = images[Math.floor(Math.random() * images.length)].filename
}
imageProcessor.getProcessedImage(parseInt(width), parseInt(height), req.query.gravity, gray, !(!req.query.blur && req.query.blur !== ''), filePath, (!req.query.image && !req.query.random && req.query.random !== ''), function (error, imagePath) {
if (error) {
console.log('filePath: ' + filePath)
console.log('imagePath: ' + imagePath)
console.log('error: ' + error)
return displayError(res, 500, 'Something went wrong')
}
res.sendFile(imagePath)
process.send(imagePath)
})
})
})
}
var checkParameters = function (params, queryparams, square, callback) {
if ((square && !params.size) || (square && isNaN(parseInt(params.size))) || (!square && !params.width) || (!square && !params.height) || (!square && isNaN(parseInt(params.width))) || (!square && isNaN(parseInt(params.height))) || (queryparams.gravity && sharp.gravity[queryparams.gravity] != 0 && !sharp.gravity[queryparams.gravity])) {
return callback(true, 400, 'Invalid arguments')
}
if (params.size > config.max_width || params.size > config.max_height || params.height > config.max_height || params.width > config.max_width) {
if (queryparams.image) {
var matchingImage = findMatchingImage(queryparams.image)
if (matchingImage && params.height == matchingImage.height && params.width == matchingImage.width) {
return callback(false)
}
}
return callback(true, 413, 'Specified dimensions too large')
}
callback(false)
}
var findMatchingImage = function (id) {
var matchingImages = images.filter(function (image) {
return image.id == id
})
if (!matchingImages.length) {
return false
}
return matchingImages[0]
}
var displayError = function (res, code, message) {
res.status(code)
res.send({ error: message })
}
callback(app)
}