youtuber-blog-frontend/build/server/chunks/_server-BWUlNVMP.js
2024-06-07 17:11:44 +02:00

48 lines
1.6 KiB
JavaScript

import { e as error, j as json } from './index-Ddp2AB5f.js';
import S3 from 'aws-sdk/clients/s3.js';
const s3 = new S3({
accessKeyId: "c39c1d310e9f6afd16840b81c7870c16",
secretAccessKey: "0d336803615f0a35026624237fedf5b62e3d5ada60287067f98e11cdcfc48aa9",
region: "auto",
endpoint: "https://f79c2f6c3ee16c813cbc853bc7e16166.r2.cloudflarestorage.com"
});
const BUCKET_NAME = "youpage";
async function POST({ request }) {
const data = await request.formData();
const file = data.get("file");
const oldFilePath = data.get("oldFilePath");
const blog_id = data.get("id");
if (!file) {
return error(400, "No file uploaded");
}
const filePath = `uploads/${blog_id}-${file.name.slice(0, 16)}`;
try {
if (file.size > 1048576 * 5) {
return error(400, "File size exceeds 5mb");
}
const uploadParams = {
Bucket: BUCKET_NAME,
Key: filePath,
Body: new Uint8Array(await file.arrayBuffer()),
ContentType: file.type
};
await s3.upload(uploadParams).promise();
if (oldFilePath && oldFilePath !== "undefined") {
console.log("old file: ", oldFilePath);
const deleteParams = {
Bucket: BUCKET_NAME,
Key: oldFilePath
};
await s3.deleteObject(deleteParams).promise();
}
return json({ filePath: `https://pub-ccf2d12acd4047ab951129d9788c75d7.r2.dev/${filePath.replace("/", "%2F")}` });
} catch (error2) {
console.error("File upload failed:", error2);
return json({ error: "File upload failed" }, { status: 500 });
}
}
export { POST };
//# sourceMappingURL=_server-BWUlNVMP.js.map