Merge branch 'feat/proxy_media_fallback' into 'develop'

feat: proxy media fallback and url preview fallback

Co-authored-by: 老周部落 <laozhoubuluo@gmail.com>

See merge request firefish/firefish!10693
This commit is contained in:
naskya 2024-03-24 17:37:59 +00:00
commit a7d6e8eb1a
2 changed files with 30 additions and 3 deletions

View file

@ -15,6 +15,23 @@ import { isMimeImage } from "@/misc/is-mime-image.js";
import { inspect } from "node:util"; import { inspect } from "node:util";
export async function proxyMedia(ctx: Koa.Context) { export async function proxyMedia(ctx: Koa.Context) {
try {
proxyMediaInternal(ctx);
} catch (e) {
if (ctx.query.fallback) {
ctx.redirect("/static-assets/badges/error.webp");
return;
}
throw e;
}
if (ctx.status !== 200 && ctx.query.fallback) {
ctx.redirect("/static-assets/badges/error.webp");
return;
}
return;
}
async function proxyMediaInternal(ctx: Koa.Context) {
let url = "url" in ctx.query ? ctx.query.url : `https://${ctx.params.url}`; let url = "url" in ctx.query ? ctx.query.url : `https://${ctx.params.url}`;
if (typeof url !== "string") { if (typeof url !== "string") {
@ -24,9 +41,10 @@ export async function proxyMedia(ctx: Koa.Context) {
url = url.replace("//", "/"); url = url.replace("//", "/");
const { hostname } = new URL(url); let hostname;
let resolvedIps; let resolvedIps;
try { try {
hostname = new URL(url);
resolvedIps = await promises.resolve(hostname); resolvedIps = await promises.resolve(hostname);
} catch (error) { } catch (error) {
ctx.status = 400; ctx.status = 400;

View file

@ -17,7 +17,11 @@
}" }"
> >
<div v-if="thumbnail" class="thumbnail"> <div v-if="thumbnail" class="thumbnail">
<img :src="thumbnail" loading="lazy" /> <img
:src="thumbnail"
onerror="this.src='/static-assets/badges/error.webp'"
loading="lazy"
/>
<button <button
v-if="tweetId" v-if="tweetId"
v-tooltip=" v-tooltip="
@ -55,7 +59,12 @@
<p :title="description"> <p :title="description">
<span> <span>
<span :title="sitename || undefined"> <span :title="sitename || undefined">
<img v-if="icon" class="icon" :src="icon" /> <img
v-if="icon"
class="icon"
:src="icon"
onerror="this.src='/static-assets/badges/error.webp'"
/>
{{ sitename }} {{ sitename }}
</span> </span>
{{ description }} {{ description }}