HTTP upload : chunks

This commit is contained in:
ABelliqueux 2022-12-14 15:01:43 +01:00
parent 0d2eee5f25
commit 7e0d217cf3
2 changed files with 45 additions and 15 deletions

56
app.py
View File

@ -208,24 +208,52 @@ def get_upload_candidate_list(host_local, port, media_list):
print("Response not ok !") print("Response not ok !")
return [] return []
def read_in_chunks(file_object, chunk_size=102400):
while True:
data = file_object.read(chunk_size)
if not data:
break
yield data
def HTTP_upload(file_dict, host_local, port): def HTTP_upload(file_dict, host_local, port):
''' '''
Build HTTP file upload request and send it. Build HTTP file upload request and send it.
https://stackoverflow.com/questions/43383823/python-flask-chunk-data-upload-not-working/70894476#70894476
''' '''
url = "https://" + host_local + ":" + str(port) + "/upload" global current_upload
http_headers_data_mime = http_headers.copy() http_headers_data_mime = http_headers.copy()
http_headers_data_mime["content-type"] = "" http_headers_data_mime["content-type"] = ""
files = {"file": (file_dict["filename"], open(media_folder_local + file_dict["filename"], "rb"), "multipart/form-data")} file_path = os.path.join(media_folder_local, file_dict["filename"])
if debug: part_size = int(file_dict["size"] / 10)
print(files) if part_size < 102400:
response = requests.post(url, files=files, headers=http_headers, verify=CAfile) part_size = 102400
if debug: url = "https://" + host_local + ":" + str(port) + "/upload/" + file_dict["filename"] + "/" + str(part_size)
print(response.text) with open(file_path, "rb") as ul_file:
if response.ok: try:
return 1 for data in read_in_chunks(ul_file, chunk_size=part_size):
else: print(len(data))
return 0 # ~ files = {"file": (file_dict["filename"], data, "multipart/form-data")}
# ~ files = {"file": (file_dict["filename"], "multipart/form-data")}
# ~ response = requests.post(url, files=files, headers=http_headers, verify=CAfile)
response = requests.post(url, data=data, headers=http_headers, verify=CAfile)
# ~ response = requests.post(url, data=data, headers=http_headers, verify=CAfile)
if debug:
print(response.text)
transferred_mb = len(data) / 1024 / 1024
current_upload["transferred_size"] += round(transferred_mb)
current_upload["transferred_percent"] += round(100 / current_upload["total_size"] * transferred_mb)
except Exception as e:
print(e)
# ~ files = {"file": (file_dict["filename"], open(media_folder_local + file_dict["filename"], "rb"), "multipart/form-data")}
# ~ if debug:
# ~ print(files)
# ~ response = requests.post(url, files=files, headers=http_headers, verify=CAfile)
# ~ if debug:
# ~ print(response.text)
# ~ if response.ok:
# ~ return 1
# ~ else:
# ~ return 0
def list_media_files(folder): def list_media_files(folder):
@ -315,8 +343,8 @@ def sync_media_folder(media_folder_local, media_folder_remote, host_local, port,
if debug: if debug:
print("File size: " + str(round(current_media_size))) print("File size: " + str(round(current_media_size)))
media_count += 1 media_count += 1
current_upload["transferred_size"] += round(current_media_size) # ~ current_upload["transferred_size"] += round(current_media_size)
current_upload["transferred_percent"] += round((100 / total_size) * current_media_size) # ~ current_upload["transferred_percent"] += round((100 / total_size) * current_media_size)
else: else:
# Upload interrupted # Upload interrupted
return _("Upload interrupted") return _("Upload interrupted")
@ -502,7 +530,7 @@ def send_pilpil_command(host, arg0, arg1, arg2):
HTTP_request = HTTP_request + "&val=" + arg1 HTTP_request = HTTP_request + "&val=" + arg1
elif (arg0 == "enqueue") or (arg0 == "add"): elif (arg0 == "enqueue") or (arg0 == "add"):
# Add 'input' url parameter # Add 'input' url parameter
HTTP_request = HTTP_request + "&input=file://" + quote(media_folder_remote_expanded) + "/" + arg1 HTTP_request = HTTP_request + "&input=file://" + os.path.join(quote(media_folder_remote_expanded),arg1)
# Send request and get data response # Send request and get data response
data = send_HTTP_request(host, port_, time_out=3, request_=HTTP_request) data = send_HTTP_request(host, port_, time_out=3, request_=HTTP_request)

View File

@ -70,11 +70,13 @@ sha256 : 401359a84c6d60902c05602bd52fae70f0b2ecac36d550b52d14e1e3230854a6
# DOING NEXT : # DOING NEXT :
* server : remove incomplete files before upload
* webgui/server : interrupt current download ? * webgui/server : interrupt current download ?
* webgui : upload status CSS transition ?
# DONE : # DONE :
* server : upload in chunks
* webgui: remove file from timeline (drag&drop to bin?) (check bugs) * webgui: remove file from timeline (drag&drop to bin?) (check bugs)
* webgui: upload progress dialog * webgui: upload progress dialog
* server : file upload : only send new files, get upload status (/sync/status) * server : file upload : only send new files, get upload status (/sync/status)