Spaces:
Running
Running
Update temp.js
Browse files
temp.js
CHANGED
@@ -1,53 +1,52 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
});
|
|
|
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');
|
8 |
+
|
9 |
+
// Pastikan direktori uploads ada
|
10 |
+
if (!fs.existsSync(uploadDir)) {
|
11 |
+
fs.mkdirSync(uploadDir);
|
12 |
+
}
|
13 |
+
|
14 |
+
app.put('/:filename', (req, res) => {
|
15 |
+
const shortId = uuidv6().slice(0, 5); // Menghasilkan ID pendek
|
16 |
+
const filename = req.params.filename;
|
17 |
+
const filepath = path.join(uploadDir, `${shortId}-${filename}`);
|
18 |
+
const fileStream = fs.createWriteStream(filepath);
|
19 |
+
|
20 |
+
req.pipe(fileStream);
|
21 |
+
|
22 |
+
fileStream.on('finish', () => {
|
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 |
+
// Hapus file setelah 24 jam
|
27 |
+
setTimeout(() => {
|
28 |
+
fs.unlink(filepath, (err) => {
|
29 |
+
if (err) console.error(`Error deleting file: ${err}`);
|
30 |
+
});
|
31 |
+
}, 24 * 60 * 60 * 1000); // 24 jam dalam milidetik
|
32 |
+
});
|
33 |
+
|
34 |
+
fileStream.on('error', (err) => {
|
35 |
+
console.error(`Error writing file: ${err}`);
|
36 |
+
res.status(500).send('Error uploading file.');
|
37 |
+
});
|
38 |
+
});
|
39 |
+
|
40 |
+
app.get('/:id/:filename', (req, res) => {
|
41 |
+
const filepath = path.join(uploadDir, `${req.params.id}-${req.params.filename}`);
|
42 |
+
res.download(filepath, req.params.filename, (err) => {
|
43 |
+
if (err) {
|
44 |
+
console.error(`Error downloading file: ${err}`);
|
45 |
+
res.status(404).send('File not found.');
|
46 |
+
}
|
47 |
+
});
|
48 |
+
});
|
49 |
+
|
50 |
+
app.listen(3000, () => {
|
51 |
+
console.log('Server is running on http://localhost:3000');
|
52 |
+
});
|
|