tecuts commited on
Commit
f05a617
·
verified ·
1 Parent(s): 9a87b07

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -5
app.py CHANGED
@@ -97,16 +97,27 @@ class DownloadResponse(BaseModel):
97
  file_size: int = None # Include file size in the response
98
 
99
 
100
- def wait_for_file_to_complete(file_path, initial_size):
 
 
101
  """
102
- Wait until the file size stops increasing, indicating the download has completed.
 
103
  """
 
104
  while True:
105
  current_size = os.path.getsize(file_path)
106
  if current_size == initial_size:
107
- break
 
 
 
 
 
 
108
  initial_size = current_size
109
- time.sleep(1) # Wait for 1 second before checking again
 
110
 
111
 
112
 
@@ -160,7 +171,8 @@ async def download_file(request: DownloadRequest):
160
  logger.info(f"Downloaded file: {downloaded_file}")
161
 
162
  # Ensure the file is fully written (wait for download to complete)
163
- wait_for_file_to_complete(downloaded_file, initial_size=os.path.getsize(downloaded_file))
 
164
 
165
  # Get the file size
166
  file_size = os.path.getsize(downloaded_file)
 
97
  file_size: int = None # Include file size in the response
98
 
99
 
100
+
101
+
102
+ def wait_for_file_to_complete(file_path, initial_size, max_wait_time=30):
103
  """
104
+ Wait until the file size stops increasing for a set amount of time,
105
+ indicating the download has completed.
106
  """
107
+ wait_time = 0
108
  while True:
109
  current_size = os.path.getsize(file_path)
110
  if current_size == initial_size:
111
+ wait_time += 1
112
+ if wait_time >= max_wait_time:
113
+ # File size hasn't changed for max_wait_time seconds
114
+ break
115
+ else:
116
+ # Reset wait time if the file is still growing
117
+ wait_time = 0
118
  initial_size = current_size
119
+ time.sleep(1) # Check every second
120
+
121
 
122
 
123
 
 
171
  logger.info(f"Downloaded file: {downloaded_file}")
172
 
173
  # Ensure the file is fully written (wait for download to complete)
174
+ initial_size = os.path.getsize(downloaded_file)
175
+ wait_for_file_to_complete(downloaded_file, initial_size)
176
 
177
  # Get the file size
178
  file_size = os.path.getsize(downloaded_file)