scale thumbnail support (#1)

master
Lauren Liberda 2021-05-23 14:08:24 +02:00
parent 5c649d8a26
commit fe4e737082
1 changed files with 13 additions and 2 deletions

View File

@ -116,7 +116,15 @@ media.get(
media.get('/thumbnail/:serverName/:mediaId', async (ctx) => {
const { serverName, mediaId } = ctx.params;
if (typeof ctx.query.width !== 'string' || typeof ctx.query.height !== 'string') return;
if (
typeof ctx.query.width !== 'string' ||
typeof ctx.query.height !== 'string' ||
typeof ctx.query.method !== 'string'
) {
return;
}
const { method } = ctx.query;
assert(['crop', 'scale'].includes(method));
const width = parseInt(ctx.query.width, 10);
const height = parseInt(ctx.query.height, 10);
@ -144,7 +152,10 @@ media.get('/thumbnail/:serverName/:mediaId', async (ctx) => {
);
}
if (file && file.statusCode === 200) {
const img = await sharp(file.body).resize(width, height).toBuffer({ resolveWithObject: true });
const resizer = sharp(file.body).resize(width, height, {
fit: method === 'crop' ? 'cover' : 'inside',
});
const img = await resizer.toBuffer({ resolveWithObject: true });
ctx.res.statusCode = 200;
ctx.res.setHeader('content-type', `image/${img.info.format}`);
ctx.res.write(img.data);