mirror of
https://github.com/45Drives/cockpit-navigator.git
synced 2025-07-29 16:45:13 +02:00
fix aborting upload for directories & empty files
This commit is contained in:
parent
d922bdfde0
commit
a837129f0c
@ -19,6 +19,7 @@
|
|||||||
|
|
||||||
import {NavWindow} from "./NavWindow.js";
|
import {NavWindow} from "./NavWindow.js";
|
||||||
import {format_time_remaining} from "../functions.js";
|
import {format_time_remaining} from "../functions.js";
|
||||||
|
import {ModalPrompt} from "./ModalPrompt.js";
|
||||||
|
|
||||||
export class FileUpload {
|
export class FileUpload {
|
||||||
/**
|
/**
|
||||||
@ -40,6 +41,7 @@ export class FileUpload {
|
|||||||
this.chunks = this.slice_file(file);
|
this.chunks = this.slice_file(file);
|
||||||
this.chunk_index = 0;
|
this.chunk_index = 0;
|
||||||
this.timestamp = Date.now();
|
this.timestamp = Date.now();
|
||||||
|
this.modal_prompt = new ModalPrompt();
|
||||||
}
|
}
|
||||||
|
|
||||||
check_if_exists() {
|
check_if_exists() {
|
||||||
@ -130,7 +132,7 @@ export class FileUpload {
|
|||||||
this.proc.fail((e, data) => {
|
this.proc.fail((e, data) => {
|
||||||
this.reader.onload = () => {}
|
this.reader.onload = () => {}
|
||||||
this.done();
|
this.done();
|
||||||
this.nav_window_ref.modal_prompt.alert(data, e);
|
this.nav_window_ref.modal_prompt.alert(e, data);
|
||||||
})
|
})
|
||||||
this.proc.done((data) => {
|
this.proc.done((data) => {
|
||||||
this.nav_window_ref.refresh();
|
this.nav_window_ref.refresh();
|
||||||
@ -147,7 +149,13 @@ export class FileUpload {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
})(this);
|
})(this);
|
||||||
this.reader.readAsArrayBuffer(this.chunks[0]);
|
try {
|
||||||
|
this.reader.readAsArrayBuffer(this.chunks[0]);
|
||||||
|
} catch {
|
||||||
|
this.reader.onload = () => {};
|
||||||
|
this.done();
|
||||||
|
this.modal_prompt.alert("Failed to read file: " + this.filename, "Upload of directories and empty files not supported.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
arrayBufferToBase64(buffer) {
|
arrayBufferToBase64(buffer) {
|
||||||
|
@ -27,16 +27,17 @@ export class NavDragDrop {
|
|||||||
* @param {NavWindow} nav_window_ref
|
* @param {NavWindow} nav_window_ref
|
||||||
*/
|
*/
|
||||||
constructor(drop_area, nav_window_ref) {
|
constructor(drop_area, nav_window_ref) {
|
||||||
drop_area.addEventListener("dragenter", this);
|
drop_area.addEventListener("dragenter", this, false);
|
||||||
drop_area.addEventListener("dragover", this);
|
drop_area.addEventListener("dragover", this, false);
|
||||||
drop_area.addEventListener("dragleave", this);
|
drop_area.addEventListener("dragleave", this, false);
|
||||||
drop_area.addEventListener("drop", this);
|
drop_area.addEventListener("drop", this, false);
|
||||||
this.drop_area = drop_area;
|
this.drop_area = drop_area;
|
||||||
this.nav_window_ref = nav_window_ref;
|
this.nav_window_ref = nav_window_ref;
|
||||||
}
|
}
|
||||||
|
|
||||||
handleEvent(e) {
|
async handleEvent(e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
e.stopPropagation();
|
||||||
switch(e.type){
|
switch(e.type){
|
||||||
case "dragenter":
|
case "dragenter":
|
||||||
this.drop_area.classList.add("drag-enter");
|
this.drop_area.classList.add("drag-enter");
|
||||||
@ -47,45 +48,72 @@ export class NavDragDrop {
|
|||||||
this.drop_area.classList.remove("drag-enter");
|
this.drop_area.classList.remove("drag-enter");
|
||||||
break;
|
break;
|
||||||
case "drop":
|
case "drop":
|
||||||
if (e.dataTransfer.items) {
|
let uploads = [];
|
||||||
for (let item of e.dataTransfer.items) {
|
// console.log(e);
|
||||||
if (item.kind === 'file') {
|
// console.log(e.dataTransfer.files);
|
||||||
var file = item.getAsFile();
|
// if (e.dataTransfer.items) {
|
||||||
if (file.type === "" && file.size !== 0) {
|
// for (let item of e.dataTransfer.items) {
|
||||||
this.nav_window_ref.modal_prompt.alert(file.name + ": Cannot upload folders.");
|
// if (item.kind === 'file') {
|
||||||
continue;
|
// var file = item.getAsFile();
|
||||||
}
|
// if (file.type === "" && file.size !== 0) {
|
||||||
if (file.size === 0) {
|
// await this.nav_window_ref.modal_prompt.alert(file.name + ": Cannot upload folders.");
|
||||||
var proc = cockpit.spawn(
|
// continue;
|
||||||
["/usr/share/cockpit/navigator/scripts/touch.py3", this.nav_window_ref.pwd().path_str() + "/" + file.name],
|
// }
|
||||||
{superuser: "try", err: "out"}
|
// if (file.size === 0) {
|
||||||
);
|
// var proc = cockpit.spawn(
|
||||||
proc.done(() => {
|
// ["/usr/share/cockpit/navigator/scripts/touch.py3", this.nav_window_ref.pwd().path_str() + "/" + file.name],
|
||||||
this.nav_window_ref.refresh();
|
// {superuser: "try", err: "out"}
|
||||||
});
|
// );
|
||||||
proc.fail((e, data) => {
|
// proc.done(() => {
|
||||||
this.nav_window_ref.modal_prompt.alert(data);
|
// this.nav_window_ref.refresh();
|
||||||
});
|
// });
|
||||||
} else {
|
// } else {
|
||||||
var uploader = new FileUpload(file, this.nav_window_ref);
|
// uploads.push(new FileUpload(file, this.nav_window_ref));
|
||||||
uploader.upload();
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
// } else {
|
||||||
|
for (let file of e.dataTransfer.files) {
|
||||||
|
console.log(file);
|
||||||
|
// if (file.type === "")
|
||||||
|
// continue;
|
||||||
|
uploads.push(new FileUpload(file, this.nav_window_ref));
|
||||||
}
|
}
|
||||||
} else {
|
// }
|
||||||
for (let file of ev.dataTransfer.files) {
|
this.drop_area.classList.remove("drag-enter");
|
||||||
if (file.type === "")
|
if (uploads.length === 0)
|
||||||
continue;
|
break;
|
||||||
var uploader = new FileUpload(file, this.nav_window_ref);
|
let keepers = [];
|
||||||
uploader.upload();
|
let requests = {};
|
||||||
|
for (let upload of uploads) {
|
||||||
|
if (!await upload.check_if_exists()) {
|
||||||
|
keepers.push(upload.filename);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let request = {};
|
||||||
|
request.label = upload.filename;
|
||||||
|
request.type = "checkbox";
|
||||||
|
let id = upload.filename;
|
||||||
|
requests[id] = request;
|
||||||
|
}
|
||||||
|
if (Object.keys(requests).length > 0) {
|
||||||
|
let responses = await this.nav_window_ref.modal_prompt.prompt(
|
||||||
|
"Conflicts found while uploading. Replace?",
|
||||||
|
requests
|
||||||
|
)
|
||||||
|
if (responses === null)
|
||||||
|
break;
|
||||||
|
for (let key of Object.keys(responses)) {
|
||||||
|
if (responses[key])
|
||||||
|
keepers.push(key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.drop_area.classList.remove("drag-enter");
|
uploads = uploads.filter((upload) => {return keepers.includes(upload.filename)});
|
||||||
|
uploads.forEach((upload) => {upload.upload()});
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
this.drop_area.classList.remove("drag-enter");
|
this.drop_area.classList.remove("drag-enter");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
e.stopPropagation();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -32,27 +32,29 @@ import sys
|
|||||||
import json
|
import json
|
||||||
|
|
||||||
def write_chunk(chunk, file):
|
def write_chunk(chunk, file):
|
||||||
|
if not file:
|
||||||
|
path = sys.argv[1]
|
||||||
|
try:
|
||||||
|
file = open(path, "wb")
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
sys.exit(1)
|
||||||
seek = chunk["seek"]
|
seek = chunk["seek"]
|
||||||
data = base64.b64decode(chunk["chunk"])
|
data = base64.b64decode(chunk["chunk"])
|
||||||
file.seek(seek)
|
file.seek(seek)
|
||||||
file.write(data)
|
file.write(data)
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
file = None
|
||||||
if len(sys.argv) != 2:
|
if len(sys.argv) != 2:
|
||||||
print("Invalid number of arguments.")
|
print("Invalid number of arguments.")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
path = sys.argv[1]
|
|
||||||
try:
|
|
||||||
file = open(path, "wb")
|
|
||||||
except Exception as e:
|
|
||||||
print(e)
|
|
||||||
sys.exit(1)
|
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
json_in = input()
|
json_in = input()
|
||||||
except EOFError:
|
except EOFError:
|
||||||
break
|
break
|
||||||
json_list = json_in.split("\n")
|
json_list = json_in.split("\n") # need to split in case writes happen faster than reads
|
||||||
for json_obj in json_list:
|
for json_obj in json_list:
|
||||||
try:
|
try:
|
||||||
obj_in = json.loads(json_obj)
|
obj_in = json.loads(json_obj)
|
||||||
@ -63,7 +65,8 @@ def main():
|
|||||||
log.close()
|
log.close()
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
write_chunk(obj_in, file)
|
write_chunk(obj_in, file)
|
||||||
file.close()
|
if file:
|
||||||
|
file.close()
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
Loading…
x
Reference in New Issue
Block a user