Spaces:
Sleeping
Sleeping
Update server.js
Browse files
server.js
CHANGED
|
@@ -6,45 +6,45 @@ const cors = require('cors');
|
|
| 6 |
const app = express();
|
| 7 |
const PORT = process.env.PORT || 7860;
|
| 8 |
|
| 9 |
-
// Enable CORS for all routes (useful if frontend is served separately)
|
| 10 |
app.use(cors());
|
| 11 |
-
|
| 12 |
-
// Increase payload limit for base64 image data (up to 10 MB)
|
| 13 |
app.use(express.json({ limit: '10mb' }));
|
| 14 |
|
| 15 |
-
// Define the snapshots directory (relative to this file)
|
| 16 |
const SNAPSHOT_DIR = path.join(__dirname, 'snapshots');
|
| 17 |
|
| 18 |
-
//
|
| 19 |
if (!fs.existsSync(SNAPSHOT_DIR)) {
|
| 20 |
fs.mkdirSync(SNAPSHOT_DIR, { recursive: true });
|
| 21 |
console.log(`[SERVER] Created snapshots directory at ${SNAPSHOT_DIR}`);
|
| 22 |
}
|
| 23 |
|
| 24 |
-
// Serve static files from
|
| 25 |
app.use(express.static(path.join(__dirname, 'public')));
|
| 26 |
|
| 27 |
-
//
|
| 28 |
-
app.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
// POST endpoint
|
| 31 |
app.post('/api/snapshot', (req, res) => {
|
| 32 |
try {
|
| 33 |
-
const { image } = req.body;
|
| 34 |
-
|
| 35 |
if (!image) {
|
| 36 |
-
return res.status(400).json({ success: false, error: 'No image data
|
| 37 |
}
|
| 38 |
|
| 39 |
-
// Validate base64 format (basic check)
|
| 40 |
-
if (typeof image !== 'string' || image.length < 100) {
|
| 41 |
-
return res.status(400).json({ success: false, error: 'Invalid image data format' });
|
| 42 |
-
}
|
| 43 |
-
|
| 44 |
-
// Convert base64 to binary buffer
|
| 45 |
const buffer = Buffer.from(image, 'base64');
|
| 46 |
-
|
| 47 |
-
// Generate timestamp-based filename
|
| 48 |
const now = new Date();
|
| 49 |
const dateStr = now.getFullYear() + '-' +
|
| 50 |
String(now.getMonth() + 1).padStart(2, '0') + '-' +
|
|
@@ -56,42 +56,27 @@ app.post('/api/snapshot', (req, res) => {
|
|
| 56 |
const filename = `snapshot_${dateStr}.png`;
|
| 57 |
const filePath = path.join(SNAPSHOT_DIR, filename);
|
| 58 |
|
| 59 |
-
// Write the file synchronously (can also use async with callback)
|
| 60 |
fs.writeFileSync(filePath, buffer);
|
|
|
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
filename: filename
|
| 69 |
-
});
|
| 70 |
-
|
| 71 |
-
} catch (err) {
|
| 72 |
-
console.error('[SERVER] Error saving snapshot:', err.message);
|
| 73 |
-
res.status(500).json({ success: false, error: 'Internal server error: ' + err.message });
|
| 74 |
-
}
|
| 75 |
-
});
|
| 76 |
|
| 77 |
-
|
| 78 |
-
app.get('/api/snapshots/list', (req, res) => {
|
| 79 |
-
try {
|
| 80 |
-
const files = fs.readdirSync(SNAPSHOT_DIR).filter(f => f.endsWith('.png'));
|
| 81 |
-
res.json({ success: true, files });
|
| 82 |
} catch (err) {
|
|
|
|
| 83 |
res.status(500).json({ success: false, error: err.message });
|
| 84 |
}
|
| 85 |
});
|
| 86 |
|
| 87 |
-
// Health check
|
| 88 |
-
app.get('/health', (req, res) =>
|
| 89 |
-
res.status(200).send('OK');
|
| 90 |
-
});
|
| 91 |
|
| 92 |
-
// Start the server
|
| 93 |
app.listen(PORT, () => {
|
| 94 |
-
console.log(`[SERVER]
|
| 95 |
-
console.log(`[SERVER]
|
| 96 |
-
console.log(`[SERVER] Snapshots will be saved to ${SNAPSHOT_DIR}`);
|
| 97 |
});
|
|
|
|
| 6 |
const app = express();
|
| 7 |
const PORT = process.env.PORT || 7860;
|
| 8 |
|
|
|
|
| 9 |
app.use(cors());
|
|
|
|
|
|
|
| 10 |
app.use(express.json({ limit: '10mb' }));
|
| 11 |
|
|
|
|
| 12 |
const SNAPSHOT_DIR = path.join(__dirname, 'snapshots');
|
| 13 |
|
| 14 |
+
// Ensure directory exists with proper permissions
|
| 15 |
if (!fs.existsSync(SNAPSHOT_DIR)) {
|
| 16 |
fs.mkdirSync(SNAPSHOT_DIR, { recursive: true });
|
| 17 |
console.log(`[SERVER] Created snapshots directory at ${SNAPSHOT_DIR}`);
|
| 18 |
}
|
| 19 |
|
| 20 |
+
// Serve static files from public
|
| 21 |
app.use(express.static(path.join(__dirname, 'public')));
|
| 22 |
|
| 23 |
+
// Explicit route for snapshots with logging
|
| 24 |
+
app.get('/snapshots/:filename', (req, res) => {
|
| 25 |
+
const filename = req.params.filename;
|
| 26 |
+
if (filename.includes('..') || filename.includes('/')) {
|
| 27 |
+
return res.status(400).send('Invalid filename');
|
| 28 |
+
}
|
| 29 |
+
const filePath = path.join(SNAPSHOT_DIR, filename);
|
| 30 |
+
if (fs.existsSync(filePath)) {
|
| 31 |
+
console.log(`[SERVER] Serving snapshot: ${filename}`);
|
| 32 |
+
res.sendFile(filePath);
|
| 33 |
+
} else {
|
| 34 |
+
console.log(`[SERVER] Snapshot not found: ${filename}`);
|
| 35 |
+
res.status(404).send('Snapshot not found');
|
| 36 |
+
}
|
| 37 |
+
});
|
| 38 |
|
| 39 |
+
// POST endpoint with enhanced logging
|
| 40 |
app.post('/api/snapshot', (req, res) => {
|
| 41 |
try {
|
| 42 |
+
const { image } = req.body;
|
|
|
|
| 43 |
if (!image) {
|
| 44 |
+
return res.status(400).json({ success: false, error: 'No image data' });
|
| 45 |
}
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
const buffer = Buffer.from(image, 'base64');
|
|
|
|
|
|
|
| 48 |
const now = new Date();
|
| 49 |
const dateStr = now.getFullYear() + '-' +
|
| 50 |
String(now.getMonth() + 1).padStart(2, '0') + '-' +
|
|
|
|
| 56 |
const filename = `snapshot_${dateStr}.png`;
|
| 57 |
const filePath = path.join(SNAPSHOT_DIR, filename);
|
| 58 |
|
|
|
|
| 59 |
fs.writeFileSync(filePath, buffer);
|
| 60 |
+
console.log(`[SERVER] Snapshot saved: ${filename} (${buffer.length} bytes) at ${filePath}`);
|
| 61 |
|
| 62 |
+
// Verify write
|
| 63 |
+
if (fs.existsSync(filePath)) {
|
| 64 |
+
console.log(`[SERVER] Verification: file exists, size ${fs.statSync(filePath).size}`);
|
| 65 |
+
} else {
|
| 66 |
+
console.error(`[SERVER] Verification FAILED: file not found after write`);
|
| 67 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
+
res.json({ success: true, path: `/snapshots/${filename}`, filename });
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
} catch (err) {
|
| 71 |
+
console.error('[SERVER] Save error:', err);
|
| 72 |
res.status(500).json({ success: false, error: err.message });
|
| 73 |
}
|
| 74 |
});
|
| 75 |
|
| 76 |
+
// Health check
|
| 77 |
+
app.get('/health', (req, res) => res.send('OK'));
|
|
|
|
|
|
|
| 78 |
|
|
|
|
| 79 |
app.listen(PORT, () => {
|
| 80 |
+
console.log(`[SERVER] Running on port ${PORT}`);
|
| 81 |
+
console.log(`[SERVER] Snapshots directory: ${SNAPSHOT_DIR}`);
|
|
|
|
| 82 |
});
|