forked from shalonjovan/VidyaGyan
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
470 lines (393 loc) · 17.2 KB
/
Copy pathserver.js
File metadata and controls
470 lines (393 loc) · 17.2 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
const express = require('express');
const fs = require('fs');
const bodyParser = require('body-parser');
const cors = require('cors');
const path = require('path');
const multer = require('multer');
const axios = require('axios');
const bcrypt = require('bcrypt');
require('dotenv').config();
const app = express();
// app.listen(PORT, () => {
// console.log(`Server running on port ${PORT}`);
// });
// In-memory users storage
let users = []; // temporary, resets on redeploy
app.use(bodyParser.json());
app.use(cors());
// server.js
// ... (your existing code) ...
// --- ADDED: Multer setup for storing profile pictures ---
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'uploads/'); // The directory where files will be stored
},
filename: function (req, file, cb) {
// Use the username from the request body to create a unique filename
// This will overwrite the previous picture if a new one is uploaded for the same user
const username = req.body.username;
const fileExtension = path.extname(file.originalname);
cb(null, username + fileExtension);
}
});
// --- ADD THIS LINE TO FIX THE ERROR ---
const upload = multer({ storage: storage });
// --- API Endpoints ---
// --- ADDED: Endpoint to handle profile picture upload ---
app.post('/upload-profile-picture', upload.single('profilePicture'), (req, res) => {
if (!req.file) {
return res.status(400).json({ message: 'No file was uploaded.' });
}
// The file is now saved. Send a success response.
res.status(200).json({ message: 'Profile picture uploaded successfully!', filePath: req.file.path });
});
// ... (rest of your code) ...
// --- Gemini API Configuration ---
const GEMINI_API_KEY = process.env.GEMINI_API_KEY;
const GEMINI_API_URL = `https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent?key=${GEMINI_API_KEY}`;
app.use(express.static(path.join(__dirname, '/')));
// Define file paths
const usersFilePath = 'users.json';
const assessmentFilePath = 'assessment_results.json';
const collegesFilePath = 'colleges.json';
const coursesFilePath = 'courses.json';
// Utility functions (read/write JSON)
const readJsonFile = (filePath, defaultValue = []) => {
if (fs.existsSync(filePath)) {
try {
const data = fs.readFileSync(filePath, 'utf8');
if (data.trim() === '') return defaultValue;
return JSON.parse(data);
} catch (err) {
console.error(`Error parsing JSON file at ${filePath}:`, err);
return defaultValue;
}
}
return defaultValue;
};
const writeJsonFile = (filePath, data) => {
try {
fs.writeFileSync(filePath, JSON.stringify(data, null, 2));
} catch (err) {
console.error(`Error writing JSON file at ${filePath}:`, err);
}
};
// Helper function to extract college filename from link
const getCollegeFileName = (link) => {
// Extract filename from link like "./college-dashboard.html?data=./colleges/college-name"
const match = link.match(/\.\/colleges\/(.+)$/);
return match ? match[1] : null;
};
// Helper function to match specialized fields to college types
const matchFieldsToCollegeTypes = (specializedFields) => {
const typeMap = {
'Engineering': ['computer', 'software', 'ai', 'artificial intelligence', 'machine learning', 'data science', 'robotics', 'cybersecurity', 'engineering', 'technology'],
'Medical': ['medicine', 'medical', 'health', 'biology', 'biotechnology', 'biomedical'],
'Management': ['management', 'business', 'mba'],
'University': ['science', 'physics', 'chemistry', 'mathematics', 'research'],
'Agricultural University': ['agriculture', 'environmental'],
'Arts & Science': ['arts', 'literature', 'humanities'],
'Science': ['science', 'physics', 'chemistry', 'mathematics'],
'Commerce': ['commerce', 'economics', 'finance']
};
const matchedTypes = new Set();
specializedFields.forEach(field => {
const fieldLower = field.toLowerCase();
Object.keys(typeMap).forEach(type => {
typeMap[type].forEach(keyword => {
if (fieldLower.includes(keyword)) {
matchedTypes.add(type);
}
});
});
});
return Array.from(matchedTypes);
};
// --- API Endpoints ---
// --- ADDED: Endpoint to handle profile picture upload ---
// server.js
app.post('/upload-profile-picture', upload.single('profilePicture'), (req, res) => {
if (!req.file) {
return res.status(400).json({ message: 'No file was uploaded.' });
}
// --- THIS IS THE CRUCIAL ADDITION ---
// Now, we save the file path to the user's data.
const { username } = req.body; // Get username from the form data
const filePath = `/uploads/${req.file.filename}`; // The public URL path to the file
if (!username) {
return res.status(400).json({ message: 'Username is required to save the picture.' });
}
try {
const users = readJsonFile(usersFilePath);
const userIndex = users.findIndex(u => u.username === username);
if (userIndex !== -1) {
// Add or update the profilePicture property for the user
users[userIndex].profilePicture = filePath;
writeJsonFile(usersFilePath, users);
console.log(`Updated profile picture for ${username}. Path: ${filePath}`);
// The file is saved and the user record is updated. Send a success response.
res.status(200).json({
message: 'Profile picture uploaded successfully!',
filePath: filePath
});
} else {
// Handle case where user is not found
res.status(404).json({ message: 'User not found.' });
}
} catch (error) {
console.error('Error updating user data with profile picture:', error);
res.status(500).json({ message: 'Server error while updating user data.' });
}
});
// --- ADDED: Endpoint to retrieve and serve a user's profile picture ---
app.get('/profile-picture/:username', (req, res) => {
const { username } = req.params;
const extensions = ['.png', '.jpg', '.jpeg', '.gif']; // Common image extensions
let userImagePath = null;
// Check for the user's image file with different possible extensions
for (const ext of extensions) {
const potentialPath = path.join(__dirname, 'uploads', username + ext);
if (fs.existsSync(potentialPath)) {
userImagePath = potentialPath;
break;
}
}
if (userImagePath) {
res.sendFile(userImagePath);
} else {
res.status(404).json({ message: 'Profile picture not found.' });
}
});
app.get('/get-colleges', (req, res) => {
const colleges = readJsonFile(collegesFilePath);
res.json(colleges);
});
// Fixed endpoint to match frontend call
app.get('/get-assessment-results', (req, res) => {
const { username } = req.query;
if (!username) return res.status(400).json({ error: 'Username is required.' });
const allResults = readJsonFile(assessmentFilePath);
// Look for both username and userName for backwards compatibility
const userResult = allResults.find(result =>
result.username === username || result.userName === username
);
if (userResult) {
res.json(userResult); // Return the result directly, not wrapped in {result: ...}
} else {
res.status(404).json({ error: 'Assessment results not found for this user.' });
}
});
// Keep the old endpoint for backwards compatibility
app.get('/get-assessment/:username', (req, res) => {
const { username } = req.params;
if (!username) return res.status(400).json({ error: 'Username is required.' });
const allResults = readJsonFile(assessmentFilePath);
const userResult = allResults.find(result =>
result.username === username || result.userName === username
);
if (userResult) {
res.json({ result: userResult });
} else {
res.status(404).json({ error: 'Assessment results not found for this user.' });
}
});
app.get('/check-assessment/:username', (req, res) => {
const { username } = req.params;
const allResults = readJsonFile(assessmentFilePath);
const hasTaken = allResults.some(result =>
result.username === username || result.userName === username
);
res.json({ hasTakenAssessment: hasTaken });
});
// New endpoint to get college recommendations based on assessment
app.get('/get-recommendations/:username', (req, res) => {
const { username } = req.params;
try {
// Get user's assessment results
const allResults = readJsonFile(assessmentFilePath);
const userResult = allResults.find(result =>
result.username === username || result.userName === username
);
if (!userResult) {
return res.status(404).json({ error: 'Assessment results not found.' });
}
// Get colleges data
const collegesData = readJsonFile(collegesFilePath, []);
// Match specialized fields to college types
const specializedFields = userResult.specializedFields || [];
const matchedTypes = matchFieldsToCollegeTypes(specializedFields);
// Find colleges matching the types
let recommendedColleges = collegesData.filter(college => {
return matchedTypes.some(type => college.type.includes(type));
});
// If no specific matches, include general universities and engineering colleges
if (recommendedColleges.length === 0) {
recommendedColleges = collegesData.filter(college =>
college.type.includes('University') ||
college.type.includes('Engineering') ||
college.type.includes('Science')
);
}
// Load detailed data for each recommended college
const detailedColleges = [];
recommendedColleges.forEach(college => {
const fileName = getCollegeFileName(college.link);
if (fileName) {
const detailedDataPath = `./colleges/${fileName}.json`;
const detailedData = readJsonFile(detailedDataPath, null);
if (detailedData && detailedData.length > 0) {
// Merge basic info with detailed info
const mergedData = {
...detailedData[0], // Detailed data from individual file
basicInfo: college, // Basic info from colleges.json
nirf: college.nirf,
naac: college.naac,
placement: college.placement,
review: college.review,
location: college.location,
gov_pri: college.gov_pri,
exam: college.exam
};
detailedColleges.push(mergedData);
}
}
});
res.json({
userInterests: specializedFields,
matchedTypes,
recommendedColleges: detailedColleges.slice(0, 10), // Limit to top 10
stream: userResult.stream,
totalFound: detailedColleges.length
});
} catch (error) {
console.error('Error getting recommendations:', error);
res.status(500).json({ error: 'Failed to get recommendations.' });
}
});
app.post('/gemini-proxy', async (req, res) => {
const { prompt } = req.body;
if (!GEMINI_API_KEY) return res.status(500).json({ error: 'API key not configured.' });
if (!prompt) return res.status(400).json({ error: 'Prompt is required.' });
try {
const response = await axios.post(GEMINI_API_URL, { contents: [{ parts: [{ text: prompt }] }] });
const candidate = response.data.candidates[0];
if (!candidate.content || candidate.finishReason === 'SAFETY') {
return res.status(400).json({ error: 'AI response blocked for safety reasons.' });
}
res.json({ text: candidate.content.parts[0].text });
} catch (error) {
console.error('Error calling Gemini API:', error.response ? error.response.data : error.message);
res.status(500).json({ error: 'Failed to communicate with AI service.' });
}
});
// app.post('/signup', async (req, res) => {
// const { name, username, password } = req.body;
// if (!name || !username || !password) {
// return res.status(400).json({ error: 'Full name, username and password are required' });
// }
// let users = readJsonFile(usersFilePath);
// if (users.some(u => u.username === username)) {
// return res.status(400).json({ error: 'Username already exists' });
// }
// const hashedPassword = await bcrypt.hash(password, 10);
// users.push({ name, username, password: hashedPassword });
// writeJsonFile(usersFilePath, users);
// res.json({ status: 'success' });
// });
app.post('/signup', async (req, res) => {
const { name, username, password } = req.body;
if (!name || !username || !password) {
return res.status(400).json({ error: 'Full name, username and password are required' });
}
if (users.some(u => u.username === username)) {
return res.status(400).json({ error: 'Username already exists' });
}
const hashedPassword = await bcrypt.hash(password, 10);
users.push({ name, username, password: hashedPassword });
console.log("Users after signup:", users); // debug log
res.json({ status: 'success' });
});
app.post('/google-signup', (req, res) => {
const { name, email } = req.body;
let users = readJsonFile(usersFilePath);
if (users.some(u => u.email === email)) {
const existingUser = users.find(u => u.email === email);
return res.json({ status: 'success', isLogin: true, user: { name: existingUser.name, username: existingUser.username } });
}
const username = email;
const newUser = { name, email, username, isGoogle: true };
users.push(newUser);
writeJsonFile(usersFilePath, users);
res.json({ status: 'success', isLogin: false, user: { name: newUser.name, username: newUser.username } });
});
// app.post('/login', async (req, res) => {
// const { username, password } = req.body;
// let users = readJsonFile(usersFilePath);
// // Debug: log all users loaded
// console.log("Users in file:", users);
// // Debug: log the login attempt
// console.log("Login attempt:", { username, password });
// const user = users.find(u => u.username === username);
// // Debug: log whether the user was found
// console.log("Found user:", user);
// if (user && !user.isGoogle) {
// try {
// const isMatch = await bcrypt.compare(password, user.password);
// // Debug: log the password match result
// console.log("Password match result:", isMatch);
// if (isMatch) {
// res.json({ status: 'success', user: { name: user.name, username: user.username } });
// } else {
// console.error("Password mismatch for user:", username);
// res.status(401).json({ error: 'Invalid username or password.' });
// }
// } catch (err) {
// console.error("Error comparing password:", err);
// res.status(500).json({ error: 'Server error during login.' });
// }
// } else {
// console.error("User not found or is a Google account:", username);
// res.status(401).json({ error: 'Invalid username or password.' });
// }
// });
app.post('/login', async (req, res) => {
const { username, password } = req.body;
const user = users.find(u => u.username === username);
if (user) {
const isMatch = await bcrypt.compare(password, user.password);
if (isMatch) {
return res.json({ status: 'success', user: { name: user.name, username: user.username } });
}
}
res.status(401).json({ error: 'Invalid username or password.' });
});
app.post('/google-login', (req, res) => {
const { email } = req.body;
let users = readJsonFile(usersFilePath);
const user = users.find(u => u.email === email);
if (user && user.isGoogle) {
res.json({ status: 'success', user: { name: user.name, username: user.username } });
} else {
res.status(401).json({ error: 'No account found for this Google email. Please sign up first.' });
}
});
app.post('/save-assessment', (req, res) => {
const assessmentData = req.body;
const assessmentResults = readJsonFile(assessmentFilePath, []);
// Look for existing assessment using both username and userName
const existingIndex = assessmentResults.findIndex(result =>
result.username === assessmentData.username ||
result.userName === assessmentData.userName
);
if (existingIndex !== -1) {
assessmentResults[existingIndex] = assessmentData;
} else {
assessmentResults.push(assessmentData);
}
writeJsonFile(assessmentFilePath, assessmentResults);
res.json({ status: 'success' });
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});