From 8597fd1b495db7f0ecd29d3e870d70d5ac17d2cd Mon Sep 17 00:00:00 2001 From: The Magician Date: Mon, 20 Nov 2023 20:51:39 +0000 Subject: [PATCH] Add javascript to make upload request --- jardin.py | 56 +++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 50 insertions(+), 6 deletions(-) diff --git a/jardin.py b/jardin.py index db76df2..b9e6dc4 100755 --- a/jardin.py +++ b/jardin.py @@ -62,7 +62,7 @@ def get_missing_filenames(localFiles, gardenFiles): return missingFiles -def create_file_input(browser): +def create_file_input(browser, userId): javascript = """const fileInput = document.createElement("input"); fileInput.id = "upload"; fileInput.type = "file"; @@ -70,9 +70,53 @@ fileInput.multiple = true; fileInput.addEventListener("change", () => { for (const file of fileInput.files) { - console.log(file); // Debug - // Miro.request(file); // What I'll probably actually use (or wrapper method) - //addFile(file); // Original (token not accessible from minified code) + + const xhr = new XMLHttpRequest(); + xhr.withCredentials = true; + xhr.responseType = "text"; + + xhr.open("POST", "/users/""" + userId + """/pipe", true); + + const data = { + parent: null, + name: file.name + }; + xhr.setRequestHeader("Content-Type", "application/octet-stream"); + xhr.setRequestHeader("X-Data", encodeURI(JSON.stringify(data))); + + xhr.send(file); + + /* + Miro.request( + "POST", + "/users/""" + userId + """/pipe", + {"Content-Type": "application/octet-stream"}, + file, + function(xhr) { + console.log("Started uploading " + file.name + "..."); + + xhr.addEventListener("readystatechange", function() { + if (xhr.readyState == XMLHttpRequest.OPENED) { + const data = { + parent: null, + name: file.name + }; + + xhr.setRequestHeader("Content-Type", "application/octet-stream"); + xhr.setRequestHeader("X-Data", encodeURI(JSON.stringify(data))); + } + }); + }, + true) + .then(Miro.response( + function(xhr) { + console.log("Uploaded " + file.name); + }), + function(xhr, error) { + console.log("Failed to upload " + file.name + " due to error:"); + console.log(error); + }); + */ } fileInput.value = null; }); @@ -88,9 +132,9 @@ def get_user_id(browser): return userId def upload_files(browser, directory, missingFiles): - create_file_input(browser) - uploadInput = browser.find_element(By.ID, "upload") userId = get_user_id(browser) + create_file_input(browser, userId) + uploadInput = browser.find_element(By.ID, "upload") for file in missingFiles: uploadInput.send_keys(directory + file)