better upload method

This commit is contained in:
MatMasIt 2022-01-31 09:20:58 +01:00 committed by GitHub
parent 8326a75a85
commit 3b3308a8cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -14,32 +14,42 @@ def vprint(data):
global verbose global verbose
if verbose: if verbose:
print(data) print(data)
# HERE IS OUR GENERATOR
def read_in_chunks(file_object, CHUNK_SIZE=1024):
while True:
data = file_object.read(CHUNK_SIZE)
if not data:
break
yield data
def upload(csvPath): def upload(csvPath):
global serverurl global serverurl
global auth global auth
vprint("Generating nonce uuid") vprint("Generating nonce uuid")
uid=str(uuid.uuid4()) uid = uuid.uuid4()
data=auth+"\n"+uid+"\nBEGIN" content_name = str(csvPath)
requests.post(serverurl, data=data) content_path = os.path.abspath(file)
vprint("Sent BEGIN") content_size = os.stat(content_path).st_size
accumulator="" print(content_name, content_path, content_size)
index=0
for line in open(csvPath):
if index==0:
data=auth+"\n"+uid+"\nWRITE\n"
accumulator=data
accumulator+=line
index+=1
if index>chunkRowNum:
vprint("Sent WRITE")
requests.post(serverurl, data=accumulator)
index=0
requests.post(serverurl, data=accumulator)
vprint("Sent WRITE")
data=auth+"\n"+uid+"\nCONCLUDE"
requests.post(serverurl, data=data)
vprint("Sent CONCLUDE")
file_object = open(content_path, "rb")
index = 0
offset = 0
headers = {}
for chunk in read_in_chunks(file_object, CHUNK_SIZE):
offset = index + len(chunk)
headers['Content-Range'] = 'bytes %s-%s/%s' % (index, offset - 1, content_size)
headers['Authorization'] = auth
headers['X-Nonce'] = uid
index = offset
try:
file = {"file": chunk}
r = requests.post(serverUrl, files=file, headers=headers)
print(r.text)
print("r: %s, Content-Range: %s" % (r, headers['Content-Range']))
except Exception as e:
print(e)
if not storage.exists("lastUpdate"): if not storage.exists("lastUpdate"):
vprint("Last database update record has been initialized") vprint("Last database update record has been initialized")
storage.save("lastUpdate",0) storage.save("lastUpdate",0)