Skip to content

Commit 111b68d

Browse files
Merge pull request #94 from abhitrueprogrammer/staging
feat: upload files based on file lastmodified order
2 parents 499c94d + e7af2af commit 111b68d

File tree

2 files changed

+33
-35
lines changed

2 files changed

+33
-35
lines changed

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

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export async function POST(req: Request) {
4040
const files: File[] = formData.getAll("files") as File[];
4141
const isPdf = formData.get("isPdf") === "true"; // Convert string to boolean
4242

43+
4344
let imageURL = "";
4445
if (isPdf) {
4546
imageURL = formData.get("image") as string;
@@ -58,53 +59,53 @@ export async function POST(req: Request) {
5859
const slot = finalTags.slot;
5960
const exam = finalTags["exam-type"];
6061
const year = finalTags.year;
61-
const campus = formData.get("campus") as string
62+
const campus = formData.get("campus") as string;
6263
const semester = finalTags.semester;
6364

6465
if (!courses.includes(subject)) {
6566
return NextResponse.json(
6667
{ message: "The course subject is invalid." },
67-
{ status: 400 }
68+
{ status: 400 },
6869
);
6970
}
70-
71+
7172
if (!slots.includes(slot)) {
7273
return NextResponse.json(
7374
{ message: "The slot is invalid." },
74-
{ status: 400 }
75+
{ status: 400 },
7576
);
7677
}
77-
78+
7879
if (!exam.includes(exam)) {
7980
return NextResponse.json(
8081
{ message: "The exam type is invalid." },
81-
{ status: 400 }
82+
{ status: 400 },
8283
);
8384
}
84-
85+
8586
if (!years.includes(year)) {
8687
return NextResponse.json(
8788
{ message: "The year is invalid." },
88-
{ status: 400 }
89+
{ status: 400 },
8990
);
9091
}
91-
92+
9293
if (!campuses.includes(campus)) {
9394
return NextResponse.json(
94-
{ message:`The ${campus} is invalid.` },
95-
{ status: 400 }
95+
{ message: `The ${campus} is invalid.` },
96+
{ status: 400 },
9697
);
9798
}
98-
99+
99100
if (!semesters.includes(semester)) {
100101
return NextResponse.json(
101102
{ message: "The semester is invalid." },
102-
{ status: 400 }
103+
{ status: 400 },
103104
);
104105
}
105-
106+
106107
// If all checks pass, continue with the rest of the logic
107-
108+
108109
await connectToDatabase();
109110
let finalUrl: string | undefined = "";
110111
let public_id_cloudinary: string | undefined = "";
@@ -160,10 +161,7 @@ export async function POST(req: Request) {
160161
semester,
161162
});
162163
await paper.save();
163-
return NextResponse.json(
164-
{ status: "success", },
165-
{ status: 201 },
166-
);
164+
return NextResponse.json({ status: "success" }, { status: 201 });
167165
} catch (error) {
168166
console.error(error);
169167
return NextResponse.json(
@@ -201,12 +199,13 @@ async function uploadFile(
201199
}
202200
}
203201

204-
async function CreatePDF(files: File[]) {
202+
async function CreatePDF(orderedFiles: File[]) {
205203
const pdfDoc = await PDFDocument.create();
206204
//sort files using name. Later remove to see if u can without names
207-
const orderedFiles = Array.from(files).sort((a, b) => {
208-
return a.name.localeCompare(b.name);
209-
});
205+
// moved to main function
206+
// const orderedFiles = Array.from(files).sort((a, b) => {
207+
// return a.name.localeCompare(b.name);
208+
// });
210209

211210
for (const file of orderedFiles) {
212211
const fileBlob = new Blob([file]);
@@ -286,11 +285,6 @@ async function setTagsFromCurrentLists(
286285
}
287286
function findMatch<T>(arr: T[], value: string | undefined): T | undefined {
288287
if (!value) return undefined; // Handle undefined case
289-
const pattern = new RegExp(
290-
value
291-
.split("")
292-
.map((char) => `(?=.*${char})`)
293-
.join(""),
294-
"i"
295-
); return arr.find((item) => pattern.test(String(item)));
296-
}
288+
const pattern = new RegExp(value, "i");
289+
return arr.find((item) => pattern.test(String(item)));
290+
}

src/app/upload/page.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ const Page = () => {
4444
const [campus, setCampus] = useState("Vellore");
4545

4646
const [files, setFiles] = useState<File[]>([]);
47+
4748
const [isUploading, setIsUploading] = useState(false);
4849
const [, setResetSearch] = useState(false);
49-
function pdfCheckAndSelect<T extends File>(acceptedFiles: T[]) {
50+
function fileCheckAndSelect<T extends File>(acceptedFiles: T[]) {
5051
const maxFileSize = 5 * 1024 * 1024;
5152
const allowedFileTypes = [
5253
"application/pdf",
@@ -96,8 +97,11 @@ const Page = () => {
9697
return;
9798
}
9899

99-
setFiles(acceptedFiles);
100-
toast.success(`${acceptedFiles.length} files selected!`, {
100+
const orderedFiles = files.sort((a, b) => {
101+
return a.lastModified - b.lastModified;
102+
});
103+
setFiles(orderedFiles);
104+
toast.success(`${orderedFiles.length} files selected!`, {
101105
id: toastId,
102106
});
103107
}
@@ -160,7 +164,7 @@ const Page = () => {
160164
<div className="flex w-full flex-col 2xl:gap-y-4">
161165
{/* File Dropzone */}
162166
<div>
163-
<Dropzone onDrop={pdfCheckAndSelect}>
167+
<Dropzone onDrop={fileCheckAndSelect}>
164168
{({ getRootProps, getInputProps }) => (
165169
<section className="my-2 -mr-2 cursor-pointer rounded-2xl border-2 border-dashed p-8 text-center">
166170
<div {...getRootProps()}>

0 commit comments

Comments
 (0)