Update server.js
Browse files
server.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
|
|
| 1 |
// @ts-nocheck
|
| 2 |
import express from 'express';
|
| 3 |
import cors from 'cors';
|
| 4 |
import multer from 'multer';
|
| 5 |
-
import {
|
| 6 |
import path from 'path';
|
| 7 |
import { fileURLToPath } from 'url';
|
| 8 |
-
|
| 9 |
-
dotenv.config()
|
| 10 |
// Fix __dirname for ES Modules
|
| 11 |
const __filename = fileURLToPath(import.meta.url);
|
| 12 |
const __dirname = path.dirname(__filename);
|
|
@@ -19,12 +19,12 @@ const port = process.env.PORT || 7860;
|
|
| 19 |
app.use(cors());
|
| 20 |
app.use(express.json());
|
| 21 |
|
| 22 |
-
// Configure Multer for memory storage
|
|
|
|
| 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',
|
|
@@ -33,24 +33,40 @@ const SERVER_CONFIG = {
|
|
| 33 |
|
| 34 |
// --- API ROUTES ---
|
| 35 |
|
| 36 |
-
// Upload Endpoint
|
| 37 |
-
|
|
|
|
| 38 |
try {
|
| 39 |
-
const
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
if (
|
| 43 |
-
return res.status(400).json({ error: '
|
| 44 |
}
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
| 48 |
}
|
| 49 |
|
| 50 |
-
console.log(`[SERVER]
|
| 51 |
|
| 52 |
-
//
|
| 53 |
-
const
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
credentials: {
|
| 55 |
accessToken: SERVER_CONFIG.TOKEN,
|
| 56 |
},
|
|
@@ -58,25 +74,26 @@ app.post('/api/upload', upload.single('file'), async (req, res) => {
|
|
| 58 |
type: SERVER_CONFIG.TYPE,
|
| 59 |
name: SERVER_CONFIG.REPO
|
| 60 |
},
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
content: new Blob([file.buffer]),
|
| 64 |
-
},
|
| 65 |
});
|
| 66 |
|
| 67 |
-
const commitHash = response.commit
|
| 68 |
const urlPrefix = "https://huggingface.co/datasets";
|
| 69 |
-
|
|
|
|
|
|
|
| 70 |
|
| 71 |
-
console.log(`[SERVER] Success: ${
|
| 72 |
|
| 73 |
res.json({
|
| 74 |
success: true,
|
| 75 |
-
|
|
|
|
| 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'
|
|
@@ -86,10 +103,8 @@ app.post('/api/upload', upload.single('file'), async (req, res) => {
|
|
| 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 |
});
|
|
@@ -100,4 +115,4 @@ app.listen(port, () => {
|
|
| 100 |
console.log(`✅ Server running on port ${port}`);
|
| 101 |
console.log(` Target Repo: ${SERVER_CONFIG.REPO}`);
|
| 102 |
console.log(`--------------------------------------------------`);
|
| 103 |
-
});
|
|
|
|
| 1 |
+
|
| 2 |
// @ts-nocheck
|
| 3 |
import express from 'express';
|
| 4 |
import cors from 'cors';
|
| 5 |
import multer from 'multer';
|
| 6 |
+
import { commit } from '@huggingface/hub'; // Use 'commit' for batch operations
|
| 7 |
import path from 'path';
|
| 8 |
import { fileURLToPath } from 'url';
|
| 9 |
+
|
|
|
|
| 10 |
// Fix __dirname for ES Modules
|
| 11 |
const __filename = fileURLToPath(import.meta.url);
|
| 12 |
const __dirname = path.dirname(__filename);
|
|
|
|
| 19 |
app.use(cors());
|
| 20 |
app.use(express.json());
|
| 21 |
|
| 22 |
+
// Configure Multer for memory storage
|
| 23 |
+
// No limit on file count here, but we limit payload size via express/nginx usually
|
| 24 |
const storage = multer.memoryStorage();
|
| 25 |
const upload = multer({ storage: storage });
|
| 26 |
|
| 27 |
// SERVER CONFIGURATION
|
|
|
|
| 28 |
const SERVER_CONFIG = {
|
| 29 |
TOKEN: process.env.HF_TOKEN || '',
|
| 30 |
REPO: process.env.HF_REPO || 'TwanAPI/DataTwan',
|
|
|
|
| 33 |
|
| 34 |
// --- API ROUTES ---
|
| 35 |
|
| 36 |
+
// Batch Upload Endpoint
|
| 37 |
+
// Accepts multiple files in the 'files' field
|
| 38 |
+
app.post('/api/upload', upload.array('files'), async (req, res) => {
|
| 39 |
try {
|
| 40 |
+
const files = req.files;
|
| 41 |
+
// req.body.paths can be a string (if 1 file) or array (if multiple)
|
| 42 |
+
// We normalize it to an array
|
| 43 |
+
const paths = [].concat(req.body.paths || []);
|
| 44 |
+
|
| 45 |
+
if (!files || files.length === 0) {
|
| 46 |
+
return res.status(400).json({ error: 'No files provided' });
|
| 47 |
+
}
|
| 48 |
|
| 49 |
+
if (files.length !== paths.length) {
|
| 50 |
+
return res.status(400).json({ error: 'Mismatch between file count and path count' });
|
| 51 |
}
|
| 52 |
|
| 53 |
+
// Security Check
|
| 54 |
+
if (!SERVER_CONFIG.TOKEN) {
|
| 55 |
+
console.error('[SERVER] Upload Failed: HF_TOKEN missing');
|
| 56 |
+
return res.status(500).json({ error: 'Server misconfiguration: HF_TOKEN secret is missing.' });
|
| 57 |
}
|
| 58 |
|
| 59 |
+
console.log(`[SERVER] Processing batch of ${files.length} files...`);
|
| 60 |
|
| 61 |
+
// Prepare operations for Hugging Face 'commit'
|
| 62 |
+
const operations = files.map((file, index) => ({
|
| 63 |
+
operation: 'addOrUpdate',
|
| 64 |
+
path: paths[index],
|
| 65 |
+
content: new Blob([file.buffer])
|
| 66 |
+
}));
|
| 67 |
+
|
| 68 |
+
// Execute single commit for all files
|
| 69 |
+
const response = await commit({
|
| 70 |
credentials: {
|
| 71 |
accessToken: SERVER_CONFIG.TOKEN,
|
| 72 |
},
|
|
|
|
| 74 |
type: SERVER_CONFIG.TYPE,
|
| 75 |
name: SERVER_CONFIG.REPO
|
| 76 |
},
|
| 77 |
+
operations: operations,
|
| 78 |
+
title: `Upload batch of ${files.length} files via DataTwan`
|
|
|
|
|
|
|
| 79 |
});
|
| 80 |
|
| 81 |
+
const commitHash = response.oid; // 'commit' returns { oid: string, ... }
|
| 82 |
const urlPrefix = "https://huggingface.co/datasets";
|
| 83 |
+
|
| 84 |
+
// Generate public URLs for response
|
| 85 |
+
const urls = paths.map(p => `${urlPrefix}/${SERVER_CONFIG.REPO}/blob/${commitHash}/${p}`);
|
| 86 |
|
| 87 |
+
console.log(`[SERVER] Batch Success: ${files.length} files committed (Hash: ${commitHash})`);
|
| 88 |
|
| 89 |
res.json({
|
| 90 |
success: true,
|
| 91 |
+
count: files.length,
|
| 92 |
+
urls: urls
|
| 93 |
});
|
| 94 |
|
| 95 |
} catch (error) {
|
| 96 |
+
console.error('[SERVER] Batch Upload Error:', error);
|
| 97 |
res.status(500).json({
|
| 98 |
success: false,
|
| 99 |
error: error.message || 'Internal Server Error'
|
|
|
|
| 103 |
|
| 104 |
// --- SERVE FRONTEND (Production) ---
|
| 105 |
|
|
|
|
| 106 |
app.use(express.static(path.join(__dirname, 'dist')));
|
| 107 |
|
|
|
|
| 108 |
app.get('*', (req, res) => {
|
| 109 |
res.sendFile(path.join(__dirname, 'dist', 'index.html'));
|
| 110 |
});
|
|
|
|
| 115 |
console.log(`✅ Server running on port ${port}`);
|
| 116 |
console.log(` Target Repo: ${SERVER_CONFIG.REPO}`);
|
| 117 |
console.log(`--------------------------------------------------`);
|
| 118 |
+
});
|