Spaces:
Running
Running
Update temp.js
Browse files
temp.js
CHANGED
@@ -1,93 +1,48 @@
|
|
1 |
-
const express = require('express');
|
2 |
-
const { v4: uuidv4 } = require('uuid');
|
3 |
-
const fs = require('fs');
|
4 |
-
const path = require('path');
|
5 |
|
6 |
-
const
|
7 |
-
const
|
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 |
-
req.on('error', (err) => {
|
54 |
-
console.error(`Error receiving file: ${err}`);
|
55 |
-
res.status(500).send('Error uploading file.');
|
56 |
-
|
57 |
-
});
|
58 |
-
|
59 |
-
writeStream.on('error', (err) => {
|
60 |
-
console.error(`Error writing file: ${err}`);
|
61 |
-
res.status(500).send('Error uploading file.');
|
62 |
-
fs.rm(fileDir, { recursive: true, force: true }, (err) => {
|
63 |
-
if (err) {
|
64 |
-
console.error(`Error deleting file and folder after write error at ${fileDir}:`, err);
|
65 |
-
} else {
|
66 |
-
console.log(`File and folder at ${fileDir} deleted due to error`);
|
67 |
-
}
|
68 |
-
});
|
69 |
-
});
|
70 |
-
|
71 |
-
});
|
72 |
-
|
73 |
-
app.get('/:id/:filename', (req, res) => {
|
74 |
-
const fileDir = path.join(UPLOAD_DIR, req.params.id);
|
75 |
-
const filepath = path.join(fileDir, req.params.filename);
|
76 |
-
console.log(`Attempting to download file: ${filepath}`);
|
77 |
-
|
78 |
-
|
79 |
-
fs.readFile(filepath, (err, fileBuffer) => {
|
80 |
-
if (err) {
|
81 |
-
console.error(`Error downloading file at ${filepath}:`, err);
|
82 |
-
res.status(404).send('File not found.');
|
83 |
-
} else {
|
84 |
-
res.setHeader('Content-Disposition', `attachment; filename="${req.params.filename}"`);
|
85 |
-
res.send(fileBuffer);
|
86 |
-
}
|
87 |
-
});
|
88 |
-
|
89 |
-
});
|
90 |
-
|
91 |
-
app.listen(7860, () => {
|
92 |
-
console.log('Server is running on http://localhost:7860');
|
93 |
-
});
|
|
|
|
|
|
|
|
|
|
|
1 |
|
2 |
+
const express = require('express');
|
3 |
+
const { v4: uuidv4 } = require('uuid');
|
4 |
+
const path = require('path');
|
5 |
+
const fs = require('fs');
|
6 |
+
|
7 |
+
const app = express();
|
8 |
+
|
9 |
+
app.put('/:filename', (req, res) => {
|
10 |
+
const shortId = uuidv4().slice(0, 5);
|
11 |
+
const filename = req.params.filename;
|
12 |
+
const filepath = path.join(__dirname, `/uploads/${shortId}-${filename}`);
|
13 |
+
const fileStream = fs.createWriteStream(filepath);
|
14 |
+
|
15 |
+
req.pipe(fileStream);
|
16 |
+
|
17 |
+
req.on('end', () => {
|
18 |
+
const fileUrl = `http://localhost:7860/${shortId}/${filename}`;
|
19 |
+
res.send(`Uploaded file to ${fileUrl}`);
|
20 |
+
|
21 |
+
// Hapus file setelah 24 jam
|
22 |
+
setTimeout(() => {
|
23 |
+
fs.unlink(filepath, (err) => {
|
24 |
+
if (err) console.error(`Error deleting file: ${err}`);
|
25 |
+
});
|
26 |
+
}, 24 * 60 * 60 * 1000); // 24 jam dalam milidetik
|
27 |
+
});
|
28 |
+
|
29 |
+
req.on('error', (err) => {
|
30 |
+
console.error(`Error receiving file: ${err}`);
|
31 |
+
res.status(500).send('Error uploading file.');
|
32 |
+
});
|
33 |
+
});
|
34 |
+
|
35 |
+
app.get('/:id/:filename', (req, res) => {
|
36 |
+
const filepath = path.join(__dirname, `/uploads/${req.params.id}-${req.params.filename}`);
|
37 |
+
res.download(filepath, req.params.filename, (err) => {
|
38 |
+
if (err) {
|
39 |
+
console.error(`Error downloading file: ${err}`);
|
40 |
+
res.status(404).send('File not found.');
|
41 |
+
}
|
42 |
+
});
|
43 |
+
});
|
44 |
+
|
45 |
+
app.listen(7860, () => {
|
46 |
+
console.log('Server is running on port 7860');
|
47 |
+
});
|
48 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|