coollsd commited on
Commit
85aea4b
·
verified ·
1 Parent(s): 1ad2c1b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -22
app.py CHANGED
@@ -477,11 +477,7 @@ HTML_CONTENT = """
477
  const formData = new FormData();
478
  formData.append('file', file);
479
 
480
- let retryCount = 0;
481
- const maxRetries = 5;
482
- const retryDelay = 1000; // 1 second
483
-
484
- while (retryCount < maxRetries) {
485
  try {
486
  const xhr = new XMLHttpRequest();
487
  xhr.open('POST', '/upload', true);
@@ -492,8 +488,10 @@ HTML_CONTENT = """
492
  const response = JSON.parse(xhr.responseText);
493
  if (response.url) {
494
  addResultLink(response.url, file.name);
 
 
495
  } else {
496
- alert('Upload failed: ' + response.error);
497
  }
498
  } else {
499
  throw new Error(`HTTP error! status: ${xhr.status}`);
@@ -515,17 +513,11 @@ HTML_CONTENT = """
515
  break; // Success, exit the loop
516
  } catch (error) {
517
  console.error('Upload error:', error);
518
- retryCount++;
519
- if (retryCount < maxRetries) {
520
- alert(`Network error. Retrying in ${retryDelay / 1000} seconds... (Attempt ${retryCount} of ${maxRetries})`);
521
- await new Promise(resolve => setTimeout(resolve, retryDelay));
522
- } else {
523
- alert('Upload failed after multiple attempts. Please check your internet connection and try again.');
524
- }
525
  }
526
  }
527
-
528
- resetUploadState();
529
  }
530
 
531
  function createProgressBar(fileName) {
@@ -746,18 +738,16 @@ async def upload_file(upload_url: str, file_content: bytes, content_type: str) -
746
  return False
747
 
748
  async def retry_upload(upload_url: str, file_content: bytes, content_type: str, max_retries: int = 5, delay: int = 1) -> bool:
749
- for attempt in range(1, max_retries + 1):
750
  try:
751
  success = await upload_file(upload_url, file_content, content_type)
752
  if success:
753
  return True
754
- print(f"Upload attempt {attempt} failed. Retrying...")
755
  except Exception as e:
756
- print(f"Error during upload attempt {attempt}: {e}")
757
 
758
- if attempt < max_retries:
759
- await asyncio.sleep(delay)
760
- delay *= 2 # Exponential backoff
761
 
762
- print("Upload failed after all retry attempts")
763
  return False
 
477
  const formData = new FormData();
478
  formData.append('file', file);
479
 
480
+ while (true) {
 
 
 
 
481
  try {
482
  const xhr = new XMLHttpRequest();
483
  xhr.open('POST', '/upload', true);
 
488
  const response = JSON.parse(xhr.responseText);
489
  if (response.url) {
490
  addResultLink(response.url, file.name);
491
+ resetUploadState();
492
+ return;
493
  } else {
494
+ throw new Error('Upload failed: ' + response.error);
495
  }
496
  } else {
497
  throw new Error(`HTTP error! status: ${xhr.status}`);
 
513
  break; // Success, exit the loop
514
  } catch (error) {
515
  console.error('Upload error:', error);
516
+ // Wait for a short time before retrying
517
+ await new Promise(resolve => setTimeout(resolve, 1000));
518
+ // The loop will continue, retrying the upload
 
 
 
 
519
  }
520
  }
 
 
521
  }
522
 
523
  function createProgressBar(fileName) {
 
738
  return False
739
 
740
  async def retry_upload(upload_url: str, file_content: bytes, content_type: str, max_retries: int = 5, delay: int = 1) -> bool:
741
+ while True:
742
  try:
743
  success = await upload_file(upload_url, file_content, content_type)
744
  if success:
745
  return True
746
+ print("Upload failed. Retrying...")
747
  except Exception as e:
748
+ print(f"Error during upload: {e}")
749
 
750
+ await asyncio.sleep(delay)
751
+ delay = min(delay * 2, 60) # Exponential backoff, capped at 60 seconds
 
752
 
 
753
  return False