Refactor PDF extraction to use chunked upload to bypass proxy size limits
Build and Deploy / build-and-push (push) Successful in 30s
Build and Deploy / build-and-push (push) Successful in 30s
This commit is contained in:
+19
-6
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user