-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
206 lines (168 loc) · 5.55 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
require('dotenv').config();
const express = require('express');
const crypto = require('crypto');
const mkdirp = require('mkdirp');
const morgan = require('morgan');
const sharp = require('sharp');
const path = require('path');
const url = require('url');
const fs = require('fs');
const findRemoveSync = require('find-remove');
const LargeObjectManager = require('pg-large-object').LargeObjectManager;
const pgp = require('pg-promise')();
const db = pgp(process.env.DATABASE_URL);
var charges = [];
var lastChargedAt = +new Date();
const chargeImage = function(token, length) {
charges.push([token, length]);
setTimeout(function() {
const now = +new Date();
if (now - lastChargedAt > 1000) {
lastChargedAt = now;
const n = charges.length;
// Sum lengths of identical tokens
const chargesAcc = charges.splice(0).reduce(function(acc, [k, v]) {
acc[k] = (acc[k] || 0) + v;
return acc;
}, {});
console.log('LOG Charging ' + n + ' request(s) for ' + Object.keys(chargesAcc).length + ' image(s)');
const sql = 'UPDATE users SET balance = balance - $2 FROM pictures WHERE token = $1 AND users.id = pictures.user_id';
const query = pgp.helpers.concat(Object.entries(chargesAcc).map(charge => ({
query: sql,
values: charge
})));
db.none(query).catch(function(err) {
console.error('Error updating user balance', err);
});
}
}, 1000);
};
const oidSQL = function(params) {
switch (params.model) {
case 'pictures':
return 'SELECT image AS oid FROM pictures WHERE token = ${id} AND image_filename = ${filename}';
case 'users':
return 'SELECT avatar AS oid FROM users WHERE username = ${id} AND avatar_filename = ${filename}';
}
};
const cacheDir = process.env.CACHE_DIR || 'tmp';
const cacheTime = process.env.CACHE_TIME || '86400';
// Remove old images from cache
setInterval(function() {
const removed = findRemoveSync(cacheDir, {
age: { seconds: cacheTime },
extensions: '.jpg'
});
const n = Object.keys(removed).length;
if (n > 0) {
console.log('LOG Removed ' + n + ' image(s) older than ' + cacheTime + ' seconds');
}
}, cacheTime * 1000);
// Get image from cache
const cacheFile = function(req, res, next) {
req.cache = path.join(cacheDir, req.path);
req.originalCache = path.join(cacheDir, ...['model', 'id', 'filename'].map(k => req.params[k]));
if (req.originalCache != req.cache) {
req.resizedCache = req.cache;
}
req.tempCache = path.join(cacheDir, crypto.randomBytes(24).toString('hex') + '.jpg');
fs.stat(req.cache, function(err, stats) {
if (stats) {
if (err) {
return next(err);
} else {
return serveFile(req, res); // Bypass next() functions
}
}
mkdirp(path.dirname(req.cache), function(err) {
return next(err);
});
});
};
const streamFile = function(req, res, next) {
db.tx(tx => {
return tx.one(oidSQL(req.params), req.params).then(function(image) {
const man = new LargeObjectManager({ pgPromise: tx });
const bufferSize = 16384;
return man.openAndReadableStreamAsync(image.oid, bufferSize).then(function([size, stream]) {
const temp = fs.createWriteStream(req.tempCache);
stream.pipe(temp);
return new Promise(function(resolve) {
stream.on('end', function() {
fs.rename(req.tempCache, req.originalCache, function(err) {
if (err) {
return next(err);
}
resolve();
});
});
});
});
});
}).then(next).catch(next);
};
const resizeFile = function(req, res, next) {
var geometry = req.params.geometry;
var crop = false;
if (geometry.slice(-1) == '!') {
geometry = geometry.slice(0, -1);
crop = true;
}
const dimensions = geometry.split('x');
const width = Number(dimensions[0]) || null;
const height = Number(dimensions[1]) || null;
var options = {
withoutEnlargement: true
};
if (!crop) {
options['fit'] = 'inside';
}
sharp(req.originalCache)
.resize(width, height, options)
.sharpen()
.jpeg({ quality: 90 })
.toFile(req.tempCache, function(err, info) {
if (err) {
return next(err);
}
fs.rename(req.tempCache, req.resizedCache, next);
});
};
const serveFile = function(req, res, next) {
fs.readFile(req.cache, function(err, data) {
if (err) {
return next(err);
}
if (req.params.model == 'pictures') {
chargeImage(req.params.id, data.length);
}
res.setHeader('Content-Type', 'image/jpeg');
res.setHeader('Content-Length', data.length);
res.send(data);
});
};
const app = express();
app.enable('trust proxy');
app.use(morgan('dev'));
// New routes:
// v1: /picture/image/3cfbb86a/x300/7919403e579249e288fcfae3926e1e04.jpg
// v2: /pictures/3cfbb86a/x300/7919403e579249e288fcfae3926e1e04.jpg
app.get('/:model/:attribute(image|avatar)/*', function(req, res) {
res.redirect('/' + req.params.model + 's/' + req.params[0]);
});
app.get('/:model/:id/:filename', cacheFile, streamFile, serveFile);
app.get('/:model/:id/:geometry/:filename', cacheFile, streamFile, resizeFile, serveFile);
app.use(express.static('public'));
const errorPage = fs.readFileSync(path.join(__dirname, 'public/index.html'));
app.use(function(req, res) {
res.writeHead(404, { 'Content-Type': 'text/html' });
res.write(errorPage);
res.end();
});
app.use(function(err, req, res, next) {
console.error(err);
res.writeHead(500, { 'Content-Type': 'text/html' });
res.write(errorPage);
res.end();
});
app.listen(process.env.PORT);