Zhofang commited on
Commit
2a727d3
·
verified ·
1 Parent(s): 40bed98

Update temp.js

Browse files
Files changed (1) hide show
  1. temp.js +63 -52
temp.js CHANGED
@@ -1,52 +1,63 @@
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
- app.put('/:filename', (req, res) => {
10
- const shortId = uuidv4().slice(0, 5); // Menghasilkan ID pendek
11
- const filename = req.params.filename;
12
- const filepath = `/${shortId}-${filename}`;
13
- const chunks = [];
14
-
15
- req.on('data', chunk => {
16
- chunks.push(chunk);
17
- });
18
-
19
- req.on('end', () => {
20
- const fileBuffer = Buffer.concat(chunks);
21
- memFs.writeFileSync(filepath, fileBuffer);
22
-
23
- const fileUrl = `https://zhofang-temp-storage.hf.space/${shortId}/${filename}`;
24
- res.send(`Uploaded 1 file, ${fileBuffer.length} bytes\n\nwget ${fileUrl}\n`);
25
-
26
- // Hapus file setelah 24 jam
27
- setTimeout(() => {
28
- memFs.unlinkSync(filepath);
29
- }, 24 * 60 * 60 * 1000); // 24 jam dalam milidetik
30
- });
31
-
32
- req.on('error', (err) => {
33
- console.error(`Error receiving file: ${err}`);
34
- res.status(500).send('Error uploading file.');
35
- });
36
- });
37
-
38
- app.get('/:id/:filename', (req, res) => {
39
- const filepath = `/${req.params.id}-${req.params.filename}`;
40
- try {
41
- const fileBuffer = memFs.readFileSync(filepath);
42
- res.setHeader('Content-Disposition', `attachment; filename="${req.params.filename}"`);
43
- res.send(fileBuffer);
44
- } catch (err) {
45
- console.error(`Error downloading file: ${err}`);
46
- res.status(404).send('File not found.');
47
- }
48
- });
49
-
50
- app.listen(7860, () => {
51
- console.log('Server is running on idk just guess it');
52
- });
 
 
 
 
 
 
 
 
 
 
 
 
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
+ const shortId = uuidv4().slice(0, 5); // Menghasilkan ID pendek
13
+ const filename = req.params.filename;
14
+ const filepath = `/${shortId}-${filename}`;
15
+ const chunks = [];
16
+
17
+ req.on('data', chunk => {
18
+ chunks.push(chunk);
19
+ });
20
+
21
+ req.on('end', () => {
22
+ const fileBuffer = Buffer.concat(chunks);
23
+ memFs.writeFileSync(filepath, fileBuffer);
24
+
25
+ const protocol = req.protocol;
26
+ let host = req.get('host');
27
+ let port = protocol === 'https' ? 443 : 80;
28
+
29
+ if (host.includes(':')) {
30
+ [host, port] = host.split(':');
31
+ }
32
+
33
+
34
+ const fileUrl = `${protocol}://${host}:${port}/${shortId}/${filename}`;
35
+ res.send(`Uploaded 1 file, ${fileBuffer.length} bytes\n\nwget ${fileUrl}\n`);
36
+
37
+ // Hapus file setelah 24 jam
38
+ setTimeout(() => {
39
+ memFs.unlinkSync(filepath);
40
+ }, 24 * 60 * 60 * 1000); // 24 jam dalam milidetik
41
+ });
42
+
43
+ req.on('error', (err) => {
44
+ console.error(`Error receiving file: ${err}`);
45
+ res.status(500).send('Error uploading file.');
46
+ });
47
+ });
48
+
49
+ app.get('/:id/:filename', (req, res) => {
50
+ const filepath = `/${req.params.id}-${req.params.filename}`;
51
+ try {
52
+ const fileBuffer = memFs.readFileSync(filepath);
53
+ res.setHeader('Content-Disposition', `attachment; filename="${req.params.filename}"`);
54
+ res.send(fileBuffer);
55
+ } catch (err) {
56
+ console.error(`Error downloading file: ${err}`);
57
+ res.status(404).send('File not found.');
58
+ }
59
+ });
60
+
61
+ app.listen(port, () => {
62
+ console.log(`Server is running on port: ${port}`);
63
+ });