1
0
mirror of https://github.com/hotomoe/hotomoe synced 2024-12-29 14:08:07 +09:00
hotomoe/src/server/web/url-preview.ts

30 lines
720 B
TypeScript
Raw Normal View History

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