hippofish/packages/backend/src/server/web/url-preview.ts

89 lines
1.9 KiB
TypeScript
Raw Normal View History

2023-01-13 05:40:33 +01:00
import type 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";
2019-02-28 04:00:57 +01:00
2023-01-13 05:40:33 +01:00
const logger = new Logger("url-preview");
2016-12-28 23:49:51 +01:00
export const urlPreviewHandler = async (ctx: Koa.Context) => {
2022-02-03 13:38:57 +01:00
const url = ctx.query.url;
2023-01-13 05:40:33 +01:00
if (typeof url !== "string") {
2022-02-03 13:38:57 +01:00
ctx.status = 400;
return;
}
const lang = ctx.query.lang;
if (Array.isArray(lang)) {
ctx.status = 400;
return;
}
2018-11-23 00:13:17 +01:00
const meta = await fetchMeta();
2023-01-13 05:40:33 +01:00
logger.info(
meta.summalyProxy
? `(Proxy) Getting preview of ${url}@${lang} ...`
: `Getting preview of ${url}@${lang} ...`,
);
2019-02-28 04:00:57 +01:00
2018-05-18 05:08:05 +02:00
try {
2023-01-13 05:40:33 +01:00
const summary = meta.summalyProxy
? await getJson(
`${meta.summalyProxy}?${query({
url: url,
lang: lang ?? "en-US",
})}`,
)
: await summaly.default(url, {
followRedirects: false,
lang: lang ?? "en-US",
});
2018-08-25 18:56:21 +02:00
2022-02-03 13:38:57 +01:00
logger.succ(`Got preview of ${url}: ${summary.title}`);
2019-02-28 04:00:57 +01:00
2023-02-11 00:41:19 +01:00
if (
summary.url &&
!(summary.url.startsWith("http://") || summary.url.startsWith("https://"))
) {
throw new Error("unsupported schema included");
2023-02-10 20:14:33 +01:00
}
2023-02-11 00:41:19 +01:00
if (
summary.player?.url &&
!(
summary.player.url.startsWith("http://") ||
summary.player.url.startsWith("https://")
)
) {
throw new Error("unsupported schema included");
2023-02-10 20:14:33 +01:00
}
2018-05-18 05:08:05 +02:00
summary.icon = wrap(summary.icon);
summary.thumbnail = wrap(summary.thumbnail);
2018-04-13 18:45:44 +02:00
2018-05-18 05:08:05 +02:00
// Cache 7days
2023-01-13 05:40:33 +01:00
ctx.set("Cache-Control", "max-age=604800, immutable");
2018-04-13 18:45:44 +02:00
2018-05-18 05:08:05 +02:00
ctx.body = summary;
2022-02-03 13:38:57 +01:00
} catch (err) {
logger.warn(`Failed to get preview of ${url}: ${err}`);
ctx.status = 200;
2023-01-13 05:40:33 +01:00
ctx.set("Cache-Control", "max-age=86400, immutable");
ctx.body = "{}";
2018-05-18 05:08:05 +02:00
}
2016-12-28 23:49:51 +01:00
};
function wrap(url?: string): string | null {
2017-01-14 02:51:48 +01:00
return url != null
2019-02-10 11:19:26 +01:00
? url.match(/^https?:\/\//)
? `${config.url}/proxy/preview.webp?${query({
2023-01-13 05:40:33 +01:00
url,
preview: "1",
})}`
2019-02-10 11:19:26 +01:00
: url
2017-01-14 02:51:48 +01:00
: null;
2016-12-28 23:49:51 +01:00
}