coollsd commited on
Commit
c22b44a
·
verified ·
1 Parent(s): 548abbb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -35
app.py CHANGED
@@ -408,8 +408,8 @@ HTML_CONTENT = """
408
  const uploadForm = document.getElementById('uploadForm');
409
  const progressContainer = document.getElementById('progressContainer');
410
  const loadingSpinner = document.getElementById('loadingSpinner');
411
- const resultContainer = document.getElementById('resultContainer');
412
- const dropZone = document.getElementById('dropZone');
413
  const modal = document.getElementById('embedModal');
414
  const span = document.getElementsByClassName("close")[0];
415
  const embedLinkInput = document.getElementById('embedLink');
@@ -486,41 +486,49 @@ HTML_CONTENT = """
486
  const formData = new FormData();
487
  formData.append('file', file);
488
 
489
- try {
490
- const xhr = new XMLHttpRequest();
491
- xhr.open('POST', '/upload', true);
492
- xhr.upload.onprogress = (event) => updateProgress(event, progressBar.querySelector('.progress'));
493
-
494
- xhr.onload = function() {
495
- if (xhr.status === 200) {
496
- const response = JSON.parse(xhr.responseText);
497
- if (response.url) {
498
- addResultLink(response.url, file.name);
499
- } else {
500
- throw new Error('Upload failed: ' + response.error);
501
- }
502
- } else {
503
- throw new Error(`HTTP error! status: ${xhr.status}`);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
504
  }
505
- };
506
-
507
- xhr.onerror = function() {
508
- throw new Error('Network error occurred');
509
- };
510
-
511
- xhr.send(formData);
512
-
513
- await new Promise((resolve, reject) => {
514
- xhr.onloadend = resolve;
515
- xhr.onerror = reject;
516
- });
517
-
518
- } catch (error) {
519
- console.error('Upload error:', error);
520
- alert('Upload failed: ' + error.message);
521
- } finally {
522
- resetUploadState();
523
  }
 
 
524
  }
525
 
526
  function createProgressBar(fileName) {
 
408
  const uploadForm = document.getElementById('uploadForm');
409
  const progressContainer = document.getElementById('progressContainer');
410
  const loadingSpinner = document.getElementById('loadingSpinner');
411
+ const resultContainer = document.getElementById('resultContainer');
412
+ const dropZone = document.getElementById('dropZone');
413
  const modal = document.getElementById('embedModal');
414
  const span = document.getElementsByClassName("close")[0];
415
  const embedLinkInput = document.getElementById('embedLink');
 
486
  const formData = new FormData();
487
  formData.append('file', file);
488
 
489
+ let retries = 0;
490
+ const maxRetries = 5;
491
+ const retryDelay = 1000;
492
+
493
+ while (retries < maxRetries) {
494
+ try {
495
+ const xhr = new XMLHttpRequest();
496
+ xhr.open('POST', '/upload', true);
497
+ xhr.upload.onprogress = (event) => updateProgress(event, progressBar.querySelector('.progress'));
498
+
499
+ const uploadPromise = new Promise((resolve, reject) => {
500
+ xhr.onload = function() {
501
+ if (xhr.status === 200) {
502
+ const response = JSON.parse(xhr.responseText);
503
+ if (response.url) {
504
+ resolve(response.url);
505
+ } else {
506
+ reject(new Error('Upload failed: ' + response.error));
507
+ }
508
+ } else {
509
+ reject(new Error(`HTTP error! status: ${xhr.status}`));
510
+ }
511
+ };
512
+ xhr.onerror = () => reject(new Error('Network error occurred'));
513
+ });
514
+
515
+ xhr.send(formData);
516
+
517
+ const url = await uploadPromise;
518
+ addResultLink(url, file.name);
519
+ break; // Success, exit the retry loop
520
+ } catch (error) {
521
+ console.error('Upload error:', error);
522
+ retries++;
523
+ if (retries >= maxRetries) {
524
+ alert('Upload failed after multiple attempts: ' + error.message);
525
+ break;
526
  }
527
+ await new Promise(resolve => setTimeout(resolve, retryDelay));
528
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
529
  }
530
+
531
+ resetUploadState();
532
  }
533
 
534
  function createProgressBar(fileName) {