Skip to content

Commit 4fb7d23

Browse files
committed
[common] fix a long-standing bug that causes infinite downloading when content-length is missing
1 parent e9165e0 commit 4fb7d23

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/you_get/common.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,8 @@ def numreturn(a):
807807
except socket.timeout:
808808
pass
809809
if not buffer:
810+
if file_size == float('+inf'): # Prevent infinite downloading
811+
break
810812
if is_chunked and received_chunk == range_length:
811813
break
812814
elif not is_chunked and received == file_size: # Download finished
@@ -827,9 +829,10 @@ def numreturn(a):
827829
received, os.path.getsize(temp_filepath), temp_filepath
828830
)
829831

830-
if os.access(filepath, os.W_OK):
832+
if os.access(filepath, os.W_OK) and file_size != float('inf'):
831833
# on Windows rename could fail if destination filepath exists
832-
os.remove(filepath)
834+
# we should simply choose a new name instead of brutal os.remove(filepath)
835+
filepath = filepath + " (2)"
833836
os.rename(temp_filepath, filepath)
834837

835838

0 commit comments

Comments
 (0)