Zhofang commited on
Commit
2ba30ac
·
verified ·
1 Parent(s): 694d8a5

Update temp.js

Browse files
Files changed (1) hide show
  1. temp.js +230 -12
temp.js CHANGED
@@ -1,40 +1,259 @@
1
  const express = require('express');
2
  const fs = require('fs');
3
  const path = require('path');
 
 
4
 
5
  const app = express();
6
  const uploadDir = path.join(__dirname, 'uploads');
7
 
8
- // Pastikan direktori uploads ada
9
  if (!fs.existsSync(uploadDir)) {
10
  fs.mkdirSync(uploadDir);
11
  }
12
 
13
- function generateShortId() {
14
- const timestamp = Date.now().toString(36);
15
- const random = Math.random().toString(36).substring(2, 7);
16
- return `${timestamp}-${random}`;
17
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
 
 
 
 
 
 
 
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  app.put('/:filename', (req, res) => {
21
- const shortId = generateShortId();
22
  const filename = req.params.filename;
23
  const filepath = path.join(uploadDir, `${shortId}-${filename}`);
24
  const fileStream = fs.createWriteStream(filepath);
25
 
26
- req.pipe(fileStream);
27
 
28
  fileStream.on('finish', () => {
29
  const fileUrl = `https://zhofang-temp-storage.hf.space/${shortId}/${filename}`;
30
  res.send(`Uploaded 1 file, ${req.headers['content-length']} bytes\n\nwget ${fileUrl}\n`);
31
 
32
- // Hapus file setelah 24 jam
33
  setTimeout(() => {
34
  fs.unlink(filepath, (err) => {
35
  if (err) console.error(`Error deleting file: ${err}`);
36
  });
37
- }, 24 * 60 * 60 * 1000); // 24 jam dalam milidetik
38
  });
39
 
40
  fileStream.on('error', (err) => {
@@ -53,7 +272,6 @@ app.get('/:id/:filename', (req, res) => {
53
  });
54
  });
55
 
56
-
57
- app.listen(7860, () => {
58
  console.log('Server is running on http://localhost:3000');
59
  });
 
1
  const express = require('express');
2
  const fs = require('fs');
3
  const path = require('path');
4
+ const { v6: uuidv6 } = require('uuid');
5
+ const multer = require('multer');
6
 
7
  const app = express();
8
  const uploadDir = path.join(__dirname, 'uploads');
9
 
10
+ // Ensure uploads directory exists
11
  if (!fs.existsSync(uploadDir)) {
12
  fs.mkdirSync(uploadDir);
13
  }
14
 
15
+ // Configure multer for file storage
16
+ const storage = multer.diskStorage({
17
+ destination: function (req, file, cb) {
18
+ cb(null, uploadDir);
19
+ },
20
+ filename: function (req, file, cb) {
21
+ const shortId = uuidv6().slice(0, 5);
22
+ cb(null, `${shortId}-${file.originalname}`);
23
+ }
24
+ });
25
+
26
+ const upload = multer({ storage: storage });
27
+
28
+ // Route for browser upload
29
+ app.get('/', (req, res) => {
30
+ res.send(`
31
+ <!DOCTYPE html>
32
+ <html lang="en">
33
+ <head>
34
+ <meta charset="UTF-8">
35
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
36
+ <title>Enhanced File Upload</title>
37
+ <style>
38
+ body {
39
+ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
40
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
41
+ height: 100vh;
42
+ margin: 0;
43
+ display: flex;
44
+ justify-content: center;
45
+ align-items: center;
46
+ }
47
+ .container {
48
+ background-color: rgba(255, 255, 255, 0.9);
49
+ padding: 2rem;
50
+ border-radius: 10px;
51
+ box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
52
+ text-align: center;
53
+ width: 300px;
54
+ }
55
+ h1 {
56
+ color: #4a5568;
57
+ margin-bottom: 1.5rem;
58
+ }
59
+ form {
60
+ display: flex;
61
+ flex-direction: column;
62
+ align-items: center;
63
+ }
64
+ input[type="file"] {
65
+ display: none;
66
+ }
67
+ .file-label {
68
+ background-color: #4a5568;
69
+ color: white;
70
+ padding: 0.5rem 1rem;
71
+ border-radius: 5px;
72
+ cursor: pointer;
73
+ margin-bottom: 1rem;
74
+ transition: background-color 0.3s ease;
75
+ }
76
+ .file-label:hover {
77
+ background-color: #2d3748;
78
+ }
79
+ #file-name {
80
+ margin-bottom: 1rem;
81
+ word-break: break-all;
82
+ }
83
+ button {
84
+ background-color: #4299e1;
85
+ color: white;
86
+ border: none;
87
+ padding: 0.5rem 1rem;
88
+ border-radius: 5px;
89
+ cursor: pointer;
90
+ transition: background-color 0.3s ease;
91
+ }
92
+ button:hover {
93
+ background-color: #3182ce;
94
+ }
95
+ #upload-progress {
96
+ width: 100%;
97
+ background-color: #e2e8f0;
98
+ border-radius: 5px;
99
+ margin-top: 1rem;
100
+ overflow: hidden;
101
+ display: none;
102
+ }
103
+ #progress-bar {
104
+ width: 0;
105
+ height: 10px;
106
+ background-color: #48bb78;
107
+ transition: width 0.5s ease;
108
+ }
109
+ </style>
110
+ </head>
111
+ <body>
112
+ <div class="container">
113
+ <h1>Upload File</h1>
114
+ <form id="upload-form" action="/upload" method="post" enctype="multipart/form-data">
115
+ <label for="file-upload" class="file-label">Choose File</label>
116
+ <input id="file-upload" type="file" name="file">
117
+ <div id="file-name"></div>
118
+ <button type="submit">Upload</button>
119
+ </form>
120
+ <div id="upload-progress">
121
+ <div id="progress-bar"></div>
122
+ </div>
123
+ </div>
124
+
125
+ <script>
126
+ const fileUpload = document.getElementById('file-upload');
127
+ const fileName = document.getElementById('file-name');
128
+ const uploadForm = document.getElementById('upload-form');
129
+ const uploadProgress = document.getElementById('upload-progress');
130
+ const progressBar = document.getElementById('progress-bar');
131
+
132
+ fileUpload.addEventListener('change', (e) => {
133
+ if (e.target.files.length > 0) {
134
+ fileName.textContent = e.target.files[0].name;
135
+ } else {
136
+ fileName.textContent = '';
137
+ }
138
+ });
139
+
140
+ uploadForm.addEventListener('submit', (e) => {
141
+ e.preventDefault();
142
+ if (!fileUpload.files.length) {
143
+ alert('Please select a file to upload.');
144
+ return;
145
+ }
146
+
147
+ const formData = new FormData(uploadForm);
148
+ const xhr = new XMLHttpRequest();
149
+
150
+ xhr.open('POST', '/upload', true);
151
 
152
+ xhr.upload.onprogress = (event) => {
153
+ if (event.lengthComputable) {
154
+ const percentComplete = (event.loaded / event.total) * 100;
155
+ uploadProgress.style.display = 'block';
156
+ progressBar.style.width = percentComplete + '%';
157
+ }
158
+ };
159
 
160
+ xhr.onload = function() {
161
+ if (xhr.status === 200) {
162
+ alert('Upload complete!');
163
+ document.body.innerHTML = xhr.responseText;
164
+ } else {
165
+ alert('Upload failed. Please try again.');
166
+ }
167
+ uploadProgress.style.display = 'none';
168
+ progressBar.style.width = '0';
169
+ fileName.textContent = '';
170
+ uploadForm.reset();
171
+ };
172
+
173
+ xhr.send(formData);
174
+ });
175
+ </script>
176
+ </body>
177
+ </html>
178
+ `);
179
+ });
180
+
181
+ // Handler for browser upload
182
+ app.post('/upload', upload.single('file'), (req, res) => {
183
+ if (!req.file) {
184
+ return res.status(400).send('No file uploaded.');
185
+ }
186
+
187
+ const fileUrl = `https://zhofang-temp-storage.hf.space/${req.file.filename.split('-')[0]}/${req.file.originalname}`;
188
+ res.send(`
189
+ <html>
190
+ <head>
191
+ <title>File Uploaded</title>
192
+ <style>
193
+ body {
194
+ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
195
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
196
+ height: 100vh;
197
+ margin: 0;
198
+ display: flex;
199
+ justify-content: center;
200
+ align-items: center;
201
+ color: white;
202
+ }
203
+ .container {
204
+ background-color: rgba(255, 255, 255, 0.1);
205
+ padding: 2rem;
206
+ border-radius: 10px;
207
+ box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
208
+ text-align: center;
209
+ }
210
+ a {
211
+ color: #4299e1;
212
+ text-decoration: none;
213
+ }
214
+ a:hover {
215
+ text-decoration: underline;
216
+ }
217
+ </style>
218
+ </head>
219
+ <body>
220
+ <div class="container">
221
+ <h1>File Uploaded Successfully</h1>
222
+ <p>Uploaded 1 file, ${req.file.size} bytes</p>
223
+ <p>Download link: <a href="${fileUrl}">${fileUrl}</a></p>
224
+ <p>wget command: <code>wget ${fileUrl}</code></p>
225
+ </div>
226
+ </body>
227
+ </html>
228
+ `);
229
+
230
+ // Delete file after 24 hours
231
+ setTimeout(() => {
232
+ fs.unlink(req.file.path, (err) => {
233
+ if (err) console.error(`Error deleting file: ${err}`);
234
+ });
235
+ }, 24 * 60 * 60 * 1000); // 24 hours in milliseconds
236
+ });
237
+
238
+ // Route for upload via PUT (like bashupload)
239
  app.put('/:filename', (req, res) => {
240
+ const shortId = uuidv6().slice(0, 5);
241
  const filename = req.params.filename;
242
  const filepath = path.join(uploadDir, `${shortId}-${filename}`);
243
  const fileStream = fs.createWriteStream(filepath);
244
 
245
+ req.pipe(fileStream);
246
 
247
  fileStream.on('finish', () => {
248
  const fileUrl = `https://zhofang-temp-storage.hf.space/${shortId}/${filename}`;
249
  res.send(`Uploaded 1 file, ${req.headers['content-length']} bytes\n\nwget ${fileUrl}\n`);
250
 
251
+ // Delete file after 24 hours
252
  setTimeout(() => {
253
  fs.unlink(filepath, (err) => {
254
  if (err) console.error(`Error deleting file: ${err}`);
255
  });
256
+ }, 24 * 60 * 60 * 1000); // 24 hours in milliseconds
257
  });
258
 
259
  fileStream.on('error', (err) => {
 
272
  });
273
  });
274
 
275
+ app.listen(3000, () => {
 
276
  console.log('Server is running on http://localhost:3000');
277
  });