2018-04-12 16:06:18 -05:00
|
|
|
import * as Koa from 'koa';
|
2016-12-28 16:49:51 -06:00
|
|
|
import summaly from 'summaly';
|
|
|
|
|
2018-04-12 16:06:18 -05:00
|
|
|
module.exports = async (ctx: Koa.Context) => {
|
2018-05-17 22:08:05 -05:00
|
|
|
try {
|
2018-05-17 22:21:53 -05:00
|
|
|
const summary = await summaly(ctx.query.url, {
|
|
|
|
followRedirects: false
|
|
|
|
});
|
2018-05-17 22:08:05 -05:00
|
|
|
summary.icon = wrap(summary.icon);
|
|
|
|
summary.thumbnail = wrap(summary.thumbnail);
|
2018-04-13 11:45:44 -05:00
|
|
|
|
2018-05-17 22:08:05 -05:00
|
|
|
// Cache 7days
|
|
|
|
ctx.set('Cache-Control', 'max-age=604800, immutable');
|
2018-04-13 11:45:44 -05:00
|
|
|
|
2018-05-17 22:08:05 -05:00
|
|
|
ctx.body = summary;
|
|
|
|
} catch (e) {
|
|
|
|
ctx.status = 500;
|
|
|
|
}
|
2016-12-28 16:49:51 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
function wrap(url: string): string {
|
2017-01-13 19:51:48 -06:00
|
|
|
return url != null
|
2018-05-09 11:08:33 -05:00
|
|
|
? url.startsWith('https://') || url.startsWith('data:')
|
2018-05-05 12:08:27 -05:00
|
|
|
? url
|
2018-05-09 06:14:34 -05:00
|
|
|
: `https://images.weserv.nl/?url=${encodeURIComponent(url.replace(/^http:\/\//, ''))}`
|
2017-01-13 19:51:48 -06:00
|
|
|
: null;
|
2016-12-28 16:49:51 -06:00
|
|
|
}
|