Refactor PDF extraction to use chunked upload to bypass proxy size limits
Build and Deploy / build-and-push (push) Successful in 30s

This commit is contained in:
AI Bot
2026-09-02 16:39:36 +05:30
parent 7a45b7a262
commit e941be0242
4 changed files with 302 additions and 11 deletions
+19 -6
View File
@@ -323,16 +323,25 @@ const getKeysFromProfile = async (userId) => {
};
// 1. Extract PDF (Gemini)
app.post('/api/sparkplug/extract', authenticate, upload.single('file'), async (req, res) => {
if (!req.file) return res.status(400).json({ error: 'No file uploaded' });
app.post('/api/sparkplug/extract', authenticate, async (req, res) => {
const { fileUrl } = req.body;
if (!fileUrl) return res.status(400).json({ error: 'No fileUrl provided' });
try {
const keys = await getKeysFromProfile(req.user.id);
const geminiKey = keys.GEMINI_API_KEY;
if (!geminiKey) return res.status(400).json({ error: 'Gemini API key missing' });
// Parse PDF
const dataBuffer = fs.readFileSync(req.file.path);
// Read PDF from the local file system
// fileUrl is something like '/uploads/1788251962641-55866782-file.pdf'
const fileName = fileUrl.replace('/uploads/', '');
const localFilePath = path.join(uploadsDir, fileName);
if (!fs.existsSync(localFilePath)) {
return res.status(404).json({ error: 'File not found on server' });
}
const dataBuffer = fs.readFileSync(localFilePath);
const base64Pdf = dataBuffer.toString('base64');
// Call Gemini to extract prompt
@@ -364,8 +373,12 @@ app.post('/api/sparkplug/extract', authenticate, upload.single('file'), async (r
} catch (err) {
res.status(500).json({ error: err.message });
} finally {
if (req.file) {
fs.unlinkSync(req.file.path); // cleanup uploaded PDF
if (fileUrl) {
const fileName = fileUrl.replace('/uploads/', '');
const localFilePath = path.join(uploadsDir, fileName);
if (fs.existsSync(localFilePath)) {
fs.unlinkSync(localFilePath); // cleanup uploaded PDF
}
}
}
});