Spaces:
Sleeping

Starchik1 commited on
Commit
2b7dc62
·
verified ·
1 Parent(s): a8557c1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -128
app.py CHANGED
@@ -15,7 +15,6 @@ import aiofiles
15
  from apscheduler.schedulers.background import BackgroundScheduler
16
 
17
  # ------------------- Configuration -------------------
18
- # Используем /tmp для Hugging Face
19
  BASE_DIR = Path("/tmp/file_sharer")
20
  UPLOAD_DIR = BASE_DIR / "uploads"
21
  CHUNK_DIR = BASE_DIR / "chunks"
@@ -124,6 +123,7 @@ async def index():
124
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
125
  <title>Free File Sharing</title>
126
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.10.1/jszip.min.js"></script>
 
127
  <style>
128
  * { box-sizing: border-box; margin: 0; padding: 0; }
129
  body {
@@ -135,149 +135,56 @@ async def index():
135
  align-items: center;
136
  padding: 20px;
137
  }
138
- .container {
139
- width: 100%;
140
- max-width: 560px;
141
- }
142
  .card {
143
  background: white;
144
  border-radius: 20px;
145
  box-shadow: 0 20px 60px rgba(0,0,0,0.3);
146
  padding: 40px 30px;
147
- transition: transform 0.3s;
148
- }
149
- h1 {
150
- text-align: center;
151
- color: #333;
152
- margin-bottom: 15px;
153
- font-size: 2em;
154
  }
 
