don't upload file if it already exists

This commit is contained in:
joshuaboud 2021-06-03 14:41:07 -03:00
parent efd09793e8
commit 73e4a09751
No known key found for this signature in database
GPG Key ID: 17EFB59E2A8BF50E
4 changed files with 43 additions and 4 deletions

View File

@ -521,7 +521,6 @@ input:checked + .slider:before {
.nav-notification-header {
position: relative;
z-index: 10;
font-size: 120%;
font-weight: bold;
}

View File

@ -854,6 +854,14 @@ class FileUpload {
this.chunk_index = 0;
}
check_if_exists() {
return new Promise((resolve, reject) => {
var proc = cockpit.spawn(["/usr/share/cockpit/navigator/scripts/fail-if-exists.py", this.path], {superuser: "try"});
proc.done((data) => {resolve(false)});
proc.fail((e, data) => {resolve(true)});
});
}
make_html_element() {
var notification = document.createElement("div");
notification.classList.add("nav-notification");
@ -892,14 +900,20 @@ class FileUpload {
return chunks;
}
upload() {
async upload() {
if (await this.check_if_exists()) {
window.alert(this.filename + ": File exists.");
return;
}
this.make_html_element();
this.proc = cockpit.spawn(["/usr/share/cockpit/navigator/scripts/write-chunks.py", this.path], {err: "out", superuser: "try"});
this.proc.fail((e, data) => {
this.reader.onload = () => {}
this.done();
window.alert(data);
})
this.proc.done((data) => {
this.nav_window_ref.stop_load();
})
this.reader.onload = (function(uploader_ref) {
return async function(evt) {

View File

@ -0,0 +1,26 @@
#!/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/>.
"""
import os
import sys
if os.path.exists(sys.argv[1]):
sys.exit(1)
sys.exit(0)

View File

@ -34,7 +34,7 @@ def main():
sys.exit(1)
path = sys.argv[1]
try:
file = open(path, "a+b")
file = open(path, "xb")
except Exception as e:
print(e)
sys.exit(1)