2018-04-12 21:06:18 +00:00
|
|
|
import * as Koa from 'koa';
|
2018-08-25 16:56:21 +00:00
|
|
|
import * as request from 'request-promise-native';
|
2016-12-28 22:49:51 +00:00
|
|
|
import summaly from 'summaly';
|
2018-11-22 23:13:17 +00:00
|
|
|
import fetchMeta from '../../misc/fetch-meta';
|
2016-12-28 22:49:51 +00:00
|
|
|
|
2019-01-22 12:42:05 +00:00
|
|
|
module.exports = async (ctx: Koa.BaseContext) => {
|
2018-11-22 23:13:17 +00:00
|
|
|
const meta = await fetchMeta();
|
|
|
|
|
2018-05-18 03:08:05 +00:00
|
|
|
try {
|
2018-11-22 23:13:17 +00:00
|
|
|
const summary = meta.summalyProxy ? await request.get({
|
|
|
|
url: meta.summalyProxy,
|
2018-08-25 16:56:21 +00:00
|
|
|
qs: {
|
|
|
|
url: ctx.query.url
|
|
|
|
},
|
|
|
|
json: true
|
|
|
|
}) : await summaly(ctx.query.url, {
|
2018-05-18 03:21:53 +00:00
|
|
|
followRedirects: false
|
|
|
|
});
|
2018-08-25 16:56:21 +00:00
|
|
|
|
2018-05-18 03:08:05 +00:00
|
|
|
summary.icon = wrap(summary.icon);
|
|
|
|
summary.thumbnail = wrap(summary.thumbnail);
|
2018-04-13 16:45:44 +00:00
|
|
|
|
2018-05-18 03:08:05 +00:00
|
|
|
// Cache 7days
|
|
|
|
ctx.set('Cache-Control', 'max-age=604800, immutable');
|
2018-04-13 16:45:44 +00:00
|
|
|
|
2018-05-18 03:08:05 +00:00
|
|
|
ctx.body = summary;
|
|
|
|
} catch (e) {
|
2018-08-11 09:04:59 +00:00
|
|
|
ctx.status = 200;
|
|
|
|
ctx.set('Cache-Control', 'max-age=86400, immutable');
|
|
|
|
ctx.body = '{}';
|
2018-05-18 03:08:05 +00:00
|
|
|
}
|
2016-12-28 22:49:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
function wrap(url: string): string {
|
2017-01-14 01:51:48 +00:00
|
|
|
return url != null
|
2018-05-09 16:08:33 +00:00
|
|
|
? url.startsWith('https://') || url.startsWith('data:')
|
2018-05-05 17:08:27 +00:00
|
|
|
? url
|
2018-05-09 11:14:34 +00:00
|
|
|
: `https://images.weserv.nl/?url=${encodeURIComponent(url.replace(/^http:\/\//, ''))}`
|
2017-01-14 01:51:48 +00:00
|
|
|
: null;
|
2016-12-28 22:49:51 +00:00
|
|
|
}
|