From 43279c2e1a9b3ec29c5baddfde874cfae16c7b68 Mon Sep 17 00:00:00 2001 From: kuekhaoyang Date: Tue, 13 Jan 2026 15:37:38 +0800 Subject: [PATCH] Fix image not load --- app/api/douban/image/route.ts | 60 +++++++++++++++++++++++++++++++ app/api/douban/recommend/route.ts | 9 +++++ 2 files changed, 69 insertions(+) create mode 100644 app/api/douban/image/route.ts diff --git a/app/api/douban/image/route.ts b/app/api/douban/image/route.ts new file mode 100644 index 0000000..c91361b --- /dev/null +++ b/app/api/douban/image/route.ts @@ -0,0 +1,60 @@ +import { NextResponse } from 'next/server'; + +export const runtime = 'nodejs'; + +export async function GET(request: Request) { + const { searchParams } = new URL(request.url); + const imageUrl = searchParams.get('url'); + + if (!imageUrl) { + return NextResponse.json({ error: 'Missing image URL' }, { status: 400 }); + } + + try { + const imageResponse = await fetch(imageUrl, { + headers: { + 'User-Agent': + 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36', + Accept: 'image/jpeg,image/png,image/gif,*/*;q=0.8', + Referer: 'https://movie.douban.com/', + }, + }); + + if (!imageResponse.ok) { + return NextResponse.json( + { error: imageResponse.statusText }, + { status: imageResponse.status } + ); + } + + const contentType = imageResponse.headers.get('content-type'); + + if (!imageResponse.body) { + return NextResponse.json( + { error: 'Image response has no body' }, + { status: 500 } + ); + } + + // 创建响应头 + const headers = new Headers(); + if (contentType) { + headers.set('Content-Type', contentType); + } + + // 设置缓存头 + headers.set('Cache-Control', 'public, max-age=15720000, s-maxage=15720000'); + + // 直接返回图片流 + // @ts-ignore + return new Response(imageResponse.body, { + status: 200, + headers, + }); + } catch (error) { + return NextResponse.json( + { error: 'Error fetching image' }, + { status: 500 } + ); + } +} diff --git a/app/api/douban/recommend/route.ts b/app/api/douban/recommend/route.ts index 490cc3c..2cc8f54 100644 --- a/app/api/douban/recommend/route.ts +++ b/app/api/douban/recommend/route.ts @@ -25,6 +25,15 @@ export async function GET(request: Request) { } const data = await response.json(); + + // 转换图片链接使用代理 + if (data.subjects && Array.isArray(data.subjects)) { + data.subjects = data.subjects.map((item: any) => ({ + ...item, + cover: item.cover ? `/api/douban/image?url=${encodeURIComponent(item.cover)}` : item.cover, + })); + } + return NextResponse.json(data); } catch (error) { console.error('Douban API error:', error);