-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
207 lines (191 loc) · 5.77 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
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
207
const express = require('express')
const morgan = require("morgan");
const createError = require('http-errors');
const mongoose = require('mongoose');
const _ = require('lodash');
const multer = require('multer');
const { body, query, param, validationResult, check, oneOf } = require('express-validator');
const config = require('./config/config');
const request = require('request');
const fs = require('fs');
const sharp = require('sharp');
const { nanoid } = require('nanoid');
const File = require('./models/file');
const urlParse = require('url-parse');
const path = require('path');
const agenda = require('./helper/agenda');
agenda.startJob();
// const storage = multer.memoryStorage();
// const upload = multer({
// storage: storage,
// fileFilter: function (req, file, cb) {
// if (file.mimetype !== 'image/png' && file.mimetype !== 'image/jpg' && file.mimetype !== 'image/jpeg') {
// cb(null, false);
// }
// else {
// cb(null, true);
// }
// }
// });
const app = express()
const port = normalizePort(process.env.PORT || "3000");
// mongodb
const options = {
useNewUrlParser: true,
useUnifiedTopology: true,
autoIndex: true,
};
mongoose.connect(
'mongodb://' +
config.dbUser +
':' +
config.dbPassword +
'@' + config.dbUrl + '/' +
config.db,
options
);
app.set('trust proxy', 1);
app.use(morgan('dev'));
app.use(express.json({ limit: '5mb' }));
app.use(express.urlencoded({ limit: '5mb', extended: false, parameterLimit: 10000 }));
app.use('/public', express.static('public'));
app.get('/', (req, res) => {
const accept = req.headers.accept;
let supportWebp = false;
if (accept && accept.indexOf('image/webp')) {
supportWebp = true;
}
console.log(supportWebp);
res.send('Hello World!')
})
app.get('/api',
query('url').notEmpty().isURL().trim(),
oneOf([
check('width').notEmpty().isInt({ min: 20, max: 3000 }).toInt(),
check('height').notEmpty().isInt({ min: 30, max: 6000 }).toInt(),
]),
query('format').default('webp').trim().isIn(['webp', 'jpg', 'png']),
async (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
const rootPath = path.join(__dirname, 'public');
let { url, width, height, format } = req.query;
const urlObject = new urlParse(url);
let limit = false;
const whiteLists = config.whiteLists;
if(whiteLists && whiteLists.length > 0) {
limit = true;
}
if(limit) {
if(whiteLists.indexOf(urlObject.host)==-1) {
return res.json({success: 0, message: 'only allow given host!'});
}
}
const resizeObj = {};
if (height && height > 6000) {
height = 6000;
}
if (width && width > 3000) {
width = 3000;
}
if (height) {
resizeObj.height = height;
}
if (width) {
resizeObj.width = width;
}
const accept = req.headers.accept;
let supportWebp = false;
if (accept && accept.indexOf('image/webp') > -1) {
supportWebp = true;
}
if (format == 'webp' && !supportWebp) {
format = 'jpg';
}
const imageUrl = urlObject.protocol + '//' + urlObject.host + urlObject.pathname;
let key = 'f-' + format;
if (width) {
key += 'w-' + width;
}
if (height) {
key += 'h-' + height;
}
const file = await File.findOne({ key, url: imageUrl });
res.set('Cache-control', 'public, max-age=3000');
if (file) {
File.updateOne({ _id: file._id }, { $inc: { views: 1 }, lastSeen: Date.now() }, function(err, res) {
if(err) {
console.log(err);
}
});
res.header('Content-Type', 'image/' + format);
res.header('Content-Disposition', 'inline; filename=index.' + format);
return res.sendFile(rootPath + '/tmp/' + file.nid);
}
request
.get(url)
.on('error', function (err) {
res.json({ success: 0, message: err.message });
})
.on('response', function (response) {
// 'image/png'
const type = response.headers['content-type'];
if (type != 'image/png' && type != 'image/jpg' && type != 'image/jpeg' && type != 'image/webp') {
return res.json({ success: 0, message: 'wrong image!' });
}
const nid = nanoid();
const desPath = './public/tmp/' + nid;
const writeStream = fs.createWriteStream(desPath);
response.pipe(writeStream);
writeStream.on('finish', async function () {
const nid = nanoid();
const newDesPath = './public/tmp/' + nid;
var info = await sharp(desPath)
.resize(resizeObj)
.toFormat(format)
.toFile(newDesPath);
const readStream = fs.createReadStream(newDesPath);
readStream.pipe(res);
File.create({ nid, key, url: imageUrl, width: info.width, height: info.height, size: info.size }, function (err) {
if (err) {
console.log(err);
}
});
sharp.cache(false);
fs.unlink(desPath, function (err) {
if (err) {
console.log(err);
}
});
});
})
});
app.use(function (req, res, next) {
next(createError(404));
});
// error handler
app.use(function (err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.json({ success: 0 });
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
function normalizePort(val) {
var port = parseInt(val, 10)
if (isNaN(port)) {
// named pipe
return val
}
if (port >= 0) {
// port number
return port
}
return false
}