ver Dec25th

added cache and upload function for setup
This commit is contained in:
David Chang
2023-12-25 14:40:30 +08:00
parent 2163a08a0d
commit 82e3353f65
7 changed files with 107 additions and 49 deletions

View File

@@ -1,12 +1,22 @@
import requests
import json
from requests_toolbelt.multipart.encoder import MultipartEncoder
import uuid
import os.path
from typing import Dict, List
from typing import Any
class SetupController:
def __init__(self, http_server: str):
def __init__( self
, http_server: str
, cache_dir: str
):
self.http_server = http_server + "/setup"
self.cache_dir: str = cache_dir
def reset_cache_dir(self, cache_dir: str):
self.cache_dir = cache_dir
def setup(self, config: List[Dict[str, Any]]):
"""
@@ -55,22 +65,56 @@ class SetupController:
for f in files:
url: str = f["url"]
path: str = f["path"]
cache_path: str = os.path.join( self.cache_dir
, "{:}_{:}".format(
uuid.uuid5(uuid.NAMESPACE_URL, url)
, os.path.basename(path)
)
)
if not url or not path:
raise Exception(f"Setup Download - Invalid URL ({url}) or path ({path}).")
payload = json.dumps({"url": url, "path": path})
headers = {
'Content-Type': 'application/json'
}
if not os.path.exists(cache_path):
max_retries = 3
downloaded = False
for i in range(max_retries):
try:
response = requests.get(url, stream=True, verify=False)
response.raise_for_status()
# send request to server to download file
with open(cache_path, 'wb') as f:
for chunk in response.iter_content(chunk_size=8192):
if chunk:
f.write(chunk)
print("File downloaded successfully")
downloaded = True
break
except requests.RequestException as e:
print(f"Failed to download {url}. Retrying... ({max_retries - i - 1} attempts left)")
if not downloaded:
raise requests.RequestException(f"Failed to download {url}. No retries left. Error: {e}")
#payload = json.dumps({"url": url, "path": path})
#headers = {
#'Content-Type': 'application/json'
#}
form = MultipartEncoder( { "file_path": path
, "file_data": (os.path.basename(path), open(cache_path, "rb"))
}
)
headers = {"Content-Type": form.content_type}
print(form.content_type)
# send request to server to upload file
try:
response = requests.post(self.http_server + "/download_file", headers=headers, data=payload)
response = requests.post(self.http_server + "/upload", headers=headers, data=form)
if response.status_code == 200:
print("Command executed successfully:", response.text)
else:
print("Failed to download file. Status code:", response.text)
print("Failed to upload file. Status code:", response.text)
except requests.exceptions.RequestException as e:
print("An error occurred while trying to send the request:", e)