94 lines
3.3 KiB
TypeScript
94 lines
3.3 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { prisma } from "@/lib/prisma";
|
|
import { fetchLogo } from "@/lib/logo-fetcher";
|
|
import { downloadLogo } from "@/lib/logo-storage";
|
|
import { requireAdmin } from "@/lib/admin-auth";
|
|
|
|
export async function PUT(
|
|
request: Request,
|
|
{ params }: { params: { id: string } }
|
|
) {
|
|
const authError = await requireAdmin();
|
|
if (authError) return authError;
|
|
|
|
try {
|
|
const id = parseInt(params.id);
|
|
const body = await request.json();
|
|
|
|
const current = await prisma.pendingTool.findUnique({ where: { id } });
|
|
if (!current) {
|
|
return NextResponse.json({ error: "工具不存在" }, { status: 404 });
|
|
}
|
|
|
|
const newWebsiteUrl = body.websiteUrl !== undefined ? body.websiteUrl : current.websiteUrl;
|
|
let logoUrl = body.logoUrl !== undefined ? body.logoUrl : current.logoUrl;
|
|
|
|
const websiteChanged = body.websiteUrl !== undefined && body.websiteUrl !== current.websiteUrl;
|
|
const logoEmpty = !logoUrl;
|
|
|
|
if (newWebsiteUrl && (websiteChanged || logoEmpty)) {
|
|
let remoteLogoUrl = "";
|
|
try {
|
|
const htmlRes = await fetch(newWebsiteUrl, {
|
|
signal: AbortSignal.timeout(8000),
|
|
headers: {
|
|
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
|
|
Accept: "text/html",
|
|
},
|
|
});
|
|
if (htmlRes.ok) {
|
|
const html = await htmlRes.text();
|
|
remoteLogoUrl = await fetchLogo(newWebsiteUrl, html);
|
|
}
|
|
} catch { /* */ }
|
|
|
|
if (!remoteLogoUrl) {
|
|
remoteLogoUrl = await fetchLogo(newWebsiteUrl);
|
|
}
|
|
|
|
if (remoteLogoUrl) {
|
|
logoUrl = await downloadLogo(remoteLogoUrl, (body.name || current.name).replace(/[^a-z0-9]/gi, "_").substring(0, 20));
|
|
}
|
|
} else if (logoUrl && !logoUrl.startsWith("/logos/") && !logoUrl.startsWith("data:")) {
|
|
logoUrl = await downloadLogo(logoUrl, (body.name || current.name).replace(/[^a-z0-9]/gi, "_").substring(0, 20));
|
|
}
|
|
|
|
const updated = await prisma.pendingTool.update({
|
|
where: { id },
|
|
data: {
|
|
...(body.name !== undefined && { name: body.name }),
|
|
...(body.description !== undefined && { description: body.description }),
|
|
...(body.websiteUrl !== undefined && { websiteUrl: body.websiteUrl }),
|
|
logoUrl: logoUrl || null,
|
|
...(body.categoryId !== undefined && { categoryId: body.categoryId }),
|
|
...(body.pricingModel !== undefined && { pricingModel: body.pricingModel }),
|
|
...(body.pricingDetail !== undefined && { pricingDetail: body.pricingDetail }),
|
|
...(body.features !== undefined && { features: body.features }),
|
|
...(body.tags !== undefined && { tags: body.tags }),
|
|
},
|
|
});
|
|
|
|
return NextResponse.json({ tool: updated });
|
|
} catch (error: unknown) {
|
|
const err = error as { message?: string };
|
|
return NextResponse.json({ error: err.message }, { status: 500 });
|
|
}
|
|
}
|
|
|
|
export async function DELETE(
|
|
_request: Request,
|
|
{ params }: { params: { id: string } }
|
|
) {
|
|
const authError = await requireAdmin();
|
|
if (authError) return authError;
|
|
|
|
try {
|
|
const id = parseInt(params.id);
|
|
await prisma.pendingTool.delete({ where: { id } });
|
|
return NextResponse.json({ success: true });
|
|
} catch (error: unknown) {
|
|
const err = error as { message?: string };
|
|
return NextResponse.json({ error: err.message }, { status: 500 });
|
|
}
|
|
}
|