2022-02-27 11:07:39 +09:00
|
|
|
import Koa from 'koa';
|
2016-12-29 07:49:51 +09:00
|
|
|
import summaly from 'summaly';
|
2022-02-27 11:07:39 +09:00
|
|
|
import { fetchMeta } from '@/misc/fetch-meta.js';
|
|
|
|
import Logger from '@/services/logger.js';
|
|
|
|
import config from '@/config/index.js';
|
|
|
|
import { query } from '@/prelude/url.js';
|
|
|
|
import { getJson } from '@/misc/fetch.js';
|
2019-02-28 12:00:57 +09:00
|
|
|
|
|
|
|
const logger = new Logger('url-preview');
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2022-02-27 11:07:39 +09:00
|
|
|
export const urlPreviewHandler = async (ctx: Koa.Context) => {
|
2022-02-03 21:38:57 +09:00
|
|
|
const url = ctx.query.url;
|
|
|
|
if (typeof url !== 'string') {
|
|
|
|
ctx.status = 400;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const lang = ctx.query.lang;
|
|
|
|
if (Array.isArray(lang)) {
|
|
|
|
ctx.status = 400;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-23 08:13:17 +09:00
|
|
|
const meta = await fetchMeta();
|
|
|
|
|
2019-03-01 14:40:29 +09:00
|
|
|
logger.info(meta.summalyProxy
|
2022-02-03 21:38:57 +09:00
|
|
|
? `(Proxy) Getting preview of ${url}@${lang} ...`
|
|
|
|
: `Getting preview of ${url}@${lang} ...`);
|
2019-02-28 12:00:57 +09:00
|
|
|
|
2018-05-18 12:08:05 +09:00
|
|
|
try {
|
2020-04-09 23:42:23 +09:00
|
|
|
const summary = meta.summalyProxy ? await getJson(`${meta.summalyProxy}?${query({
|
2022-02-03 21:38:57 +09:00
|
|
|
url: url,
|
|
|
|
lang: lang ?? 'ja-JP',
|
2022-03-01 01:33:25 +09:00
|
|
|
})}`) : await summaly.default(url, {
|
2019-06-21 15:41:02 +09:00
|
|
|
followRedirects: false,
|
2022-02-03 21:38:57 +09:00
|
|
|
lang: lang ?? 'ja-JP',
|
2018-05-18 12:21:53 +09:00
|
|
|
});
|
2018-08-26 01:56:21 +09:00
|
|
|
|
2022-02-03 21:38:57 +09:00
|
|
|
logger.succ(`Got preview of ${url}: ${summary.title}`);
|
2019-02-28 12:00:57 +09:00
|
|
|
|
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;
|
2022-02-03 21:38:57 +09:00
|
|
|
} catch (err) {
|
|
|
|
logger.warn(`Failed to get preview of ${url}: ${err}`);
|
2018-08-11 18:04:59 +09:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2019-04-13 01:43:22 +09:00
|
|
|
function wrap(url?: string): string | null {
|
2017-01-14 10:51:48 +09:00
|
|
|
return url != null
|
2019-02-10 19:19:26 +09:00
|
|
|
? url.match(/^https?:\/\//)
|
2022-04-28 11:14:03 +09:00
|
|
|
? `${config.url}/proxy/preview.webp?${query({
|
2019-03-11 01:03:09 +09:00
|
|
|
url,
|
2021-12-09 23:58:30 +09:00
|
|
|
preview: '1',
|
2019-03-11 01:03:09 +09:00
|
|
|
})}`
|
2019-02-10 19:19:26 +09:00
|
|
|
: url
|
2017-01-14 10:51:48 +09:00
|
|
|
: null;
|
2016-12-29 07:49:51 +09:00
|
|
|
}
|