Skip to content
Open
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
5 changes: 2 additions & 3 deletions src/app/api/upload/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ export async function POST(req: Request) {
const formData = await req.formData();
const files = formData.getAll("files").filter(Boolean) as File[];
const isPdf = formData.get("isPdf") === "true";
const thumbnail = formData.get("thumbnail") as File | null;
const campus = formData.get("campus") as string | null;

if (files.length === 0) {
return failure("No files received.", 400);
}

const result = await uploadPaper({ files, isPdf, thumbnail, campus });
const result = await uploadPaper({ files, isPdf, campus });

if (!result.success) {
return failure(result.message, result.status);
Expand Down Expand Up @@ -55,7 +54,7 @@ export async function POST(req: Request) {
}

return success(
{ file_url: result.file_url, thumbnail_url: result.thumbnail_url },
{ file_url: result.file_url },
"Created",
201,
);
Expand Down
1 change: 0 additions & 1 deletion src/app/upload/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ GlobalWorkerOptions.workerSrc =

interface uploadResponse {
file_url: string;
thumbnail_url: string;
}
export default function Page() {
const campus = "Vellore";
Expand Down
16 changes: 4 additions & 12 deletions src/lib/services/upload.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PaperAdmin } from "@/db/papers";
import { createPDFfromImages, compressPDF } from "@/lib/storage/pdf";
import { uploadPDF, uploadThumbnail } from "@/lib/storage/gcp";
import { uploadPDF } from "@/lib/storage/gcp";
import { connectToDatabase } from "@/lib/database/mongoose";

const MAX_COMPRESSED_PDF_SIZE = 5 * 1024 * 1024; // 5MB compressed
Expand All @@ -9,18 +9,16 @@ const COMPRESS_THRESHOLD = 5 * 1024 * 1024; // 5MB
type UploadPaperInput = {
files: File[];
isPdf: boolean;
thumbnail: File | null;
campus: string | null;
};

type UploadPaperResult =
| { success: true; file_url: string; thumbnail_url: string | null }
| { success: true; file_url: string }
| { success: false; message: string; status: number };

export async function uploadPaper({
files,
isPdf,
thumbnail,
campus,
}: UploadPaperInput): Promise<UploadPaperResult> {
await connectToDatabase();
Expand Down Expand Up @@ -63,15 +61,9 @@ export async function uploadPaper({
const buffer = Buffer.from(pdfBytes);
const file_url = await uploadPDF("unapproved", buffer);

let thumbnail_url: string | null = null;
if (thumbnail) {
const thumbBuffer = Buffer.from(await thumbnail.arrayBuffer());
thumbnail_url = await uploadThumbnail(thumbBuffer, file_url);
}

const paper = new PaperAdmin({
file_url,
thumbnail_url,
thumbnail_url: null,
campus,
subject: null,
slot: null,
Expand All @@ -82,5 +74,5 @@ export async function uploadPaper({
});
await paper.save();

return { success: true, file_url, thumbnail_url };
return { success: true, file_url };
}
10 changes: 0 additions & 10 deletions src/lib/storage/gcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,4 @@ export async function uploadPDF(folder: string, buffer: Buffer) {
const pdfFilename = `${folder}/${Date.now()}-${Math.random().toString(36).substring(2)}.pdf`;
await bucket.file(pdfFilename).save(buffer, { resumable: false, contentType: "application/pdf" });
return `https://storage.googleapis.com/${bucketName}/${pdfFilename}`;
}

export async function uploadThumbnail(buffer: Buffer, pdfFilename: string) {
if (!bucket) {
throw new Error("Google Cloud Storage is not configured");
}
const bucketName = process.env.GOOGLE_CLOUD_BUCKET ?? "";
const thumbFilename = pdfFilename.replace(".pdf", ".png");
await bucket.file(thumbFilename).save(buffer, { resumable: false, contentType: "image/png" });
return `https://storage.googleapis.com/${bucketName}/${thumbFilename}`;
}