Skip to content

Commit 9161fe3

Browse files
fix: convert image to base64 buffer instead of plain string. connect to mongo before accessing it in cloudinary db
1 parent 3e53bde commit 9161fe3

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

src/app/api/ai-upload/route.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ export async function POST(req: Request) {
5050
if (!process.env.NEXT_PUBLIC_CLOUDINARY_UPLOAD_PRESET) {
5151
return NextResponse.json({ message: "ServerMisconfig" }, { status: 500 });
5252
}
53-
const count: number = await Paper.countDocuments();
53+
await connectToDatabase();
54+
55+
const count: number = await PaperAdmin.countDocuments();
5456
const configIndex = count % cloudinaryConfigs.length;
5557
const selectedConfig = cloudinaryConfigs[configIndex];
5658
cloudinary.v2.config(selectedConfig);
@@ -66,7 +68,7 @@ export async function POST(req: Request) {
6668
const bytes = await files[0]?.arrayBuffer();
6769
if (bytes) {
6870
const buffer = Buffer.from(bytes);
69-
imageURL = `data:${"image/png"};base64,${buffer.toString("base64")}`;
71+
imageURL = buffer.toString("base64"); // Plain Base64 string, no data URL prefix
7072
}
7173
}
7274
const tags = await processAndAnalyze({ imageURL });
@@ -125,7 +127,6 @@ export async function POST(req: Request) {
125127

126128
// If all checks pass, continue with the rest of the logic
127129

128-
await connectToDatabase();
129130
let finalUrl: string | undefined = "";
130131
let public_id_cloudinary: string | undefined = "";
131132
let thumbnailUrl: string | undefined = "";

src/app/upload/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ async function pdfToImage(file: File) {
3737
await pdfPage.render({ canvasContext: context, viewport }).promise;
3838

3939
// Convert the canvas to the desired output (Buffer, base64, etc.)
40-
return canvas.toDataURL(); // Returns a Base64 string
40+
return canvas.toDataURL().replace(/^data:image\/\w+;base64,/, "");
4141
}
4242

4343
const Page = () => {

0 commit comments

Comments
 (0)