Zhofang commited on
Commit
a263f12
·
verified ·
1 Parent(s): 1a0c0a2

Update temp.js

Browse files
Files changed (1) hide show
  1. temp.js +93 -52
temp.js CHANGED
@@ -1,52 +1,93 @@
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 fs = require('fs');
4
+ const path = require('path');
5
+
6
+ const app = express();
7
+ const UPLOAD_DIR = path.join(__dirname, 'uploads'); // Folder untuk menyimpan file
8
+
9
+ // Pastikan folder upload ada
10
+ if (!fs.existsSync(UPLOAD_DIR)) {
11
+ fs.mkdirSync(UPLOAD_DIR);
12
+ }
13
+
14
+ app.put('/:filename', (req, res) => {
15
+ const shortId = uuidv4(); // Gunakan UUID penuh
16
+ const filename = req.params.filename;
17
+ const fileDir = path.join(UPLOAD_DIR, shortId); // Folder khusus per upload
18
+ const filepath = path.join(fileDir, filename);
19
+
20
+ // Buat folder untuk file ini
21
+ fs.mkdirSync(fileDir, { recursive: true });
22
+
23
+
24
+ const writeStream = fs.createWriteStream(filepath);
25
+ let bytesReceived = 0;
26
+
27
+
28
+ req.on('data', chunk => {
29
+ writeStream.write(chunk);
30
+ bytesReceived += chunk.length;
31
+ });
32
+
33
+ req.on('end', () => {
34
+ writeStream.end(); // Menutup stream, file selesai ditulis
35
+ console.log(`Uploaded file ${filename}, ${bytesReceived} bytes to ${filepath}`);
36
+ const fileUrl = `https://zhofang-temp-storage.hf.space/${shortId}/${filename}`;
37
+ res.send(`Uploaded 1 file, ${bytesReceived} bytes\n\nwget ${fileUrl}\n`);
38
+
39
+
40
+ // Hapus file dan folder setelah 24 jam
41
+ setTimeout(() => {
42
+ fs.rm(fileDir, { recursive: true, force: true }, (err) => {
43
+ if (err) {
44
+ console.error(`Error deleting file and folder at ${fileDir}:`, err);
45
+ } else {
46
+ console.log(`File and folder at ${fileDir} deleted successfully`);
47
+ }
48
+ });
49
+ }, 24 * 60 * 60 * 1000); // 24 jam dalam milidetik
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
+ });