mirror of
https://github.com/45Drives/cockpit-navigator.git
synced 2025-07-31 01:24:37 +02:00
implement zipping multiple files for download
This commit is contained in:
parent
2c197d7b50
commit
e36a0995fa
@ -841,8 +841,41 @@ class NavContextMenu {
|
|||||||
this.nav_window_ref.refresh();
|
this.nav_window_ref.refresh();
|
||||||
}
|
}
|
||||||
|
|
||||||
download() {
|
zip_for_download() {
|
||||||
var download = new NavDownloader(this.target);
|
return new Promise((resolve, reject) => {
|
||||||
|
var cmd = [
|
||||||
|
"/usr/share/cockpit/navigator/scripts/zip-for-download.py",
|
||||||
|
this.nav_window_ref.pwd().path_str()
|
||||||
|
];
|
||||||
|
for (let entry of this.nav_window_ref.selected_entries) {
|
||||||
|
cmd.push(entry.path_str());
|
||||||
|
}
|
||||||
|
var proc = cockpit.spawn(cmd, {superuser: "try", err: "out"});
|
||||||
|
proc.fail((e, data) => {
|
||||||
|
reject(JSON.parse(data));
|
||||||
|
});
|
||||||
|
proc.done((data) => {
|
||||||
|
resolve(JSON.parse(data));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async download() {
|
||||||
|
var download_target = "";
|
||||||
|
if (this.nav_window_ref.selected_entries.size === 1 && !this.target instanceof NavDir) {
|
||||||
|
download_target = this.target;
|
||||||
|
} else {
|
||||||
|
this.nav_window_ref.start_load();
|
||||||
|
var result;
|
||||||
|
try {
|
||||||
|
result = await this.zip_for_download();
|
||||||
|
} catch(e) {
|
||||||
|
window.alert(e.message);
|
||||||
|
}
|
||||||
|
this.nav_window_ref.stop_load();
|
||||||
|
download_target = new NavFile(result["archive-path"], result["stat"], this.nav_window_ref);
|
||||||
|
}
|
||||||
|
var download = new NavDownloader(download_target);
|
||||||
download.download();
|
download.download();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -866,6 +899,7 @@ class NavContextMenu {
|
|||||||
} else {
|
} else {
|
||||||
this.nav_window_ref.set_selected(target, false, false);
|
this.nav_window_ref.set_selected(target, false, false);
|
||||||
}
|
}
|
||||||
|
this.menu_options["download"].style.display = "flex";
|
||||||
if (target === this.nav_window_ref.pwd()) {
|
if (target === this.nav_window_ref.pwd()) {
|
||||||
this.menu_options["copy"].style.display = "none";
|
this.menu_options["copy"].style.display = "none";
|
||||||
this.menu_options["cut"].style.display = "none";
|
this.menu_options["cut"].style.display = "none";
|
||||||
@ -877,12 +911,9 @@ class NavContextMenu {
|
|||||||
}
|
}
|
||||||
if (this.nav_window_ref.selected_entries.size > 1) {
|
if (this.nav_window_ref.selected_entries.size > 1) {
|
||||||
this.menu_options["rename"].style.display = "none";
|
this.menu_options["rename"].style.display = "none";
|
||||||
this.menu_options["download"].style.display = "none";
|
|
||||||
} else {
|
} else {
|
||||||
this.menu_options["rename"].style.display = "flex";
|
this.menu_options["rename"].style.display = "flex";
|
||||||
if (target instanceof NavFile)
|
if (target instanceof NavFileLink)
|
||||||
this.menu_options["download"].style.display = "flex";
|
|
||||||
else
|
|
||||||
this.menu_options["download"].style.display = "none";
|
this.menu_options["download"].style.display = "none";
|
||||||
}
|
}
|
||||||
this.target = target;
|
this.target = target;
|
||||||
|
101
navigator/scripts/zip-for-download.py
Executable file
101
navigator/scripts/zip-for-download.py
Executable file
@ -0,0 +1,101 @@
|
|||||||
|
#!/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/>.
|
||||||
|
"""
|
||||||
|
|
||||||
|
"""
|
||||||
|
Synopsis: `zip-for-download.py </path/to/cwd> </path/to/file> [</path/to/file> ...]`
|
||||||
|
Output is JSON object with form:
|
||||||
|
{
|
||||||
|
message: <error message if applicable>,
|
||||||
|
archive-path: </path/to/archive>,
|
||||||
|
stat: {
|
||||||
|
size: <size of archive in bytes> // for setting channel max read size
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
import subprocess
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
def get_relpaths(full_paths, cwd):
|
||||||
|
response = []
|
||||||
|
for path in full_paths:
|
||||||
|
response.append(os.path.relpath(path, cwd))
|
||||||
|
return response
|
||||||
|
|
||||||
|
def make_zip(path):
|
||||||
|
try:
|
||||||
|
cwd = sys.argv[1]
|
||||||
|
files = get_relpaths(sys.argv[2:], cwd)
|
||||||
|
os.chdir(cwd)
|
||||||
|
except Exception as e:
|
||||||
|
print(json.dumps({
|
||||||
|
"message": e
|
||||||
|
}))
|
||||||
|
sys.exit(1)
|
||||||
|
cmd = ["zip", "-r", "--symlinks", path, *files]
|
||||||
|
try:
|
||||||
|
child = subprocess.Popen(
|
||||||
|
cmd,
|
||||||
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
print(json.dumps({
|
||||||
|
"message": e
|
||||||
|
}))
|
||||||
|
sys.exit(1)
|
||||||
|
child.wait()
|
||||||
|
if child.returncode:
|
||||||
|
stdout, stderr = child.communicate()
|
||||||
|
print(json.dumps({
|
||||||
|
"message": stdout + stderr
|
||||||
|
}))
|
||||||
|
sys.exit(child.returncode)
|
||||||
|
try:
|
||||||
|
archive_size = os.stat(path).st_size
|
||||||
|
except Exception as e:
|
||||||
|
print(json.dumps({
|
||||||
|
"message": e
|
||||||
|
}))
|
||||||
|
sys.exit(1)
|
||||||
|
print(json.dumps({
|
||||||
|
"message": "",
|
||||||
|
"archive-path": path,
|
||||||
|
"stat": {
|
||||||
|
"size": archive_size
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
|
||||||
|
def main():
|
||||||
|
tmp_dir = "/tmp/navigator"
|
||||||
|
if not os.path.exists(tmp_dir):
|
||||||
|
os.mkdir(tmp_dir)
|
||||||
|
elif not os.path.isdir(tmp_dir):
|
||||||
|
print(json.dumps({
|
||||||
|
"message": "Temp path already exists."
|
||||||
|
}))
|
||||||
|
sys.exit(1)
|
||||||
|
archive_path = tmp_dir + "/navigator-download_" + datetime.now().strftime("%Y-%m-%d_%H-%M-%S.%f") + ".zip"
|
||||||
|
make_zip(archive_path)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
Loading…
x
Reference in New Issue
Block a user