Zhofang commited on
Commit
70e4b51
·
verified ·
1 Parent(s): 5755e8b

Update temp.js

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