File size: 8,874 Bytes
2d88112
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>آپلودر پیشرفته فایل به Hugging Face</title>
    <style>
        body { 
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; 
            text-align: right; 
            margin: 20px; 
            background-color: #f4f4f9; 
            color: #333; 
        }
        .container { 
            max-width: 600px; 
            margin: 40px auto; 
            padding: 30px; 
            background-color: #fff; 
            border-radius: 8px; 
            box-shadow: 0 4px 12px rgba(0,0,0,0.1); 
        }
        h1, h2 { 
            color: #4a4a4a; 
            text-align: center; 
        }
        .upload-section { 
            margin-bottom: 25px; 
            padding: 20px; 
            border: 1px dashed #ccc; 
            border-radius: 5px; 
        }
        label { 
            display: block; 
            margin-bottom: 8px; 
            font-weight: bold; 
        }
        input[type="file"], input[type="text"] { 
            width: calc(100% - 22px); 
            padding: 10px; 
            margin-bottom: 15px; 
            border: 1px solid #ddd; 
            border-radius: 4px; 
            box-sizing: border-box; 
        }
        button { 
            background-color: #5e72e4; 
            color: white; 
            padding: 12px 20px; 
            border: none; 
            border-radius: 5px; 
            cursor: pointer; 
            width: 100%; 
            font-size: 16px; 
            transition: background-color 0.2s; 
        }
        button:hover { 
            background-color: #485ab8; 
        }
        button:disabled { 
            background-color: #cccccc; 
            cursor: not-allowed; 
        }
        #result { 
            margin-top: 25px; 
            padding: 15px; 
            border-radius: 5px; 
            background-color: #f0f0f0; 
            min-height: 50px; 
            word-wrap: break-word; 
            text-align: left; 
        }
        .progress-container { 
            width: 100%; 
            background-color: #e0e0e0; 
            border-radius: 4px; 
            display: none; 
            margin-top: 15px; 
            overflow: hidden; /* برای گرد شدن گوشه های نوار پیشرفت */
        }
        .progress-bar { 
            width: 0%; 
            height: 20px; 
            background-color: #4caf50; 
            border-radius: 4px; 
            text-align: center; 
            line-height: 20px; 
            color: white; 
            transition: width 0.3s ease-in-out;
        }
        .success { color: #28a745; } 
        .error { color: #dc3545; } 
        .loading { color: #ffc107; }
        .result-link { 
            font-weight: bold; 
            color: #5e72e4; 
            text-decoration: none; 
        }
        .result-link:hover { 
            text-decoration: underline; 
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>آپلودر پیشرفته فایل</h1>
        <div class="upload-section">
            <h2>آپلود از کامپیوتر</h2>
            <input type="file" id="fileInput" accept="*/*">
            <button id="uploadFileBtn">آپلود فایل</button>
        </div>
        <div class="upload-section">
            <h2>آپلود از طریق لینک</h2>
            <input type="text" id="urlInput" placeholder="آدرس URL فایل را وارد کنید">
            <button id="uploadUrlBtn">آپلود از لینک</button>
        </div>
        <div class="progress-container" id="progressContainer">
            <div class="progress-bar" id="progressBar">0%</div>
        </div>
        <div id="result"><p>نتیجه اینجا نمایش داده می‌شود.</p></div>
    </div>
    <script>
        const fileInput = document.getElementById('fileInput');
        const uploadFileBtn = document.getElementById('uploadFileBtn');
        const urlInput = document.getElementById('urlInput');
        const uploadUrlBtn = document.getElementById('uploadUrlBtn');
        const resultDiv = document.getElementById('result');
        const progressContainer = document.getElementById('progressContainer');
        const progressBar = document.getElementById('progressBar');
        
        const API_URL = '/upload'; 

        function showResult(message, type = 'info') { 
            resultDiv.innerHTML = `<p class="${type}">${message}</p>`; 
        }
        function setButtonsEnabled(enabled) { 
            uploadFileBtn.disabled = !enabled; 
            uploadUrlBtn.disabled = !enabled; 
        }
        function updateProgress(percent) {
            progressContainer.style.display = 'block';
            progressBar.style.width = percent + '%';
            progressBar.textContent = percent + '%';
        }
        function hideProgress() {
            setTimeout(() => { 
                progressContainer.style.display = 'none'; 
                updateProgress(0);
            }, 2000);
        }

        function uploadWithXHR(requestData, isFormData = false) {
            return new Promise((resolve, reject) => {
                const xhr = new XMLHttpRequest();
                xhr.open('POST', API_URL, true);

                xhr.upload.onprogress = function(event) {
                    if (event.lengthComputable && isFormData) {
                        const percentComplete = Math.round((event.loaded / event.total) * 100);
                        updateProgress(percentComplete);
                    }
                };

                xhr.onload = function() {
                    if (xhr.status >= 200 && xhr.status < 300) {
                        resolve(JSON.parse(xhr.responseText));
                    } else {
                        try {
                            const errorData = JSON.parse(xhr.responseText);
                            reject(errorData.error || `Error ${xhr.status}: ${xhr.statusText}`);
                        } catch (e) {
                            reject(`Error ${xhr.status}: ${xhr.statusText}`);
                        }
                    }
                };

                xhr.onerror = function() {
                    reject('خطای ارتباط با شبکه رخ داد.');
                };
                
                if (isFormData) {
                    xhr.send(requestData);
                } else {
                    xhr.setRequestHeader('Content-Type', 'application/json');
                    xhr.send(JSON.stringify(requestData));
                }
            });
        }

        uploadFileBtn.addEventListener('click', async () => {
            const file = fileInput.files[0];
            if (!file) { 
                showResult('لطفاً یک فایل انتخاب کنید.', 'error'); 
                return; 
            }
            
            showResult('در حال آماده سازی برای آپلود...', 'loading'); 
            setButtonsEnabled(false);
            updateProgress(0);

            const formData = new FormData();
            formData.append('file', file);
            
            try {
                const data = await uploadWithXHR(formData, true);
                showResult(`فایل با موفقیت آپلود شد! <br> لینک: <a href="${data.hf_url}" target="_blank" class="result-link">${data.hf_url}</a>`, 'success');
            } catch (error) {
                showResult(`خطا در آپلود: ${error}`, 'error');
            } finally {
                setButtonsEnabled(true);
                hideProgress();
            }
        });

        uploadUrlBtn.addEventListener('click', async () => {
            const url = urlInput.value.trim();
            if (!url) { 
                showResult('لطفاً آدرس URL را وارد کنید.', 'error'); 
                return; 
            }
            
            showResult('در حال ارسال درخواست به سرور... (این ممکن است زمان ببرد)', 'loading'); 
            setButtonsEnabled(false);
            progressContainer.style.display = 'none';
            
            try {
                // ***** این خط اصلاح شد *****
                const data = await uploadWithXHR({ url: url }, false);
                showResult(`فایل با موفقیت از لینک آپلود شد! <br> لینک: <a href="${data.hf_url}" target="_blank" class="result-link">${data.hf_url}</a>`, 'success');
            } catch (error) {
                showResult(`خطا در آپلود: ${error}`, 'error');
            } finally {
                setButtonsEnabled(true);
            }
        });
    </script>
</body>
</html>