12a3c6872f
* wip * Update packages/client/src/os.ts Co-authored-by: tamaina <tamaina@hotmail.co.jp> * メニューをComposition API化、switchアイテム追加 クライアントサイド画像圧縮の準備 * メニュー型定義を分離 (TypeScriptの型支援が効かないので) * disabled * make keepOriginal to follow setting value * ✌️ * fix * fix * ✌️ * WEBP * aaa * ✌️ * webp * lazy load browser-image-resizer * rename * rename 2 * Fix * clean up * add comment * clean up * jpeg, pngにもどす * fix * fix name * webpでなくする ただしサムネやプレビューはwebpのまま (テスト) * 動画サムネイルはjpegに * エラーハンドリング * ✌️ * v2.2.1-misskey-beta.2 * browser-image-resizer#v2.2.1-misskey.1 * ✌️ * fix alert * update browser-image-resizer to v2.2.1-misskey.2 * lockfile Co-authored-by: mei23 <m@m544.net> Co-authored-by: MeiMei <30769358+mei23@users.noreply.github.com>
66 lines
1.5 KiB
TypeScript
66 lines
1.5 KiB
TypeScript
import Koa from 'koa';
|
|
import summaly from 'summaly';
|
|
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';
|
|
|
|
const logger = new Logger('url-preview');
|
|
|
|
export const urlPreviewHandler = async (ctx: Koa.Context) => {
|
|
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;
|
|
}
|
|
|
|
const meta = await fetchMeta();
|
|
|
|
logger.info(meta.summalyProxy
|
|
? `(Proxy) Getting preview of ${url}@${lang} ...`
|
|
: `Getting preview of ${url}@${lang} ...`);
|
|
|
|
try {
|
|
const summary = meta.summalyProxy ? await getJson(`${meta.summalyProxy}?${query({
|
|
url: url,
|
|
lang: lang ?? 'ja-JP',
|
|
})}`) : await summaly.default(url, {
|
|
followRedirects: false,
|
|
lang: lang ?? 'ja-JP',
|
|
});
|
|
|
|
logger.succ(`Got preview of ${url}: ${summary.title}`);
|
|
|
|
summary.icon = wrap(summary.icon);
|
|
summary.thumbnail = wrap(summary.thumbnail);
|
|
|
|
// Cache 7days
|
|
ctx.set('Cache-Control', 'max-age=604800, immutable');
|
|
|
|
ctx.body = summary;
|
|
} catch (err) {
|
|
logger.warn(`Failed to get preview of ${url}: ${err}`);
|
|
ctx.status = 200;
|
|
ctx.set('Cache-Control', 'max-age=86400, immutable');
|
|
ctx.body = '{}';
|
|
}
|
|
};
|
|
|
|
function wrap(url?: string): string | null {
|
|
return url != null
|
|
? url.match(/^https?:\/\//)
|
|
? `${config.url}/proxy/preview.webp?${query({
|
|
url,
|
|
preview: '1',
|
|
})}`
|
|
: url
|
|
: null;
|
|
}
|