diff --git a/backend/server3.js b/backend/server3.js index ae14c7c..84732d8 100644 --- a/backend/server3.js +++ b/backend/server3.js @@ -8,11 +8,12 @@ import sqlite3 from 'sqlite3'; import jwt from 'jsonwebtoken'; import { v4 as uuidv4 } from 'uuid'; import path from 'path'; -import fs from 'fs'; +import fs from 'fs/promises'; import multer from 'multer'; import mammoth from 'mammoth'; import { fileURLToPath } from 'url'; -import * as pdfjsLib from 'pdfjs-dist/legacy/build/pdf.js'; +import pkg from 'pdfjs-dist'; + import OpenAI from 'openai'; // --- Basic file init --- @@ -28,6 +29,7 @@ dotenv.config({ path: envPath }); // Load .env file const app = express(); const PORT = process.env.PREMIUM_PORT || 5002; +const { getDocument } = pkg; let db; const initDB = async () => { @@ -1655,6 +1657,22 @@ ${resumeText} Precisely Tailored, ATS-Optimized Resume: `; +async function extractTextFromPDF(filePath) { + const fileBuffer = await fs.readFile(filePath); + const uint8Array = new Uint8Array(fileBuffer); // Convert Buffer explicitly + const pdfDoc = await getDocument({ data: uint8Array }).promise; + + let text = ''; + for (let pageNum = 1; pageNum <= pdfDoc.numPages; pageNum++) { + const page = await pdfDoc.getPage(pageNum); + const pageText = await page.getTextContent(); + text += pageText.items.map(item => item.str).join(' '); + } + return text; +} + + +// Your corrected endpoint with limits correctly returned: app.post( '/api/premium/resume/optimize', upload.single('resumeFile'), @@ -1668,7 +1686,6 @@ app.post( const userId = req.userId; const now = new Date(); - const currentWeek = getWeekNumber(now); // Function defined below const userProfile = await db.get( `SELECT is_premium, is_pro_premium, resume_optimizations_used, resume_limit_reset, resume_booster_count @@ -1702,16 +1719,20 @@ app.post( } const filePath = req.file.path; - const fileExt = req.file.originalname.split('.').pop().toLowerCase(); + const mimeType = req.file.mimetype; let resumeText = ''; - if (fileExt === 'pdf') { + if (mimeType === 'application/pdf') { resumeText = await extractTextFromPDF(filePath); - } else if (fileExt === 'docx') { + } else if ( + mimeType === 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' || + mimeType === 'application/msword' + ) { const result = await mammoth.extractRawText({ path: filePath }); resumeText = result.value; } else { - return res.status(400).json({ error: 'Unsupported file type.' }); + await fs.unlink(filePath); + return res.status(400).json({ error: 'Unsupported or corrupted file upload.' }); } const prompt = buildResumePrompt(resumeText, jobTitle, jobDescription); @@ -1728,12 +1749,16 @@ app.post( `UPDATE user_profile SET resume_optimizations_used = resume_optimizations_used + 1 WHERE user_id = ?`, [userId] ); - - // Calculate remaining optimizations + const remainingOptimizations = totalLimit - (userProfile.resume_optimizations_used + 1); - - fs.unlinkSync(filePath); - res.json({ optimizedResume, remainingOptimizations }); + + await fs.unlink(filePath); + res.json({ + optimizedResume, + remainingOptimizations, + resetDate: resetDate.toISOString() // <-- explicitly returned here! + }); + } catch (err) { console.error('Error optimizing resume:', err); res.status(500).json({ error: 'Failed to optimize resume.' }); diff --git a/src/App.js b/src/App.js index d203acd..93907df 100644 --- a/src/App.js +++ b/src/App.js @@ -236,12 +236,12 @@ function App() { {/* Logout */}
{error}
} -