130 lines
No EOL
3.8 KiB
TypeScript
130 lines
No EOL
3.8 KiB
TypeScript
import { S3Client, PutObjectCommand, ListObjectsV2Command, ListBucketsCommand } from "@aws-sdk/client-s3";
|
|
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
|
|
import { NextRequest, NextResponse } from "next/server";
|
|
|
|
function getR2() {
|
|
// Debug: explicitly hardcode for now since env might not be passed to container
|
|
const accountId = "e71f22a2f8614fb3ba6d9b28a264d8ce";
|
|
const accessKeyId = "6606d525c8e647d94e051b6d6565803b";
|
|
const secretKey = "244fcc44041f452241cbc68374cd7a6ca651cb71f361bc36cc932407bbe37863";
|
|
const bucket = "tia";
|
|
|
|
if (!accountId || !accessKeyId || !secretKey || !bucket) {
|
|
throw new Error(`Missing R2 config`);
|
|
}
|
|
|
|
// S3 API endpoint includes bucket name: https://<accountId>.r2.cloudflarestorage.com/<bucket>
|
|
const endpoint = `https://${accountId}.r2.cloudflarestorage.com/${bucket}`;
|
|
|
|
return {
|
|
client: new S3Client({
|
|
region: "auto",
|
|
endpoint,
|
|
credentials: { accessKeyId, secretAccessKey: secretKey },
|
|
}),
|
|
bucket,
|
|
// Public URL uses pub- subdomain format
|
|
baseUrl: `https://pub-37a76fd657c94d1dbc521a109c087a11.r2.dev/tia`,
|
|
};
|
|
}
|
|
|
|
// GET: List memories
|
|
export async function GET(req: NextRequest) {
|
|
try {
|
|
const { client, bucket, baseUrl } = getR2();
|
|
const childId = req.nextUrl.searchParams.get("childId") || "default";
|
|
|
|
// List ALL objects in bucket (no prefix filtering for debugging)
|
|
const command = new ListObjectsV2Command({
|
|
Bucket: bucket,
|
|
MaxKeys: 100
|
|
});
|
|
|
|
const res = await client.send(command);
|
|
|
|
const objects = (res.Contents || []).map((obj) => ({
|
|
key: obj.Key,
|
|
url: `${baseUrl}/${obj.Key}`,
|
|
size: obj.Size,
|
|
lastModified: obj.LastModified?.toISOString(),
|
|
}));
|
|
|
|
return NextResponse.json({ memories: objects });
|
|
} catch (error) {
|
|
console.error("R2 list error:", error);
|
|
return NextResponse.json({ error: String(error) }, { status: 500 });
|
|
}
|
|
}
|
|
|
|
// POST: Get upload URL
|
|
export async function POST(req: NextRequest) {
|
|
let body;
|
|
try {
|
|
body = await req.json();
|
|
} catch {
|
|
return NextResponse.json({ error: "Invalid JSON" }, { status: 400 });
|
|
}
|
|
|
|
const { filename, contentType, childId } = body;
|
|
if (!filename || !contentType) {
|
|
return NextResponse.json({ error: "Missing filename or contentType" }, { status: 400 });
|
|
}
|
|
|
|
try {
|
|
const { client, bucket, baseUrl } = getR2();
|
|
|
|
const ext = filename.split(".").pop() || "jpg";
|
|
const key = `memories/${childId || "default"}/${Date.now()}-${Math.random().toString(36).slice(2, 8)}.${ext}`;
|
|
|
|
const command = new PutObjectCommand({
|
|
Bucket: bucket,
|
|
Key: key,
|
|
ContentType: contentType,
|
|
});
|
|
|
|
const url = await getSignedUrl(client, command, { expiresIn: 3600 });
|
|
|
|
return NextResponse.json({
|
|
uploadUrl: url,
|
|
key,
|
|
publicUrl: `${baseUrl}/${key}`,
|
|
});
|
|
} catch (error) {
|
|
console.error("R2 error:", error);
|
|
return NextResponse.json({ error: String(error) }, { status: 500 });
|
|
}
|
|
}
|
|
|
|
// PUT: Direct upload
|
|
export async function PUT(req: NextRequest) {
|
|
try {
|
|
const { client, bucket, baseUrl } = getR2();
|
|
const key = req.nextUrl.searchParams.get("key");
|
|
const contentType = req.nextUrl.searchParams.get("contentType") || "image/jpeg";
|
|
|
|
if (!key) {
|
|
return NextResponse.json({ error: "Missing key" }, { status: 400 });
|
|
}
|
|
|
|
const arrayBuffer = await req.arrayBuffer();
|
|
const buffer = Buffer.from(arrayBuffer);
|
|
|
|
const command = new PutObjectCommand({
|
|
Bucket: bucket,
|
|
Key: key,
|
|
Body: buffer,
|
|
ContentType: contentType,
|
|
});
|
|
|
|
await client.send(command);
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
key,
|
|
url: `${baseUrl}/${key}`,
|
|
});
|
|
} catch (error) {
|
|
console.error("R2 upload error:", error);
|
|
return NextResponse.json({ error: String(error) }, { status: 500 });
|
|
}
|
|
} |