Spaces:
Paused
Paused
Update temp.js
Browse files
temp.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
const express = require('express');
|
| 2 |
const fs = require('fs');
|
| 3 |
const path = require('path');
|
| 4 |
-
const { v6: uuidv6 } = require('uuid');
|
| 5 |
|
| 6 |
const app = express();
|
| 7 |
const uploadDir = path.join(__dirname, 'uploads');
|
|
@@ -11,8 +10,15 @@ if (!fs.existsSync(uploadDir)) {
|
|
| 11 |
fs.mkdirSync(uploadDir);
|
| 12 |
}
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
app.put('/:filename', (req, res) => {
|
| 15 |
-
const shortId =
|
| 16 |
const filename = req.params.filename;
|
| 17 |
const filepath = path.join(uploadDir, `${shortId}-${filename}`);
|
| 18 |
const fileStream = fs.createWriteStream(filepath);
|
|
@@ -23,18 +29,18 @@ app.put('/:filename', (req, res) => {
|
|
| 23 |
const fileUrl = `https://dd5957d9-d77c-438b-8270-1fbf38f27e54-00-2459iusmnsrys.riker.replit.dev/${shortId}/${filename}`;
|
| 24 |
res.send(`Uploaded 1 file, ${req.headers['content-length']} bytes\n\nwget ${fileUrl}\n`);
|
| 25 |
|
| 26 |
-
|
| 27 |
setTimeout(() => {
|
| 28 |
fs.unlink(filepath, (err) => {
|
| 29 |
-
|
| 30 |
});
|
| 31 |
}, 24 * 60 * 60 * 1000); // 24 jam dalam milidetik
|
| 32 |
-
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
res.status(500).send('Error uploading file.');
|
| 37 |
-
|
| 38 |
});
|
| 39 |
|
| 40 |
app.get('/:id/:filename', (req, res) => {
|
|
@@ -42,11 +48,12 @@ app.get('/:id/:filename', (req, res) => {
|
|
| 42 |
res.download(filepath, req.params.filename, (err) => {
|
| 43 |
if (err) {
|
| 44 |
console.error(`Error downloading file: ${err}`);
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
});
|
| 49 |
|
|
|
|
| 50 |
app.listen(3000, () => {
|
| 51 |
console.log('Server is running on http://localhost:3000');
|
| 52 |
-
});
|
|
|
|
| 1 |
const express = require('express');
|
| 2 |
const fs = require('fs');
|
| 3 |
const path = require('path');
|
|
|
|
| 4 |
|
| 5 |
const app = express();
|
| 6 |
const uploadDir = path.join(__dirname, 'uploads');
|
|
|
|
| 10 |
fs.mkdirSync(uploadDir);
|
| 11 |
}
|
| 12 |
|
| 13 |
+
function generateShortId() {
|
| 14 |
+
const timestamp = Date.now().toString(36);
|
| 15 |
+
const random = Math.random().toString(36).substring(2, 7);
|
| 16 |
+
return `${timestamp}-${random}`;
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
|
| 20 |
app.put('/:filename', (req, res) => {
|
| 21 |
+
const shortId = generateShortId();
|
| 22 |
const filename = req.params.filename;
|
| 23 |
const filepath = path.join(uploadDir, `${shortId}-${filename}`);
|
| 24 |
const fileStream = fs.createWriteStream(filepath);
|
|
|
|
| 29 |
const fileUrl = `https://dd5957d9-d77c-438b-8270-1fbf38f27e54-00-2459iusmnsrys.riker.replit.dev/${shortId}/${filename}`;
|
| 30 |
res.send(`Uploaded 1 file, ${req.headers['content-length']} bytes\n\nwget ${fileUrl}\n`);
|
| 31 |
|
| 32 |
+
// Hapus file setelah 24 jam
|
| 33 |
setTimeout(() => {
|
| 34 |
fs.unlink(filepath, (err) => {
|
| 35 |
+
if (err) console.error(`Error deleting file: ${err}`);
|
| 36 |
});
|
| 37 |
}, 24 * 60 * 60 * 1000); // 24 jam dalam milidetik
|
| 38 |
+
});
|
| 39 |
|
| 40 |
+
fileStream.on('error', (err) => {
|
| 41 |
+
console.error(`Error writing file: ${err}`);
|
| 42 |
res.status(500).send('Error uploading file.');
|
| 43 |
+
});
|
| 44 |
});
|
| 45 |
|
| 46 |
app.get('/:id/:filename', (req, res) => {
|
|
|
|
| 48 |
res.download(filepath, req.params.filename, (err) => {
|
| 49 |
if (err) {
|
| 50 |
console.error(`Error downloading file: ${err}`);
|
| 51 |
+
res.status(404).send('File not found.');
|
| 52 |
+
}
|
| 53 |
+
});
|
| 54 |
});
|
| 55 |
|
| 56 |
+
|
| 57 |
app.listen(3000, () => {
|
| 58 |
console.log('Server is running on http://localhost:3000');
|
| 59 |
+
});
|