File size: 9,326 Bytes
70e4b51
 
 
2ba30ac
70e4b51
 
 
 
2ba30ac
70e4b51
 
 
 
5475b91
 
 
 
 
2ba30ac
 
 
 
 
 
5475b91
2ba30ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2346531
2ba30ac
 
 
 
 
 
 
2346531
2ba30ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70e4b51
5475b91
70e4b51
 
 
 
2ba30ac
70e4b51
 
49cf139
70e4b51
 
2ba30ac
70e4b51
 
2346531
70e4b51
2ba30ac
2346531
70e4b51
2346531
 
70e4b51
2346531
70e4b51
 
 
 
 
 
 
2346531
 
 
70e4b51
 
741ec55
70e4b51
2346531
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
const express = require('express');
const fs = require('fs');
const path = require('path');
const multer = require('multer');

const app = express();
const uploadDir = path.join(__dirname, 'uploads');

// Ensure uploads directory exists
if (!fs.existsSync(uploadDir)) {
    fs.mkdirSync(uploadDir);
}

// Function to generate a short random ID (alphanumeric)
function generateShortId() {
    return Math.random().toString(36).substring(2, 7); // Generates a random string of length 5
}

// Configure multer for file storage
const storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, uploadDir);
    },
    filename: function (req, file, cb) {
        const shortId = generateShortId();
        cb(null, `${shortId}-${file.originalname}`);
    }
});

const upload = multer({ storage: storage });

// Route for browser upload
app.get('/', (req, res) => {
    res.send(`
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Enhanced File Upload</title>
        <style>
            body {
                font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
                background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
                height: 100vh;
                margin: 0;
                display: flex;
                justify-content: center;
                align-items: center;
            }
            .container {
                background-color: rgba(255, 255, 255, 0.9);
                padding: 2rem;
                border-radius: 10px;
                box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
                text-align: center;
                width: 300px;
            }
            h1 {
                color: #4a5568;
                margin-bottom: 1.5rem;
            }
            form {
                display: flex;
                flex-direction: column;
                align-items: center;
            }
            input[type="file"] {
                display: none;
            }
            .file-label {
                background-color: #4a5568;
                color: white;
                padding: 0.5rem 1rem;
                border-radius: 5px;
                cursor: pointer;
                margin-bottom: 1rem;
                transition: background-color 0.3s ease;
            }
            .file-label:hover {
                background-color: #2d3748;
            }
            #file-name {
                margin-bottom: 1rem;
                word-break: break-all;
            }
            button {
                background-color: #4299e1;
                color: white;
                border: none;
                padding: 0.5rem 1rem;
                border-radius: 5px;
                cursor: pointer;
                transition: background-color 0.3s ease;
            }
            button:hover {
                background-color: #3182ce;
            }
            #upload-progress {
                width: 100%;
                background-color: #e2e8f0;
                border-radius: 5px;
                margin-top: 1rem;
                overflow: hidden;
                display: none;
            }
            #progress-bar {
                width: 0;
                height: 10px;
                background-color: #48bb78;
                transition: width 0.5s ease;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <h1>Upload File</h1>
            <form id="upload-form" action="/upload" method="post" enctype="multipart/form-data">
                <label for="file-upload" class="file-label">Choose File</label>
                <input id="file-upload" type="file" name="file">
                <div id="file-name"></div>
                <button type="submit">Upload</button>
            </form>
            <div id="upload-progress">
                <div id="progress-bar"></div>
            </div>
        </div>

        <script>
            const fileUpload = document.getElementById('file-upload');
            const fileName = document.getElementById('file-name');
            const uploadForm = document.getElementById('upload-form');
            const uploadProgress = document.getElementById('upload-progress');
            const progressBar = document.getElementById('progress-bar');

            fileUpload.addEventListener('change', (e) => {
                if (e.target.files.length > 0) {
                    fileName.textContent = e.target.files[0].name;
                } else {
                    fileName.textContent = '';
                }
            });

            uploadForm.addEventListener('submit', (e) => {
                e.preventDefault();
                if (!fileUpload.files.length) {
                    alert('Please select a file to upload.');
                    return;
                }

                const formData = new FormData(uploadForm);
                const xhr = new XMLHttpRequest();

                xhr.open('POST', '/upload', true);

                xhr.upload.onprogress = (event) => {
                    if (event.lengthComputable) {
                        const percentComplete = (event.loaded / event.total) * 100;
                        uploadProgress.style.display = 'block';
                        progressBar.style.width = percentComplete + '%';
                    }
                };

                xhr.onload = function() {
                    if (xhr.status === 200) {
                        alert('Upload complete!');
                        document.body.innerHTML = xhr.responseText;
                    } else {
                        alert('Upload failed. Please try again.');
                    }
                    uploadProgress.style.display = 'none';
                    progressBar.style.width = '0';
                    fileName.textContent = '';
                    uploadForm.reset();
                };

                xhr.send(formData);
            });
        </script>
    </body>
    </html>
    `);
});

