const express = require('express'); const fs = require('fs'); const path = require('path'); const multer = require('multer'); const app = express(); const uploadDir = path.join(__dirname, 'uploads'); // Ensure uploads directory exists if (!fs.existsSync(uploadDir)) { fs.mkdirSync(uploadDir); } // Function to generate a short random ID (alphanumeric) function generateShortId() { return Math.random().toString(36).substring(2, 7); // Generates a random string of length 5 } // Configure multer for file storage const storage = multer.diskStorage({ destination: function (req, file, cb) { cb(null, uploadDir); }, filename: function (req, file, cb) { const shortId = generateShortId(); cb(null, `${shortId}-${file.originalname}`); } }); const upload = multer({ storage: storage }); // Route for browser upload app.get('/', (req, res) => { res.send(` Enhanced File Upload

Upload File

`); }); // Handler for browser upload app.post('/upload', upload.single('file'), (req, res) => { if (!req.file) { return res.status(400).send('No file uploaded.'); } const fileUrl = `https://zhofang-temp-storage.hf.space/${req.file.filename.split('-')[0]}/${req.file.originalname}`; res.send(` File Uploaded

File Uploaded Successfully

Uploaded 1 file, ${req.file.size} bytes

Download link: ${fileUrl}

wget command: wget ${fileUrl}

`); // Delete file after 24 hours setTimeout(() => { fs.unlink(req.file.path, (err) => { if (err) console.error(`Error deleting file: ${err}`); }); }, 24 * 60 * 60 * 1000); // 24 hours in milliseconds }); // Route for upload via PUT (like bashupload) app.put('/:filename', (req, res) => { const shortId = generateShortId(); const filename = req.params.filename; const filepath = path.join(uploadDir, `${shortId}-${filename}`); const fileStream = fs.createWriteStream(filepath); req.pipe(fileStream); fileStream.on('finish', () => { const fileUrl = `https://zhofang-temp-storage.hf.space/${shortId}/${filename}`; res.send(`Uploaded 1 file, ${req.headers['content-length']} bytes\n\nwget ${fileUrl}\n`); // Delete file after 24 hours setTimeout(() => { fs.unlink(filepath, (err) => { if (err) console.error(`Error deleting file: ${err}`); }); }, 24 * 60 * 60 * 1000); // 24 hours in milliseconds }); fileStream.on('error', (err) => { console.error(`Error writing file: ${err}`); res.status(500).send('Error uploading file.'); }); }); app.get('/:id/:filename', (req, res) => { const filepath = path.join(uploadDir, `${req.params.id}-${req.params.filename}`); res.download(filepath, req.params.filename, (err) => { if (err) { console.error(`Error downloading file: ${err}`); res.status(404).send('File not found.'); } }); }); app.listen(7860, () => { console.log('Server is running on http://localhost:3000'); });