Add javascript to make upload request
This commit is contained in:
parent
83db84091f
commit
8597fd1b49
56
jardin.py
56
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)
|
||||
|
||||
|
|
Loading…
Reference in New Issue