Skip to content

Commit

Permalink
[#42409] Resume the download after connection reset
Browse files Browse the repository at this point in the history
  • Loading branch information
GPlaczek authored and AdamOlech committed Mar 7, 2023
1 parent 66b1cfa commit 76198e0
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions dockersave/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#!/usr/bin/env python3

import requests, os, json, hashlib, tarfile, shutil, subprocess
from os.path import join
from os.path import join, exists
from requests.auth import HTTPBasicAuth
from dockersave.exceptions import UnsupportedManifest
import time

json_template = {
"id": "",
Expand Down Expand Up @@ -106,17 +107,34 @@ def get_blob(image, digest, token, registry_url):

return r

def save_blob_chunked(image, digest, filename, token, registry_url):
def save_blob_chunked(image, digest, filename, token, registry_url, content_range=0, retries=0):
request_url = "{}/v2/{}/blobs/{}".format(registry_url, image, digest)
if retries > 20:
raise ConnectionError("Too many connection retries")

headers = {'Authorization':'Bearer {}'.format(token)}
i = content_range
headers = {
'Authorization':'Bearer {}'.format(token),
'Range': 'bytes={}-'.format(content_range),
}

with requests.get(request_url, headers = headers, stream=True) as r:
r.raise_for_status()
with open(filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=8192):
if chunk:
f.write(chunk)
if exists(filename) and content_range == 0:
mode = 'wb'
else:
mode = 'ab'
with open(filename, mode) as f:
try:
for chunk in r.iter_content(chunk_size=8192):
if chunk:
i += len(chunk)
f.write(chunk)
except requests.models.ProtocolError:
time.sleep(5)
retries += 1
save_blob_chunked(image, digest, filename, token, registry_url, i, retries)
return

def sha256(string):
return hashlib.sha256(string.encode()).hexdigest()
Expand Down

0 comments on commit 76198e0

Please sign in to comment.