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
|
return missingFiles
|
||||||
|
|
||||||
def create_file_input(browser):
|
def create_file_input(browser, userId):
|
||||||
javascript = """const fileInput = document.createElement("input");
|
javascript = """const fileInput = document.createElement("input");
|
||||||
fileInput.id = "upload";
|
fileInput.id = "upload";
|
||||||
fileInput.type = "file";
|
fileInput.type = "file";
|
||||||
|
@ -70,9 +70,53 @@ fileInput.multiple = true;
|
||||||
|
|
||||||
fileInput.addEventListener("change", () => {
|
fileInput.addEventListener("change", () => {
|
||||||
for (const file of fileInput.files) {
|
for (const file of fileInput.files) {
|
||||||
console.log(file); // Debug
|
|
||||||
// Miro.request(file); // What I'll probably actually use (or wrapper method)
|
const xhr = new XMLHttpRequest();
|
||||||
//addFile(file); // Original (token not accessible from minified code)
|
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;
|
fileInput.value = null;
|
||||||
});
|
});
|
||||||
|
@ -88,9 +132,9 @@ def get_user_id(browser):
|
||||||
return userId
|
return userId
|
||||||
|
|
||||||
def upload_files(browser, directory, missingFiles):
|
def upload_files(browser, directory, missingFiles):
|
||||||
create_file_input(browser)
|
|
||||||
uploadInput = browser.find_element(By.ID, "upload")
|
|
||||||
userId = get_user_id(browser)
|
userId = get_user_id(browser)
|
||||||
|
create_file_input(browser, userId)
|
||||||
|
uploadInput = browser.find_element(By.ID, "upload")
|
||||||
for file in missingFiles:
|
for file in missingFiles:
|
||||||
uploadInput.send_keys(directory + file)
|
uploadInput.send_keys(directory + file)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue