Spaces:
Running
Running
Update app.js
Browse files
app.js
CHANGED
|
@@ -219,42 +219,96 @@ app.post('/feedback', async (req, res) => {
|
|
| 219 |
}
|
| 220 |
});
|
| 221 |
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
app.post('/feedback2', verifyFirebaseUser, async (req, res) => {
|
| 228 |
-
const { projectId, prompt, ...otherPayload } = req.body;
|
| 229 |
-
const userId = req.user.uid;
|
| 230 |
|
| 231 |
if (!projectId || !prompt) {
|
| 232 |
return res.status(400).json({ error: 'Missing projectId or prompt' });
|
| 233 |
}
|
| 234 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
const externalBase = process.env.EXTERNAL_SERVER_URL || 'http://localhost:7860';
|
| 236 |
const targetUrl = externalBase.replace(/\/$/, '') + '/project/feedback';
|
| 237 |
|
| 238 |
-
console.log(`📨 Forwarding DASHBOARD feedback for ${projectId} (User: ${userId})`);
|
| 239 |
-
|
| 240 |
try {
|
| 241 |
-
// We inject the authenticated userId here so the Core server knows
|
| 242 |
-
// it was verified by the Auth Proxy.
|
| 243 |
const response = await axios.post(targetUrl, {
|
| 244 |
userId: userId,
|
| 245 |
projectId: projectId,
|
| 246 |
prompt: prompt,
|
| 247 |
-
|
|
|
|
| 248 |
});
|
| 249 |
|
| 250 |
return res.json({ success: true, externalResponse: response.data });
|
| 251 |
-
|
| 252 |
} catch (err) {
|
| 253 |
-
console.error("
|
| 254 |
-
|
| 255 |
-
return res.status(err.response.status).json(err.response.data);
|
| 256 |
-
}
|
| 257 |
-
return res.status(502).json({ error: 'Failed to forward feedback to Main AI server' });
|
| 258 |
}
|
| 259 |
});
|
| 260 |
|
|
|
|
| 219 |
}
|
| 220 |
});
|
| 221 |
|
| 222 |
+
|
| 223 |
+
// ---------------------------------------------------------
|
| 224 |
+
// NEW: DELETE ENDPOINT
|
| 225 |
+
// ---------------------------------------------------------
|
| 226 |
+
app.post('/project/delete', verifyFirebaseUser, async (req, res) => {
|
| 227 |
+
const { projectId } = req.body;
|
| 228 |
+
const userId = req.user.uid;
|
| 229 |
+
|
| 230 |
+
if (!projectId) return res.status(400).json({ error: "Missing Project ID" });
|
| 231 |
+
|
| 232 |
+
console.log(`🗑️ Deleting Project: ${projectId} requested by ${userId}`);
|
| 233 |
+
|
| 234 |
+
try {
|
| 235 |
+
// 1. Verify Ownership (Check Firestore or RTDB)
|
| 236 |
+
const projectRef = db.ref(`projects/${projectId}/info`);
|
| 237 |
+
const snapshot = await projectRef.once('value');
|
| 238 |
+
if (snapshot.exists()) {
|
| 239 |
+
const data = snapshot.val();
|
| 240 |
+
if (data.userId !== userId) {
|
| 241 |
+
return res.status(403).json({ error: "Unauthorized" });
|
| 242 |
+
}
|
| 243 |
+
}
|
| 244 |
+
|
| 245 |
+
const promises = [];
|
| 246 |
+
|
| 247 |
+
// 2. Delete from Realtime Database
|
| 248 |
+
promises.push(db.ref(`projects/${projectId}`).remove());
|
| 249 |
+
promises.push(db.ref(`plugin_oauth/${userId}/${projectId}`).remove());
|
| 250 |
+
|
| 251 |
+
// 3. Delete from Firestore
|
| 252 |
+
if (firestore) {
|
| 253 |
+
promises.push(firestore.collection('projects').doc(projectId).delete());
|
| 254 |
+
}
|
| 255 |
+
|
| 256 |
+
// 4. Delete from Storage (Recursive)
|
| 257 |
+
if (storage) {
|
| 258 |
+
const bucket = storage.bucket();
|
| 259 |
+
// Delete all files with the projectId prefix
|
| 260 |
+
promises.push(bucket.deleteFiles({ prefix: `${projectId}/` }));
|
| 261 |
+
}
|
| 262 |
+
|
| 263 |
+
// 5. Clear from Memory
|
| 264 |
+
StateManager.cleanupProject(projectId); // You might need to add this method to StateManager or just map delete
|
| 265 |
+
// Direct map delete if StateManager logic isn't exposed
|
| 266 |
+
// activeSessions.delete(`${userId}:${projectId}`);
|
| 267 |
+
|
| 268 |
+
await Promise.all(promises);
|
| 269 |
+
|
| 270 |
+
console.log(`✅ Project ${projectId} deleted successfully.`);
|
| 271 |
+
res.json({ success: true });
|
| 272 |
+
|
| 273 |
+
} catch (err) {
|
| 274 |
+
console.error("Delete Error:", err);
|
| 275 |
+
res.status(500).json({ error: "Failed to delete project resources" });
|
| 276 |
+
}
|
| 277 |
+
});
|
| 278 |
+
|
| 279 |
+
|
| 280 |
+
// ---------------------------------------------------------
|
| 281 |
+
// UPDATED FEEDBACK ENDPOINT (Handling Images)
|
| 282 |
+
// ---------------------------------------------------------
|
| 283 |
app.post('/feedback2', verifyFirebaseUser, async (req, res) => {
|
| 284 |
+
const { projectId, prompt, images, ...otherPayload } = req.body; // Extract images
|
| 285 |
+
const userId = req.user.uid;
|
| 286 |
|
| 287 |
if (!projectId || !prompt) {
|
| 288 |
return res.status(400).json({ error: 'Missing projectId or prompt' });
|
| 289 |
}
|
| 290 |
|
| 291 |
+
// Log if image is received
|
| 292 |
+
if (images && images.length > 0) {
|
| 293 |
+
console.log(`📸 Received ${images.length} image(s) from Dashboard.`);
|
| 294 |
+
}
|
| 295 |
+
|
| 296 |
const externalBase = process.env.EXTERNAL_SERVER_URL || 'http://localhost:7860';
|
| 297 |
const targetUrl = externalBase.replace(/\/$/, '') + '/project/feedback';
|
| 298 |
|
|
|
|
|
|
|
| 299 |
try {
|
|
|
|
|
|
|
| 300 |
const response = await axios.post(targetUrl, {
|
| 301 |
userId: userId,
|
| 302 |
projectId: projectId,
|
| 303 |
prompt: prompt,
|
| 304 |
+
images: images || [], // Ensure it is passed to Core AI
|
| 305 |
+
...otherPayload
|
| 306 |
});
|
| 307 |
|
| 308 |
return res.json({ success: true, externalResponse: response.data });
|
|
|
|
| 309 |
} catch (err) {
|
| 310 |
+
console.error("Forward Error:", err.message);
|
| 311 |
+
return res.status(502).json({ error: 'Failed to forward' });
|
|
|
|
|
|
|
|
|
|
| 312 |
}
|
| 313 |
});
|
| 314 |
|