Create server.js
Browse files
server.js
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// @ts-nocheck
|
| 2 |
+
import express from 'express';
|
| 3 |
+
import cors from 'cors';
|
| 4 |
+
import multer from 'multer';
|
| 5 |
+
import { uploadFile } from '@huggingface/hub';
|
| 6 |
+
import path from 'path';
|
| 7 |
+
import { fileURLToPath } from 'url';
|
| 8 |
+
import dotenv from 'dotenv';
|
| 9 |
+
dotenv.config()
|
| 10 |
+
// Fix __dirname for ES Modules
|
| 11 |
+
const __filename = fileURLToPath(import.meta.url);
|
| 12 |
+
const __dirname = path.dirname(__filename);
|
| 13 |
+
|
| 14 |
+
const app = express();
|
| 15 |
+
// Hugging Face Spaces requires port 7860
|
| 16 |
+
const port = process.env.PORT || 7860;
|
| 17 |
+
|
| 18 |
+
// Middleware
|
| 19 |
+
app.use(cors());
|
| 20 |
+
app.use(express.json());
|
| 21 |
+
|
| 22 |
+
// Configure Multer for memory storage (file buffer)
|
| 23 |
+
const storage = multer.memoryStorage();
|
| 24 |
+
const upload = multer({ storage: storage });
|
| 25 |
+
|
| 26 |
+
// SERVER CONFIGURATION
|
| 27 |
+
// PRIORITY: Environment Variable > Hardcoded Fallback
|
| 28 |
+
const SERVER_CONFIG = {
|
| 29 |
+
TOKEN: process.env.HF_TOKEN || '',
|
| 30 |
+
REPO: process.env.HF_REPO || 'TwanAPI/DataTwan',
|
| 31 |
+
TYPE: 'dataset'
|
| 32 |
+
};
|
| 33 |
+
|
| 34 |
+
// --- API ROUTES ---
|
| 35 |
+
|
| 36 |
+
// Upload Endpoint
|
| 37 |
+
app.post('/api/upload', upload.single('file'), async (req, res) => {
|
| 38 |
+
try {
|
| 39 |
+
const file = req.file;
|
| 40 |
+
const filePath = req.body.path;
|
| 41 |
+
|
| 42 |
+
if (!file) {
|
| 43 |
+
return res.status(400).json({ error: 'No file provided' });
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
if (!filePath) {
|
| 47 |
+
return res.status(400).json({ error: 'No file path provided' });
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
console.log(`[SERVER] Uploading: ${filePath} (${file.size} bytes)`);
|
| 51 |
+
|
| 52 |
+
// Upload to Hugging Face
|
| 53 |
+
const response = await uploadFile({
|
| 54 |
+
credentials: {
|
| 55 |
+
accessToken: SERVER_CONFIG.TOKEN,
|
| 56 |
+
},
|
| 57 |
+
repo: {
|
| 58 |
+
type: SERVER_CONFIG.TYPE,
|
| 59 |
+
name: SERVER_CONFIG.REPO
|
| 60 |
+
},
|
| 61 |
+
file: {
|
| 62 |
+
path: filePath,
|
| 63 |
+
content: new Blob([file.buffer]),
|
| 64 |
+
},
|
| 65 |
+
});
|
| 66 |
+
|
| 67 |
+
const commitHash = response.commit.oid;
|
| 68 |
+
const urlPrefix = "https://huggingface.co/datasets";
|
| 69 |
+
const publicUrl = `${urlPrefix}/${SERVER_CONFIG.REPO}/blob/${commitHash}/${filePath}`;
|
| 70 |
+
|
| 71 |
+
console.log(`[SERVER] Success: ${publicUrl}`);
|
| 72 |
+
|
| 73 |
+
res.json({
|
| 74 |
+
success: true,
|
| 75 |
+
url: publicUrl
|
| 76 |
+
});
|
| 77 |
+
|
| 78 |
+
} catch (error) {
|
| 79 |
+
console.error('[SERVER] Upload Error:', error);
|
| 80 |
+
res.status(500).json({
|
| 81 |
+
success: false,
|
| 82 |
+
error: error.message || 'Internal Server Error'
|
| 83 |
+
});
|
| 84 |
+
}
|
| 85 |
+
});
|
| 86 |
+
|
| 87 |
+
// --- SERVE FRONTEND (Production) ---
|
| 88 |
+
|
| 89 |
+
// Serve static files from the React build directory
|
| 90 |
+
app.use(express.static(path.join(__dirname, 'dist')));
|
| 91 |
+
|
| 92 |
+
// Handle React Routing (SPA) - Return index.html for any unknown route
|
| 93 |
+
app.get('*', (req, res) => {
|
| 94 |
+
res.sendFile(path.join(__dirname, 'dist', 'index.html'));
|
| 95 |
+
});
|
| 96 |
+
|
| 97 |
+
// Start Server
|
| 98 |
+
app.listen(port, () => {
|
| 99 |
+
console.log(`--------------------------------------------------`);
|
| 100 |
+
console.log(`✅ Server running on port ${port}`);
|
| 101 |
+
console.log(` Target Repo: ${SERVER_CONFIG.REPO}`);
|
| 102 |
+
console.log(`--------------------------------------------------`);
|
| 103 |
+
});
|