Skip to content

Commit cb96982

Browse files
Fixed admin route to work for public IDs too
1 parent 3a4911a commit cb96982

File tree

3 files changed

+32
-31
lines changed

3 files changed

+32
-31
lines changed

src/app/api/admin/route.ts

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export async function POST(req: Request) {
3434

3535
await connectToDatabase();
3636
let finalUrl: string | undefined = "";
37+
let public_id_cloudinary: string | undefined = "";
3738
let thumbnailUrl: string | undefined = "";
3839
const existingPaper = await Paper.findOne({
3940
subject,
@@ -60,15 +61,15 @@ export async function POST(req: Request) {
6061
}
6162

6263
const mergedPdfBytes = await CreatePDF(files);
63-
finalUrl = await uploadPDFFile(mergedPdfBytes, uploadPreset);
64+
[public_id_cloudinary, finalUrl] = await uploadPDFFile(mergedPdfBytes, uploadPreset);
6465
} catch (error) {
6566
return NextResponse.json(
6667
{ error: "Failed to process PDF" },
6768
{ status: 500 },
6869
);
6970
}
7071
} else {
71-
finalUrl = await uploadPDFFile(files[0] as File, uploadPreset);
72+
[public_id_cloudinary, finalUrl] = await uploadPDFFile(files[0] as File, uploadPreset);
7273
}
7374
const thumbnailResponse = cloudinary.v2.image(finalUrl!, {
7475
format: "jpg",
@@ -77,8 +78,9 @@ export async function POST(req: Request) {
7778
.replace("pdf", "jpg")
7879
.replace("upload", "upload/w_400,h_400,c_fill")
7980
.replace(/<img src='|'\s*\/>/g, "");
80-
8181
const paper = new Paper({
82+
83+
public_id_cloudinary,
8284
finalUrl,
8385
thumbnailUrl,
8486
subject,
@@ -101,40 +103,38 @@ export async function POST(req: Request) {
101103
}
102104
}
103105

104-
export async function DELETE(req: Request) {
105-
try {
106-
const url = new URL(req.url);
107-
const public_id = url.searchParams.get("public_id");
108-
const type = url.searchParams.get("type");
106+
// export async function DELETE(req: Request) {
107+
// try {
108+
// const url = new URL(req.url);
109+
// const public_id = url.searchParams.get("public_id");
110+
// const type = url.searchParams.get("type");
109111

110-
if (!public_id || !type) {
111-
return NextResponse.json(
112-
{ message: "Missing parameters: public_id or type" },
113-
{ status: 400 },
114-
);
115-
}
116-
await cloudinary.v2.uploader.destroy(public_id, {
117-
type: type,
118-
});
112+
// if (!public_id || !type) {
113+
// return NextResponse.json(
114+
// { message: "Missing parameters: public_id or type" },
115+
// { status: 400 },
116+
// );
117+
// }
118+
// await cloudinary.v2.uploader.destroy(public_id, {
119+
// type: type,
120+
// });
119121

120-
return NextResponse.json({ message: "Asset deleted successfully" });
121-
} catch (error) {
122-
return NextResponse.json(
123-
{ message: "Failed to delete asset", error },
124-
{ status: 500 },
125-
);
126-
}
127-
}
122+
// return NextResponse.json({ message: "Asset deleted successfully" });
123+
// } catch (error) {
124+
// return NextResponse.json(
125+
// { message: "Failed to delete asset", error },
126+
// { status: 500 },
127+
// );
128+
// }
129+
// }
128130
async function uploadPDFFile(file: File | ArrayBuffer, uploadPreset: string) {
129131
let bytes;
130-
if(file instanceof File)
132+
if(file instanceof File) //for pdf
131133
{
132-
console.log("PDF file");
133134
bytes = await file.arrayBuffer();
134135
}
135-
else
136+
else // for images that are pdf
136137
{
137-
console.log("Non PDF file");
138138
bytes = file as ArrayBuffer;
139139
}
140140
return uploadFile(bytes, uploadPreset, "application/pdf")
@@ -146,8 +146,7 @@ async function uploadPDFFile(file: File | ArrayBuffer, uploadPreset: string) {
146146

147147
const uploadResult: CloudinaryUploadResult =
148148
await cloudinary.v2.uploader.unsigned_upload(dataUrl, uploadPreset);
149-
150-
return uploadResult.secure_url;
149+
return [uploadResult.public_id, uploadResult.secure_url ];
151150
} catch (e) {
152151
throw (e);
153152
}

src/db/papers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import mongoose, { Schema, type Model } from "mongoose";
22
import { type IPaper } from "@/interface";
33

44
const paperSchema = new Schema<IPaper>({
5+
public_id_cloudinary: {type: String, required: true},
56
finalUrl: { type: String, required: true },
67
thumbnailUrl: { type: String, required: true },
78
subject: { type: String, required: true, index: true },

src/interface.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export interface PostRequestBody {
5656
}
5757

5858
export interface IPaper{
59+
public_id_cloudinary: string;
5960
finalUrl: string;
6061
thumbnailUrl: string;
6162
subject: string;

0 commit comments

Comments
 (0)