-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
94 lines (82 loc) · 3.02 KB
/
index.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
const asyncHandler = require('express-async-handler')
const express = require('express')
const createError = require('http-errors')
const got = require('got')
const sharp = require('sharp')
const { query, validationResult } = require('express-validator');
const app = express()
const sharp_resize_modes = {
'bestfit' : 'inside', // Return image not exact size, but maintain aspect ratio
'stretch' : 'fill', // Stretch to fit. Does not maintain aspect ratio
'trim' : 'cover', // Trim off edges to fit exactly. Maintains aspect ratio.
'pad' : 'contain' // Pads one edge so that the image fits exactly and aspect ratio is maintained.
}
async function loadImageFromURL(source_url){
response = await got(source_url, {timeout: 5000, retry:0});
const image = sharp(response.rawBody).withMetadata();
image.rotate();
return image;
}
async function resizeImage(image, w, h, mode){
const sharp_resize_mode = sharp_resize_modes[mode];
return image.resize({
width: w,
height: h,
fit: sharp_resize_mode,
withoutEnlargement : true,
//background
})
}
async function renderBuffer(image, format){
if (format=='png') {
return [await image.png().toBuffer(), 'image/png'];
} else {
return [await image.jpeg().toBuffer(), 'image/jpeg'];
}
}
app.get('/tmb', [
query('url').isURL(),
query('w').isInt().toInt(),
query('h').isInt().toInt(),
query('mode').isIn([null,'bestfit','stretch','trim','pad']),
query('format').isIn([null, 'png', 'jpeg'])
], asyncHandler(async(req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
throw createError(400, 'Parameters incorrect', { errors: errors.array() })
}
const source_url = req.query.url
const w = req.query.w
const h = req.query.h
const resize_mode = req.query.mode || 'bestfit'
const format = req.query.format || 'jpeg'
const source_image = await loadImageFromURL(source_url)
const resized_image = await resizeImage(source_image, w, h, resize_mode)
const [output_buffer, mime_type] = await renderBuffer(resized_image, format)
res .set('Content-Type', mime_type)
.set('Cache-control', 'public, max-age=3600')
.send(output_buffer);
}));
// Error handler to send a 1x1 transparent png back as to not break img tags.
// The error message is included in a response HTTP header 'X-Image-Error'
// This error image has a cache duration of 60s
app.use(function (err, req, res, next) {
console.error(err)
const canvas = sharp({
create: {
width: 1,
height: 1,
channels: 4,
background: '#ffffff00'
}
});
canvas.png().toBuffer().then((buffer) => {
if (!err.statusCode) err.statusCode = 500;
res.status(err.statusCode)
.set('Content-Type', 'image/png')
.set('Cache-control', 'public, max-age=60')
.set('X-Error-Message', err.message)
.send(buffer)
})
})
exports.app = app;