const express = require('express'); const { v4: uuidv4 } = require('uuid'); const fs = require('fs'); const path = require('path'); const app = express(); const storageDir = path.join(__dirname, 'uploads'); // Ensure the storage directory exists if (!fs.existsSync(storageDir)) { fs.mkdirSync(storageDir); } app.put('/:filename', (req, res) => { const shortId = uuidv4().slice(0, 5); // Generate a short ID const filename = req.params.filename; const filepath = path.join(storageDir, `${shortId}-${filename}`); const chunks = []; req.on('data', chunk => { chunks.push(chunk); }); req.on('end', () => { const fileBuffer = Buffer.concat(chunks); fs.writeFile(filepath, fileBuffer, (err) => { if (err) { console.error(`Error writing file: ${err}`); return res.status(500).send('Error uploading file.'); } const fileUrl = `https://zhofang-temp-storage.hf.space/${shortId}/${filename}`; res.send(`Uploaded 1 file, ${fileBuffer.length} bytes\n\nwget ${fileUrl}\n`); // Delete the 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 }); }); req.on('error', (err) => { console.error(`Error receiving file: ${err}`); res.status(500).send('Error uploading file.'); }); }); app.get('/:id/:filename', (req, res) => { const filepath = path.join(storageDir, `${req.params.id}-${req.params.filename}`); fs.readFile(filepath, (err, fileBuffer) => { if (err) { console.error(`Error downloading file: ${err}`); return res.status(404).send('File not found.'); } res.setHeader('Content-Disposition', `attachment; filename="${req.params.filename}"`); res.send(fileBuffer); }); }); app.listen(7860, () => { console.log('Server is running on port 7860'); });