// Handler for browser upload
app.post('/upload', upload.single('file'), (req, res) => {
    if (!req.file) {
        return res.status(400).send('No file uploaded.');
    }

    const fileUrl = `https://zhofang-temp-storage.hf.space/${req.file.filename.split('-')[0]}/${req.file.originalname}`;
    res.send(`
    <html>
    <head>
        <title>File Uploaded</title>
        <style>
            body {
                font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
                background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
                height: 100vh;
                margin: 0;
                display: flex;
                justify-content: center;
                align-items: center;
                color: white;
            }
            .container {
                background-color: rgba(255, 255, 255, 0.1);
                padding: 2rem;
                border-radius: 10px;
                box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
                text-align: center;
            }
            a {
                color: #4299e1;
                text-decoration: none;
            }
            a:hover {
                text-decoration: underline;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <h1>File Uploaded Successfully</h1>
            <p>Uploaded 1 file, ${req.file.size} bytes</p>
            <p>Download link: <a href="${fileUrl}">${fileUrl}</a></p>
            <p>wget command: <code>wget ${fileUrl}</code></p>
        </div>
    </body>
    </html>
    `);

    // Delete file after 24 hours
    setTimeout(() => {
        fs.unlink(req.file.path, (err) => {
            if (err) console.error(`Error deleting file: ${err}`);
        });
    }, 24 * 60 * 60 * 1000); // 24 hours in milliseconds
});

// Route for upload via PUT (like bashupload)
app.put('/:filename', (req, res) => {
    const shortId = generateShortId();
    const filename = req.params.filename;
    const filepath = path.join(uploadDir, `${shortId}-${filename}`);
    const fileStream = fs.createWriteStream(filepath);

    req.pipe(fileStream);

    fileStream.on('finish', () => {
        const fileUrl = `https://zhofang-temp-storage.hf.space/${shortId}/${filename}`;
        res.send(`Uploaded 1 file, ${req.headers['content-length']} bytes\n\nwget ${fileUrl}\n`);

        // Delete file after 24 hours
        setTimeout(() => {
            fs.unlink(filepath, (err) => {
                if (err) console.error(`Error deleting file: ${err}`);
            });
        }, 24 * 60 * 60 * 1000); // 24 hours in milliseconds
    });

    fileStream.on('error', (err) => {
        console.error(`Error writing file: ${err}`);
        res.status(500).send('Error uploading file.');
    });
});

app.get('/:id/:filename', (req, res) => {
    const filepath = path.join(uploadDir, `${req.params.id}-${req.params.filename}`);
    res.download(filepath, req.params.filename, (err) => {
        if (err) {
            console.error(`Error downloading file: ${err}`);
            res.status(404).send('File not found.');
        }
    });
});

app.listen(7860, () => {
    console.log('Server is running on http://localhost:3000');
});