Add image upload and edit functions
This commit is contained in:
21
src/app/api/artworks/page/route.ts
Normal file
21
src/app/api/artworks/page/route.ts
Normal file
@ -0,0 +1,21 @@
|
||||
// app/api/artworks/page/route.ts
|
||||
import { getArtworksPage } from "@/lib/queryArtworks";
|
||||
import { NextResponse } from "next/server";
|
||||
// import { getArtworksPage } from "@/lib/artworks/query";
|
||||
|
||||
export async function GET(req: Request) {
|
||||
const { searchParams } = new URL(req.url);
|
||||
|
||||
const published = (searchParams.get("published") ?? "all") as
|
||||
| "all"
|
||||
| "published"
|
||||
| "unpublished"
|
||||
| "needsWork";
|
||||
|
||||
const cursor = searchParams.get("cursor") ?? undefined;
|
||||
const take = Number(searchParams.get("take") ?? "48");
|
||||
|
||||
const data = await getArtworksPage({ published, cursor, take });
|
||||
|
||||
return NextResponse.json(data);
|
||||
}
|
||||
34
src/app/api/image/[...key]/route.ts
Normal file
34
src/app/api/image/[...key]/route.ts
Normal file
@ -0,0 +1,34 @@
|
||||
import { s3 } from "@/lib/s3";
|
||||
import { GetObjectCommand } from "@aws-sdk/client-s3";
|
||||
import "dotenv/config";
|
||||
|
||||
export async function GET(req: Request, { params }: { params: { key: string[] } }) {
|
||||
const { key } = await params;
|
||||
const s3Key = key.join("/");
|
||||
|
||||
try {
|
||||
const command = new GetObjectCommand({
|
||||
Bucket: `${process.env.BUCKET_NAME}`,
|
||||
Key: s3Key,
|
||||
});
|
||||
|
||||
const response = await s3.send(command);
|
||||
|
||||
if (!response.Body) {
|
||||
return new Response("No body", { status: 500 });
|
||||
}
|
||||
|
||||
const contentType = response.ContentType ?? "application/octet-stream";
|
||||
|
||||
return new Response(response.Body as ReadableStream, {
|
||||
headers: {
|
||||
"Content-Type": contentType,
|
||||
"Cache-Control": "public, max-age=3600",
|
||||
"Content-Disposition": "inline", // use 'attachment' to force download
|
||||
},
|
||||
});
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
return new Response("Image not found", { status: 404 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user