File size: 5,578 Bytes
5306da4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
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 };