Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 25 additions & 31 deletions src/app/api/ai-upload/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export async function POST(req: Request) {
const files: File[] = formData.getAll("files") as File[];
const isPdf = formData.get("isPdf") === "true"; // Convert string to boolean


let imageURL = "";
if (isPdf) {
imageURL = formData.get("image") as string;
Expand All @@ -58,53 +59,53 @@ export async function POST(req: Request) {
const slot = finalTags.slot;
const exam = finalTags["exam-type"];
const year = finalTags.year;
const campus = formData.get("campus") as string
const campus = formData.get("campus") as string;
const semester = finalTags.semester;

if (!courses.includes(subject)) {
return NextResponse.json(
{ message: "The course subject is invalid." },
{ status: 400 }
{ status: 400 },
);
}

if (!slots.includes(slot)) {
return NextResponse.json(
{ message: "The slot is invalid." },
{ status: 400 }
{ status: 400 },
);
}

if (!exam.includes(exam)) {
return NextResponse.json(
{ message: "The exam type is invalid." },
{ status: 400 }
{ status: 400 },
);
}

if (!years.includes(year)) {
return NextResponse.json(
{ message: "The year is invalid." },
{ status: 400 }
{ status: 400 },
);
}

if (!campuses.includes(campus)) {
return NextResponse.json(
{ message:`The ${campus} is invalid.` },
{ status: 400 }
{ message: `The ${campus} is invalid.` },
{ status: 400 },
);
}

if (!semesters.includes(semester)) {
return NextResponse.json(
{ message: "The semester is invalid." },
{ status: 400 }
{ status: 400 },
);
}

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

await connectToDatabase();
let finalUrl: string | undefined = "";
let public_id_cloudinary: string | undefined = "";
Expand Down Expand Up @@ -160,10 +161,7 @@ export async function POST(req: Request) {
semester,
});
await paper.save();
return NextResponse.json(
{ status: "success", },
{ status: 201 },
);
return NextResponse.json({ status: "success" }, { status: 201 });
} catch (error) {
console.error(error);
return NextResponse.json(
Expand Down Expand Up @@ -201,12 +199,13 @@ async function uploadFile(
}
}

async function CreatePDF(files: File[]) {
async function CreatePDF(orderedFiles: File[]) {
const pdfDoc = await PDFDocument.create();
//sort files using name. Later remove to see if u can without names
const orderedFiles = Array.from(files).sort((a, b) => {
return a.name.localeCompare(b.name);
});
// moved to main function
// const orderedFiles = Array.from(files).sort((a, b) => {
// return a.name.localeCompare(b.name);
// });

for (const file of orderedFiles) {
const fileBlob = new Blob([file]);
Expand Down Expand Up @@ -286,11 +285,6 @@ async function setTagsFromCurrentLists(
}
function findMatch<T>(arr: T[], value: string | undefined): T | undefined {
if (!value) return undefined; // Handle undefined case
const pattern = new RegExp(
value
.split("")
.map((char) => `(?=.*${char})`)
.join(""),
"i"
); return arr.find((item) => pattern.test(String(item)));
}
const pattern = new RegExp(value, "i");
return arr.find((item) => pattern.test(String(item)));
}
12 changes: 8 additions & 4 deletions src/app/upload/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ const Page = () => {
const [campus, setCampus] = useState("Vellore");

const [files, setFiles] = useState<File[]>([]);

const [isUploading, setIsUploading] = useState(false);
const [, setResetSearch] = useState(false);
function pdfCheckAndSelect<T extends File>(acceptedFiles: T[]) {
function fileCheckAndSelect<T extends File>(acceptedFiles: T[]) {
const maxFileSize = 5 * 1024 * 1024;
const allowedFileTypes = [
"application/pdf",
Expand Down Expand Up @@ -96,8 +97,11 @@ const Page = () => {
return;
}

setFiles(acceptedFiles);
toast.success(`${acceptedFiles.length} files selected!`, {
const orderedFiles = files.sort((a, b) => {
return a.lastModified - b.lastModified;
});
setFiles(orderedFiles);
toast.success(`${orderedFiles.length} files selected!`, {
id: toastId,
});
}
Expand Down Expand Up @@ -160,7 +164,7 @@ const Page = () => {
<div className="flex w-full flex-col 2xl:gap-y-4">
{/* File Dropzone */}
<div>
<Dropzone onDrop={pdfCheckAndSelect}>
<Dropzone onDrop={fileCheckAndSelect}>
{({ getRootProps, getInputProps }) => (
<section className="my-2 -mr-2 cursor-pointer rounded-2xl border-2 border-dashed p-8 text-center">
<div {...getRootProps()}>
Expand Down