Skip to content

Commit

Permalink
Fixed range header. Flush fileobj.
Browse files Browse the repository at this point in the history
  • Loading branch information
jake authored and jake committed Apr 12, 2024
1 parent 6ba9bcf commit 3c6692f
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions internetarchive/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,15 +238,14 @@ def download(# noqa: max-complexity=38
and os.path.exists(file_path.encode('utf-8')):
st = os.stat(file_path.encode('utf-8'))
if st.st_size != self.size and not checksum:
headers = {"Range": f"bytes={st.st_size}-{self.size}"}
headers = {"Range": f"bytes={st.st_size}-"}

response = self.item.session.get(self.url,
stream=True,
timeout=timeout,
auth=self.auth,
params=params,
headers=headers)

# Get timestamp from Last-Modified header
last_mod_header = response.headers.get('Last-Modified')
if last_mod_header:
Expand Down Expand Up @@ -308,18 +307,22 @@ def download(# noqa: max-complexity=38
fileobj = os.fdopen(sys.stdout.fileno(), 'wb', closefd=False)
if not fileobj or retrying:
if 'Range' in headers:
fileobj = open(file_path.encode('utf-8'), 'ab')
fileobj = open(file_path.encode('utf-8'), 'rb+')
else:
fileobj = open(file_path.encode('utf-8'), 'wb')

with fileobj, progress_bar as bar:
if 'Range' in headers:
fileobj.seek(st.st_size)
for chunk in response.iter_content(chunk_size=chunk_size):
if chunk:
size = fileobj.write(chunk)
fileobj.flush()
if bar is not None:
bar.update(size)
if ors:
fileobj.write(os.environ.get("ORS", "\n").encode("utf-8"))
fileobj.flush()

if 'Range' in headers:
with open(file_path, 'rb') as fh:
Expand Down

0 comments on commit 3c6692f

Please sign in to comment.