const axios = require("axios"); const FormData = require("form-data"); const uploadPDF = async (req, res) => { try { const file = req.file; if (!file) { return res .status(400) .json({ success: false, message: "No file uploaded" }); } const { name, sessionId, title, summary, categories } = req.body; const formData = new FormData(); formData.append("pdf_file", file.buffer, file.originalname); formData.append("name", name); formData.append("sessionId", sessionId); formData.append("title", title); formData.append("summary", summary); formData.append("categories", categories); const response = await axios.post( "http://localhost:8082/upload-pdf", formData, { headers: { ...formData.getHeaders(), }, } ); // fs.unlink(file.path, (err) => { // if (err) console.error("Error deleting the file:", err); // else console.log("File deleted successfully"); // }); return res.status(200).json({ success: true, message: "PDF uploaded and processed successfully", data: response.data, }); } catch (error) { console.warn("Error in uploadPDF:", error); return res.status(500).json({ success: false, message: "Failed to process PDF", error: error.message, }); } }; const uploadLink = async (req, res) => { try { const { link, sessionId, title, summary, categories } = req.body; const response = await axios.post("http://localhost:8082/process-link", { link, sessionId, title, summary, categories, }); console.info("Response : ", response.data); if (response.data.status === "success") { // If the FastAPI endpoint indicates success return res.status(200).json({ success: true, message: "Link processed successfully", data: response.data, // Include any data if needed }); } else { // If the FastAPI endpoint indicates an error return res.status(400).json({ success: false, message: response.data.detail || "Failed to process link. Please try again", }); } } catch (error) { console.error("Error in uploading link : ", error); if (error.response) { const { status, data } = error.response; return res.status(status).json({ success: false, message: data.detail || "Failed to process link. Please try again", }); } else if (error.request) { console.error("No response received from the server:", error.request); return res.status(500).json({ success: false, message: "No response received from the server. Please try again later.", }); } else { // Something happened in setting up the request that triggered an Error console.error("Error in setting up the request:", error.message); return res.status(500).json({ success: false, message: "An unexpected error occurred. Please try again", }); } } }; const uploadText = async (req, res) => { try { const { text, sessionId, name, title, summary, categories } = req.body; const response = await axios.post("http://localhost:8082/process-text", { text, sessionId, title, name, summary, categories, }); console.info("Response : ", response.data); if (response.data.status === "success") { // If the FastAPI endpoint indicates success return res.status(200).json({ success: true, message: "Link processed successfully", data: response.data, // Include any data if needed }); } else { // If the FastAPI endpoint indicates an error return res.status(400).json({ success: false, message: response.data.detail || "Failed to process link. Please try again", }); } } catch (error) { console.error("Error in uploading link : ", error); if (error.response) { const { status, data } = error.response; return res.status(status).json({ success: false, message: data.detail || "Failed to process link. Please try again", }); } else if (error.request) { console.error("No response received from the server:", error.request); return res.status(500).json({ success: false, message: "No response received from the server. Please try again later.", }); } else { // Something happened in setting up the request that triggered an Error console.error("Error in setting up the request:", error.message); return res.status(500).json({ success: false, message: "An unexpected error occurred. Please try again", }); } } }; const clearContext = async (req, res) => { try { const { sessionId } = req.body; const response = await axios.post("http://localhost:8082/clear-context", { sessionId, }); const { data } = response; if (data.status === "success") { return res .status(200) .json({ success: true, message: "context cleared successfully" }); } return res .status(400) .json({ message: "failed to clear context", success: false }); } catch (error) { console.error("Error in clearing all context : ", error); return res .status(500) .json({ message: "failed to clear context", success: false }); } }; module.exports = { uploadPDF, uploadLink, clearContext, uploadText };