Skip to content
Open
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
44 changes: 22 additions & 22 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ require("dotenv").config();
const express = require("express");
const cors = require("cors");
const multer = require("multer");
const crypto = require('crypto');
const axios = require("axios");
const fs = require("fs");
const fsPromises = require("fs/promises");
Expand Down Expand Up @@ -446,32 +447,24 @@ const startUploadsCleanup = () => {
};

const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, UPLOADS_DIR);
},
destination: (req, file, cb) => cb(null, 'uploads/'),
filename: (req, file, cb) => {
cb(null, `${crypto.randomUUID()}.pdf`);
},
const unique = `${Date.now()}-${crypto.randomUUID()}`;
const ext = path.extname(file.originalname).toLowerCase();
cb(null, unique + ext);
},
});

const upload = multer({
storage,
limits: {
fileSize: MAX_PDF_SIZE_BYTES,
// Additional limits to prevent abuse
files: 1, // Only allow one file per request
},
fileFilter: (req, file, cb) => {
const isPdfMime = file.mimetype === "application/pdf";
const isPdfExtension = file.originalname.toLowerCase().endsWith(".pdf");

if (!isPdfMime || !isPdfExtension) {
return cb(new Error("Only PDF files are allowed."));
}

const fileFilter = (req, file, cb) => {
const ext = path.extname(file.originalname).toLowerCase();
if (file.mimetype === 'application/pdf' && ext === '.pdf') {
cb(null, true);
},
});
} else {
cb(new Error('Only PDF files are allowed.'), false);
}
};

const upload = multer({ storage, fileFilter });

const cleanupFile = async (filePath) => {
if (!filePath) return;
Expand Down Expand Up @@ -1129,6 +1122,13 @@ app.get("/health", (req, res) => {
res.json({ status: "ok" });
});

app.use((err, req, res, next) => {
if (err.message === 'Only PDF files are allowed.') {
return res.status(400).json({ error: err.message });
}
next(err);
});

app.use((req, res, next) => {
res.status(404).json({ error: "Not found" });
});
Expand Down
Loading