first commit
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
import { syncUnsplashWallpapersToGallery } from "@/server/admin/gallery-sync";
|
||||
import { NextResponse, type NextRequest } from "next/server";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
function isAuthorized(request: NextRequest) {
|
||||
const cronSecret = process.env.CRON_SECRET;
|
||||
|
||||
if (!cronSecret) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return request.headers.get("authorization") === `Bearer ${cronSecret}`;
|
||||
}
|
||||
|
||||
async function handleGalleryPhotosCron(request: NextRequest) {
|
||||
if (!process.env.CRON_SECRET) {
|
||||
return NextResponse.json(
|
||||
{ ok: false, error: "CRON_SECRET is not configured." },
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
|
||||
if (!isAuthorized(request)) {
|
||||
return NextResponse.json(
|
||||
{ ok: false, error: "Unauthorized." },
|
||||
{ status: 401 },
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await syncUnsplashWallpapersToGallery();
|
||||
return NextResponse.json({ ok: true, result });
|
||||
} catch (error) {
|
||||
const message =
|
||||
error instanceof Error ? error.message : "Unknown cron error.";
|
||||
|
||||
return NextResponse.json(
|
||||
{ ok: false, error: message },
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
return handleGalleryPhotosCron(request);
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
return handleGalleryPhotosCron(request);
|
||||
}
|
||||
Reference in New Issue
Block a user