Spaces:
Sleeping
Sleeping
Upload deepseek_javascript_20260623_6d713d.js
Browse files
deepseek_javascript_20260623_6d713d.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const express = require('express');
|
| 2 |
+
const fs = require('fs');
|
| 3 |
+
const path = require('path');
|
| 4 |
+
const cors = require('cors');
|
| 5 |
+
|
| 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 |
+
// Create snapshots folder if it doesn't exist
|
| 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 the "public" folder at the root URL
|
| 25 |
+
app.use(express.static(path.join(__dirname, 'public')));
|
| 26 |
+
|
| 27 |
+
// Serve the snapshots folder as a static endpoint (so users can view saved images)
|
| 28 |
+
app.use('/snapshots', express.static(SNAPSHOT_DIR));
|
| 29 |
+
|
| 30 |
+
// POST endpoint to save a snapshot
|
| 31 |
+
app.post('/api/snapshot', (req, res) => {
|
| 32 |
+
try {
|
| 33 |
+
const { image } = req.body; // expects base64 string (without the data:image/png;base64, prefix)
|
| 34 |
+
|
| 35 |
+
if (!image) {
|
| 36 |
+
return res.status(400).json({ success: false, error: 'No image data provided' });
|
| 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') + '-' +
|
| 51 |
+
String(now.getDate()).padStart(2, '0') + '_' +
|
| 52 |
+
String(now.getHours()).padStart(2, '0') + '-' +
|
| 53 |
+
String(now.getMinutes()).padStart(2, '0') + '-' +
|
| 54 |
+
String(now.getSeconds()).padStart(2, '0') + '_' +
|
| 55 |
+
String(now.getMilliseconds()).padStart(3, '0');
|
| 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 |
+
console.log(`[SERVER] Saved snapshot: ${filename} (${buffer.length} bytes)`);
|
| 63 |
+
|
| 64 |
+
// Return success response with the file path (relative URL)
|
| 65 |
+
res.json({
|
| 66 |
+
success: true,
|
| 67 |
+
path: `/snapshots/${filename}`,
|
| 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 |
+
// Optional: endpoint to list all saved snapshots (for debugging)
|
| 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 endpoint (useful for Hugging Face)
|
| 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] Gesture Drawing Space running on port ${PORT}`);
|
| 95 |
+
console.log(`[SERVER] Serving static files from ${path.join(__dirname, 'public')}`);
|
| 96 |
+
console.log(`[SERVER] Snapshots will be saved to ${SNAPSHOT_DIR}`);
|
| 97 |
+
});
|