check if paths exist all at once

This commit is contained in:
joshuaboud 2021-07-19 16:03:26 -03:00
parent 5ee43047f9
commit dffe7b00ce
No known key found for this signature in database
GPG Key ID: 17EFB59E2A8BF50E
2 changed files with 68 additions and 2 deletions

View File

@ -98,10 +98,30 @@ export class NavDragDrop {
* @returns {FileUpload[]}
*/
async handle_conflicts(uploads) {
let test_paths = [];
for (let upload of uploads)
test_paths.push(upload.path);
let proc = cockpit.spawn(
["/usr/share/cockpit/navigator/scripts/return-exists.py3", ... test_paths],
{error: "out", superuser: "try"}
);
let exist_result;
proc.done((data) => {
exist_result = JSON.parse(data);
});
proc.fail((e, data) => {
this.nav_window_ref.modal_prompt.alert(e, data);
});
try {
await proc;
} catch {
return;
}
console.log(exist_result);
let keepers = [];
let requests = {};
for (let upload of uploads) {
if (!await check_if_exists(upload.path)) {
if (!exist_result[upload.path]) {
keepers.push(upload.filename);
continue;
}
@ -163,8 +183,10 @@ export class NavDragDrop {
}
}
this.drop_area.classList.remove("drag-enter");
if (uploads.length === 0)
if (uploads.length === 0) {
this.nav_window_ref.stop_load();
break;
}
uploads = await this.handle_conflicts(uploads);
this.nav_window_ref.stop_load();
uploads.forEach((upload) => {upload.upload()});

View File

@ -0,0 +1,44 @@
#!/usr/bin/env python3
"""
Cockpit Navigator - A File System Browser for Cockpit.
Copyright (C) 2021 Josh Boudreau <jboudreau@45drives.com>
This file is part of Cockpit Navigator.
Cockpit Navigator is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Cockpit Navigator is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Cockpit Navigator. If not, see <https://www.gnu.org/licenses/>.
"""
"""
Synopsys: return-exists.py3 /full/path1 [/full/path2 ...]
replys with JSON formatted dictionary of path : boolean where true means the file exists
"""
import os
import sys
import json
def main():
argv = sys.argv
argc = len(sys.argv)
if argc <= 1:
print("No arguments provided")
sys.exit(1)
response = {}
for i in range (1, argc):
path = argv[i]
response[path] = os.path.lexists(path)
print(json.dumps(response))
sys.exit(0)
if __name__ == "__main__":
main()