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

Update temp.js

Browse files
Files changed (1) hide show
  1. temp.js +52 -65
temp.js CHANGED
@@ -1,66 +1,53 @@
1
- const express = require('express');
2
- const { v4: uuidv4 } = require('uuid');
3
- const fs = require('fs');
4
- const path = require('path');
5
 
6
- const app = express();
7
- const storageDir = path.join(__dirname, 'uploads');
8
-
9
- // Ensure the storage directory exists
10
- if (!fs.existsSync(storageDir)) {
11
- fs.mkdirSync(storageDir);
12
- }
13
-
14
- app.put('/:filename', (req, res) => {
15
- const shortId = uuidv4().slice(0, 5); // Generate a short ID
16
- const filename = req.params.filename;
17
- const filepath = path.join(storageDir, `${shortId}-${filename}`);
18
- const chunks = [];
19
-
20
- req.on('data', chunk => {
21
- chunks.push(chunk);
22
- });
23
-
24
- req.on('end', () => {
25
- const fileBuffer = Buffer.concat(chunks);
26
-
27
- fs.writeFile(filepath, fileBuffer, (err) => {
28
- if (err) {
29
- console.error(`Error writing file: ${err}`);
30
- return res.status(500).send('Error uploading file.');
31
- }
32
-
33
- const fileUrl = `https://zhofang-temp-storage.hf.space/${shortId}/${filename}`;
34
- res.send(`Uploaded 1 file, ${fileBuffer.length} bytes\n\nwget ${fileUrl}\n`);
35
-
36
- // Delete the file after 24 hours
37
- setTimeout(() => {
38
- fs.unlink(filepath, (err) => {
39
- if (err) console.error(`Error deleting file: ${err}`);
40
- });
41
- }, 24 * 60 * 60 * 1000); // 24 hours in milliseconds
42
- });
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 = path.join(storageDir, `${req.params.id}-${req.params.filename}`);
53
- fs.readFile(filepath, (err, fileBuffer) => {
54
- if (err) {
55
- console.error(`Error downloading file: ${err}`);
56
- return res.status(404).send('File not found.');
57
- }
58
-
59
- res.setHeader('Content-Disposition', `attachment; filename="${req.params.filename}"`);
60
- res.send(fileBuffer);
61
- });
62
- });
63
-
64
- app.listen(7860, () => {
65
- console.log('Server is running on port 7860');
66
- });
 
 
 
 
 
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
+ });