Spaces:
Running
Running
Update temp.js
Browse files
temp.js
CHANGED
@@ -1,63 +1,65 @@
|
|
1 |
const express = require('express');
|
2 |
const { v4: uuidv4 } = require('uuid');
|
3 |
const MemoryFS = require('memory-fs');
|
|
|
4 |
const fs = require('fs');
|
5 |
|
|
|
6 |
const app = express();
|
7 |
const memFs = new MemoryFS();
|
|
|
|
|
|
|
8 |
|
9 |
-
const port = process.env.PORT || 7860;
|
10 |
|
11 |
app.put('/:filename', (req, res) => {
|
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 |
-
res.status(500).send('Error uploading file.');
|
46 |
-
});
|
47 |
});
|
48 |
|
49 |
app.get('/:id/:filename', (req, res) => {
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
});
|
60 |
|
61 |
-
|
62 |
-
|
63 |
});
|
|
|
1 |
const express = require('express');
|
2 |
const { v4: uuidv4 } = require('uuid');
|
3 |
const MemoryFS = require('memory-fs');
|
4 |
+
const http = require('http'); // Import http
|
5 |
const fs = require('fs');
|
6 |
|
7 |
+
|
8 |
const app = express();
|
9 |
const memFs = new MemoryFS();
|
10 |
+
const port = process.env.PORT || 7860; // Ambil port dari env atau default
|
11 |
+
|
12 |
+
const server = http.createServer(app);
|
13 |
|
|
|
14 |
|
15 |
app.put('/:filename', (req, res) => {
|
16 |
+
const shortId = uuidv4().slice(0, 5); // Menghasilkan ID pendek
|
17 |
+
const filename = req.params.filename;
|
18 |
+
const filepath = `/${shortId}-${filename}`;
|
19 |
+
const chunks = [];
|
20 |
+
|
21 |
+
req.on('data', chunk => {
|
22 |
+
chunks.push(chunk);
|
23 |
+
});
|
24 |
+
|
25 |
+
req.on('end', () => {
|
26 |
+
const fileBuffer = Buffer.concat(chunks);
|
27 |
+
memFs.writeFileSync(filepath, fileBuffer);
|
28 |
+
|
29 |
+
const protocol = req.protocol; // Mengambil protokol dari request (http atau https)
|
30 |
+
let host = req.get('host'); // Mengambil host dari request
|
31 |
+
let filePort = protocol === 'https' ? 443 : 80; // Default port untuk http dan https
|
32 |
+
if (host.includes(':')) {
|
33 |
+
[host, filePort] = host.split(':');
|
34 |
+
}
|
35 |
+
|
36 |
+
const fileUrl = `${protocol}://${host}:${filePort}/${shortId}/${filename}`;
|
37 |
+
res.send(`Uploaded 1 file, ${fileBuffer.length} bytes\n\nwget ${fileUrl}\n`);
|
38 |
+
|
39 |
+
// Hapus file setelah 24 jam
|
40 |
+
setTimeout(() => {
|
41 |
+
memFs.unlinkSync(filepath);
|
42 |
+
}, 24 * 60 * 60 * 1000); // 24 jam dalam milidetik
|
43 |
+
});
|
44 |
+
|
45 |
+
req.on('error', (err) => {
|
46 |
+
console.error(`Error receiving file: ${err}`);
|
47 |
+
res.status(500).send('Error uploading file.');
|
48 |
+
});
|
|
|
|
|
49 |
});
|
50 |
|
51 |
app.get('/:id/:filename', (req, res) => {
|
52 |
+
const filepath = `/${req.params.id}-${req.params.filename}`;
|
53 |
+
try {
|
54 |
+
const fileBuffer = memFs.readFileSync(filepath);
|
55 |
+
res.setHeader('Content-Disposition', `attachment; filename="${req.params.filename}"`);
|
56 |
+
res.send(fileBuffer);
|
57 |
+
} catch (err) {
|
58 |
+
console.error(`Error downloading file: ${err}`);
|
59 |
+
res.status(404).send('File not found.');
|
60 |
+
}
|
61 |
});
|
62 |
|
63 |
+
server.listen(port, () => {
|
64 |
+
console.log(`Server is running on port: ${port}`);
|
65 |
});
|