155
  .logo {
156
- display: block;
157
  margin: 0 auto 20px;
158
- width: 80px;
159
- height: 80px;
160
  background: linear-gradient(135deg, #667eea, #764ba2);
161
  border-radius: 20px;
162
- display: flex;
163
- align-items: center;
164
- justify-content: center;
165
- font-size: 2.5em;
166
- color: white;
167
  box-shadow: 0 5px 15px rgba(0,0,0,0.2);
168
  }
169
  .drop-zone {
170
- border: 2px dashed #ccc;
171
- border-radius: 15px;
172
- padding: 30px;
173
- text-align: center;
174
- background: #f9f9ff;
175
- margin: 25px 0;
176
- transition: all 0.3s;
177
- cursor: pointer;
178
- }
179
- .drop-zone.dragover {
180
- border-color: #667eea;
181
- background: #eef0ff;
182
- transform: scale(1.02);
183
- }
184
- .drop-zone p {
185
- color: #555;
186
- font-size: 0.95em;
187
- }
188
- .drop-zone label {
189
- color: #667eea;
190
- font-weight: bold;
191
- cursor: pointer;
192
- text-decoration: underline;
193
- }
194
- .progress-container {
195
- margin: 20px 0;
196
- }
197
- .progress-bar-wrapper {
198
- background: #e0e0e0;
199
- border-radius: 20px;
200
- overflow: hidden;
201
- height: 25px;
202
- margin: 10px 0;
203
- }
204
- .progress-fill {
205
- width: 0%;
206
- height: 100%;
207
- background: linear-gradient(90deg, #667eea, #764ba2);
208
- border-radius: 20px;
209
- transition: width 0.2s;
210
- position: relative;
211
- }
212
- .progress-text {
213
- display: flex;
214
- justify-content: space-between;
215
- font-size: 0.9em;
216
- color: #555;
217
- }
218
  .btn {
219
- display: inline-block;
220
- padding: 12px 25px;
221
- border: none;
222
- border-radius: 30px;
223
- font-weight: bold;
224
- cursor: pointer;
225
- transition: all 0.2s;
226
- background: #f0f0f0;
227
- color: #333;
228
- }
229
- .btn-primary {
230
- background: linear-gradient(135deg, #667eea, #764ba2);
231
- color: white;
232
- box-shadow: 0 4px 10px rgba(102,126,234,0.4);
233
- }
234
- .btn-primary:hover {
235
- transform: translateY(-2px);
236
- box-shadow: 0 6px 15px rgba(102,126,234,0.6);
237
- }
238
- .btn-danger {
239
- background: #ff5252;
240
- color: white;
241
- }
242
- .btn-danger:hover {
243
- background: #ff1744;
244
- }
245
- .result-box {
246
- background: #f0fff0;
247
- border-radius: 15px;
248
- padding: 20px;
249
- margin: 20px 0;
250
- border: 1px solid #b8e6b8;
251
- }
252
  .share-link {
253
- display: flex;
254
- align-items: center;
255
- justify-content: space-between;
256
- background: white;
257
- border-radius: 30px;
258
- padding: 8px 20px;
259
- margin: 10px 0;
260
- border: 1px solid #ccc;
261
- }
262
- .share-link a {
263
- color: #333;
264
- text-decoration: none;
265
- font-weight: 500;
266
- word-break: break-all;
267
- }
268
- .copy-btn {
269
- background: none;
270
- border: none;
271
- color: #667eea;
272
- cursor: pointer;
273
- font-size: 1.2em;
274
- margin-left: 10px;
275
  }
 
 
276
  .copy-btn:hover { color: #764ba2; }
277
  .hidden { display: none; }
278
  .error-message { color: #ff5252; text-align: center; margin-top: 10px; }
279
  .success-message { color: #2e7d32; text-align: center; margin-top: 10px; }
280
-
281
  @media (max-width: 600px) {
282
  .card { padding: 25px 20px; }
283
  h1 { font-size: 1.5em; }
@@ -300,12 +207,13 @@ async def index():
300
 
301
  <div id="progress-container" class="progress-container hidden">
302
  <div class="progress-text">
303
- <span id="progress-label">Uploading...</span>
304
  <span id="progress-percent">0%</span>
305
  </div>
306
  <div class="progress-bar-wrapper">
307
  <div id="progress-fill" class="progress-fill" style="width:0%"></div>
308
  </div>
 
309
  <button id="cancel-btn" class="btn" style="margin-top:10px;">Cancel</button>
310
  </div>
311
 
@@ -315,6 +223,7 @@ async def index():
315
  <a id="share-link" href="#" target="_blank"></a>
316
  <button class="copy-btn" id="copy-btn" title="Copy link">📋</button>
317
  </div>
 
318
  <p style="margin-top:15px; font-size:0.9em; color:#555;">Files available for 7 days</p>
319
  <button id="delete-btn" class="btn btn-danger" style="margin-top:10px;">🗑 Delete now</button>
320
  </div>
@@ -327,6 +236,7 @@ async def index():
327
  let currentUploadId = null;
328
  let deleteToken = null;
329
  let abortController = null;
 
330
 
331
  const dropZone = document.getElementById('drop-zone');
332
  const fileInput = document.getElementById('file-input');
@@ -334,14 +244,15 @@ const progressContainer = document.getElementById('progress-container');
334
  const progressFill = document.getElementById('progress-fill');
335
  const progressPercent = document.getElementById('progress-percent');
336
  const progressLabel = document.getElementById('progress-label');
 
337
  const cancelBtn = document.getElementById('cancel-btn');
338
  const resultDiv = document.getElementById('result');
339
  const shareLink = document.getElementById('share-link');
340
  const copyBtn = document.getElementById('copy-btn');
341
  const deleteBtn = document.getElementById('delete-btn');
342
  const messageDiv = document.getElementById('message');
 
343
 
344
- // Drag & Drop
345
  dropZone.addEventListener('dragover', (e) => { e.preventDefault(); dropZone.classList.add('dragover'); });
346
  dropZone.addEventListener('dragleave', () => dropZone.classList.remove('dragover'));
347
  dropZone.addEventListener('drop', (e) => {
@@ -452,13 +363,41 @@ function prepareUI(upload_id, short_code, delete_token) {
452
  progressContainer.classList.remove('hidden');
453
  progressFill.style.width = '0%';
454
  progressPercent.textContent = '0%';
455
- progressLabel.textContent = 'Uploading...';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
456
  }
457
 
458
  async function uploadFileInChunks(file, fileName, fileSize) {
459
  const chunkCount = Math.ceil(fileSize / (5 * 1024 * 1024));
460
  abortController = new AbortController();
461
  let uploaded = 0;
 
462
  try {
463
  for (let i = 0; i < chunkCount; i++) {
464
  if (abortController.signal.aborted) throw new Error('Cancelled');
@@ -480,9 +419,13 @@ async function uploadFileInChunks(file, fileName, fileSize) {
480
  if (!res.ok) throw new Error('Error');
481
  uploaded += (end - start);
482
  const percent = Math.round((uploaded / fileSize) * 100);
 
 
 
483
  progressFill.style.width = percent + '%';
484
  progressPercent.textContent = percent + '%';
485
- progressLabel.textContent = `Uploaded ${percent}%`;
 
486
  }
487
  } catch (err) {
488
  if (err.message !== 'Cancelled') showError(err.message);
 
15
  from apscheduler.schedulers.background import BackgroundScheduler
16
 
17
  # ------------------- Configuration -------------------
 
18
  BASE_DIR = Path("/tmp/file_sharer")
19
  UPLOAD_DIR = BASE_DIR / "uploads"
20
  CHUNK_DIR = BASE_DIR / "chunks"
 
123
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
124
  <title>Free File Sharing</title>
125
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.10.1/jszip.min.js"></script>
126
+ <script src="https://cdn.jsdelivr.net/npm/qrcodejs@1.0.0/qrcode.min.js"></script>
127
  <style>
128
  * { box-sizing: border-box; margin: 0; padding: 0; }
129
  body {
 
135
  align-items: center;
136
  padding: 20px;
137
  }
138
+ .container { width: 100%; max-width: 560px; }
 
 
 
139
  .card {
140
  background: white;
141
  border-radius: 20px;
142
  box-shadow: 0 20px 60px rgba(0,0,0,0.3);
143
  padding: 40px 30px;
 
 
 
 
 
 
 
144
  }
145
+ h1 { text-align: center; color: #333; margin-bottom: 15px; font-size: 2em; }
146
  .logo {
 
147
  margin: 0 auto 20px;
148
+ width: 80px; height: 80px;
 
149
  background: linear-gradient(135deg, #667eea, #764ba2);
150
  border-radius: 20px;
151
+ display: flex; align-items: center; justify-content: center;
152
+ font-size: 2.5em; color: white;
 
 
 
153
  box-shadow: 0 5px 15px rgba(0,0,0,0.2);
154
  }
155
  .drop-zone {
156
+ border: 2px dashed #ccc; border-radius: 15px; padding: 30px;
157
+ text-align: center; background: #f9f9ff; margin: 25px 0;
158
+ transition: all 0.3s; cursor: pointer;
159
+ }
160
+ .drop-zone.dragover { border-color: #667eea; background: #eef0ff; transform: scale(1.02); }
161
+ .drop-zone p { color: #555; font-size: 0.95em; }
162
+ .drop-zone label { color: #667eea; font-weight: bold; text-decoration: underline; cursor: pointer; }
163
+ .progress-container { margin: 20px 0; }
164
+ .progress-bar-wrapper { background: #e0e0e0; border-radius: 20px; overflow: hidden; height: 25px; margin: 10px 0; }
165
+ .progress-fill { width: 0%; height: 100%; background: linear-gradient(90deg, #667eea, #764ba2); border-radius: 20px; transition: width 0.2s; }
166
+ .progress-text { display: flex; justify-content: space-between; font-size: 0.9em; color: #555; margin-bottom: 5px; }
167
+ .time-remaining { text-align: center; font-size: 0.9em; color: #555; }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
168
  .btn {
169
+ display: inline-block; padding: 12px 25px; border: none; border-radius: 30px;
170
+ font-weight: bold; cursor: pointer; transition: all 0.2s; background: #f0f0f0; color: #333;
171
+ }
172
+ .btn-primary { background: linear-gradient(135deg, #667eea, #764ba2); color: white; box-shadow: 0 4px 10px rgba(102,126,234,0.4); }
173
+ .btn-primary:hover { transform: translateY(-2px); box-shadow: 0 6px 15px rgba(102,126,234,0.6); }
174
+ .btn-danger { background: #ff5252; color: white; }
175
+ .btn-danger:hover { background: #ff1744; }
176
+ .result-box { background: #f0fff0; border-radius: 15px; padding: 20px; margin: 20px 0; border: 1px solid #b8e6b8; text-align: center; }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
177
  .share-link {
178
+ display: flex; align-items: center; justify-content: space-between;
179
+ background: white; border-radius: 30px; padding: 8px 20px; margin: 10px 0; border: 1px solid #ccc;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
180
  }
181
+ .share-link a { color: #333; text-decoration: none; font-weight: 500; word-break: break-all; }
182
+ .copy-btn { background: none; border: none; color: #667eea; cursor: pointer; font-size: 1.2em; margin-left: 10px; }
183
  .copy-btn:hover { color: #764ba2; }
184
  .hidden { display: none; }
185
  .error-message { color: #ff5252; text-align: center; margin-top: 10px; }
186
  .success-message { color: #2e7d32; text-align: center; margin-top: 10px; }
187
+ #qrcode { margin-top: 15px; display: inline-block; }
188
  @media (max-width: 600px) {
189
  .card { padding: 25px 20px; }
190
  h1 { font-size: 1.5em; }
 
207
 
208
  <div id="progress-container" class="progress-container hidden">
209
  <div class="progress-text">
210
+ <span id="progress-label">Uploading... 0%</span>
211
  <span id="progress-percent">0%</span>
212
  </div>
213
  <div class="progress-bar-wrapper">
214
  <div id="progress-fill" class="progress-fill" style="width:0%"></div>
215
  </div>
216
+ <div id="time-remaining" class="time-remaining"></div>
217
  <button id="cancel-btn" class="btn" style="margin-top:10px;">Cancel</button>
218
  </div>
219
 
 
223
  <a id="share-link" href="#" target="_blank"></a>
224
  <button class="copy-btn" id="copy-btn" title="Copy link">📋</button>
225
  </div>
226
+ <div id="qrcode"></div>
227
  <p style="margin-top:15px; font-size:0.9em; color:#555;">Files available for 7 days</p>
228
  <button id="delete-btn" class="btn btn-danger" style="margin-top:10px;">🗑 Delete now</button>
229
  </div>
 
236
  let currentUploadId = null;
237
  let deleteToken = null;
238
  let abortController = null;
239
+ let uploadStartTime = null;
240
 
241
  const dropZone = document.getElementById('drop-zone');
242
  const fileInput = document.getElementById('file-input');
 
244
  const progressFill = document.getElementById('progress-fill');
245
  const progressPercent = document.getElementById('progress-percent');
246
  const progressLabel = document.getElementById('progress-label');
247
+ const timeRemaining = document.getElementById('time-remaining');
248
  const cancelBtn = document.getElementById('cancel-btn');
249
  const resultDiv = document.getElementById('result');
250
  const shareLink = document.getElementById('share-link');
251
  const copyBtn = document.getElementById('copy-btn');
252
  const deleteBtn = document.getElementById('delete-btn');
253
  const messageDiv = document.getElementById('message');
254
+ const qrDiv = document.getElementById('qrcode');
255
 
 
256
  dropZone.addEventListener('dragover', (e) => { e.preventDefault(); dropZone.classList.add('dragover'); });
257
  dropZone.addEventListener('dragleave', () => dropZone.classList.remove('dragover'));
258
  dropZone.addEventListener('drop', (e) => {
 
363
  progressContainer.classList.remove('hidden');
364
  progressFill.style.width = '0%';
365
  progressPercent.textContent = '0%';
366
+ progressLabel.textContent = 'Uploading... 0%';
367
+ timeRemaining.textContent = 'Time remaining: calculating...';
368
+ qrDiv.innerHTML = '';
369
+ new QRCode(qrDiv, {
370
+ text: shareLink.textContent,
371
+ width: 128,
372
+ height: 128,
373
+ });
374
+ }
375
+
376
+ function formatSpeed(bytesPerSec) {
377
+ if (bytesPerSec === 0) return '0 KB/s';
378
+ if (bytesPerSec < 1024) return bytesPerSec.toFixed(0) + ' B/s';
379
+ const kb = bytesPerSec / 1024;
380
+ if (kb < 1024) return kb.toFixed(1) + ' KB/s';
381
+ const mb = kb / 1024;
382
+ return mb.toFixed(1) + ' MB/s';
383
+ }
384
+
385
+ function formatTime(seconds) {
386
+ if (!isFinite(seconds) || seconds < 0) return '...';
387
+ if (seconds < 60) return Math.round(seconds) + 's';
388
+ const mins = Math.floor(seconds / 60);
389
+ const secs = Math.round(seconds % 60);
390
+ if (mins < 60) return mins + 'm ' + secs + 's';
391
+ const hours = Math.floor(mins / 60);
392
+ const remainingMins = mins % 60;
393
+ return hours + 'h ' + remainingMins + 'm';
394
  }
395
 
396
  async function uploadFileInChunks(file, fileName, fileSize) {
397
  const chunkCount = Math.ceil(fileSize / (5 * 1024 * 1024));
398
  abortController = new AbortController();
399
  let uploaded = 0;
400
+ uploadStartTime = Date.now();
401
  try {
402
  for (let i = 0; i < chunkCount; i++) {
403
  if (abortController.signal.aborted) throw new Error('Cancelled');
 
419
  if (!res.ok) throw new Error('Error');
420
  uploaded += (end - start);
421
  const percent = Math.round((uploaded / fileSize) * 100);
422
+ const elapsed = (Date.now() - uploadStartTime) / 1000;
423
+ const speed = uploaded / (elapsed || 1);
424
+ const remaining = (fileSize - uploaded) / (speed || 1);
425
  progressFill.style.width = percent + '%';
426
  progressPercent.textContent = percent + '%';
427
+ progressLabel.textContent = `Uploaded ${percent}% · ${formatSpeed(speed)}`;
428
+ timeRemaining.textContent = `Time remaining: ${formatTime(remaining)}`;
429
  }
430
  } catch (err) {
431
  if (err.message !== 'Cancelled') showError(err.message);