rudeshark.net/src/server/web/url-preview.ts

22 lines
514 B
TypeScript
Raw Normal View History

2018-04-12 23:06:18 +02:00
import * as Koa from 'koa';
2016-12-28 23:49:51 +01:00
import summaly from 'summaly';
2018-04-12 23:06:18 +02:00
module.exports = async (ctx: Koa.Context) => {
const summary = await summaly(ctx.query.url);
2016-12-28 23:49:51 +01:00
summary.icon = wrap(summary.icon);
summary.thumbnail = wrap(summary.thumbnail);
2018-04-13 18:45:44 +02:00
// Cache 7days
ctx.set('Cache-Control', 'max-age=604800, immutable');
2018-04-12 23:06:18 +02:00
ctx.body = summary;
2016-12-28 23:49:51 +01:00
};
function wrap(url: string): string {
2017-01-14 02:51:48 +01:00
return url != null
2018-05-05 19:08:27 +02:00
? url.startsWith('https://')
? url
: `https://images.weserv.nl/?url=${url.replace(/^http:\/\//, '')}`
2017-01-14 02:51:48 +01:00
: null;
2016-12-28 23:49:51 +01:00
}