2025-02-27 22:10:01 +01:00

119 lines
4.7 KiB
TypeScript

import 'core-js/features/promise/with-resolvers'; // Polyfill for Promise.withResolvers
import { NextResponse } from "next/server";
import fs from "fs";
import path from "path";
import * as pdfjsLib from 'pdfjs-dist';
import '../../../public/utils/pdf.worker.mjs';
import { v4 as uuidv4 } from 'uuid';
import { fileTypeFromBuffer } from 'file-type';
const uploadDir = path.join(process.cwd(), "uploads", "cv");
// Ensure the upload directory exists
if (!fs.existsSync(uploadDir)) {
fs.mkdirSync(uploadDir, { recursive: true });
}
async function extractTextFromPdf(pdfPath: string): Promise<string> {
console.log("Starting extractTextFromPdf for path:", pdfPath);
try {
console.log("Reading PDF file:", pdfPath);
const data = new Uint8Array(fs.readFileSync(pdfPath));
console.log("PDF file read successfully. Starting loading document...");
const loadingTask = pdfjsLib.getDocument({ data });
console.log("Loading task initiated. Waiting for promise...");
const pdf = await loadingTask.promise; // Await the PDF loading
console.log("PDF document loaded successfully. Number of pages:", pdf.numPages);
let fullText = "";
const processPages = async () => {
for (let i = 1; i <= pdf.numPages; i++) {
console.log("Processing page:", i);
const page = await pdf.getPage(i);
console.log("Page", i, "loaded. Getting text content...");
const textContent = await page.getTextContent();
console.log("Text content for page", i, "obtained. Processing items...");
fullText += textContent.items.map((item: any) => item.str ? item.str : '').join(" ");
console.log("Text from page", i, "added to fullText.");
}
console.log("Text extraction completed successfully.");
console.log("Parsed PDF Text before return:", fullText); // Added log here
return fullText;
};
return await processPages(); // Await the page processing
} catch (error) {
console.error("Error extracting text from PDF:", error);
throw new Error("Error extracting text from PDF");
} finally {
console.log("Finished extractTextFromPdf for path:", pdfPath);
}
}
export async function POST(req: Request) {
console.log("Received request for CV file upload");
try {
const formData = await req.formData();
const file: File | null = formData.get('cv') as unknown as File | null;
if (!file) {
console.warn("No file uploaded.");
return NextResponse.json({ message: "No file uploaded." }, { status: 400 });
}
const originalFilename = file.name;
const uniqueFilename = `${uuidv4()}-${originalFilename}`;
const newFilePath = path.join(uploadDir, uniqueFilename);
console.log(`Saving file to: ${newFilePath}`);
const fileBuffer = await file.arrayBuffer();
const type = await fileTypeFromBuffer(Buffer.from(fileBuffer));
console.log("Detected file type:", type);
if (!type || type.mime !== 'application/pdf') {
return NextResponse.json({ message: "Unsupported file type detected. Only PDF files are allowed." }, { status: 400 });
}
await fs.promises.writeFile(newFilePath, Buffer.from(fileBuffer));
console.log("File uploaded and saved successfully!");
console.log("Before PDF parsing");
const extractedText = await extractTextFromPdf(newFilePath);
console.log("After PDF parsing");
console.log("Before generating summary");
const command = `python3 utils/resume_analysis.py "${extractedText}"`;
console.log("Executing python command:", command);
console.log("Extracted Text being passed to python script:", extractedText);
console.log("Length of extractedText:", extractedText.length); // Log length
const executionResult: { stdout: string, stderr: string } = await new Promise((resolve, reject) => {
require('child_process').exec(command, (error: any, stdout: string, stderr: string) => {
if (error) {
console.error("Python script execution error:", error);
console.error("Python script stderr:", stderr);
reject({ error, stdout, stderr });
} else {
console.log("Python script executed successfully");
console.log("Python script stdout:", stdout);
resolve({ stdout, stderr });
}
});
});
const { stdout, stderr } = executionResult;
if (stderr) {
console.error("Error from python script (stderr):", stderr);
}
const summary: string = stdout.trim();
console.log("After generating summary");
return NextResponse.json({ summary: summary }, { status: 200 });
} catch (error: any) {
console.error("Error during file processing:", error);
return NextResponse.json({ message: "Error processing file: " + error.message }, { status: 500 });
}
}