From 73e4a09751de9c82ccc62eaa45ea8c61d1719b13 Mon Sep 17 00:00:00 2001 From: joshuaboud Date: Thu, 3 Jun 2021 14:41:07 -0300 Subject: [PATCH] don't upload file if it already exists --- navigator/navigator.css | 1 - navigator/navigator.js | 18 ++++++++++++++++-- navigator/scripts/fail-if-exists.py | 26 ++++++++++++++++++++++++++ navigator/scripts/write-chunks.py | 2 +- 4 files changed, 43 insertions(+), 4 deletions(-) create mode 100755 navigator/scripts/fail-if-exists.py diff --git a/navigator/navigator.css b/navigator/navigator.css index 769a562..5a8821c 100644 --- a/navigator/navigator.css +++ b/navigator/navigator.css @@ -521,7 +521,6 @@ input:checked + .slider:before { .nav-notification-header { position: relative; z-index: 10; - font-size: 120%; font-weight: bold; } diff --git a/navigator/navigator.js b/navigator/navigator.js index 55ac8fe..0201cfe 100644 --- a/navigator/navigator.js +++ b/navigator/navigator.js @@ -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) { diff --git a/navigator/scripts/fail-if-exists.py b/navigator/scripts/fail-if-exists.py new file mode 100755 index 0000000..39cdcf6 --- /dev/null +++ b/navigator/scripts/fail-if-exists.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 + +""" + Cockpit Navigator - A File System Browser for Cockpit. + Copyright (C) 2021 Josh Boudreau + + 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 . +""" + +import os +import sys + +if os.path.exists(sys.argv[1]): + sys.exit(1) + +sys.exit(0) diff --git a/navigator/scripts/write-chunks.py b/navigator/scripts/write-chunks.py index 6cde13d..79b7647 100755 --- a/navigator/scripts/write-chunks.py +++ b/navigator/scripts/write-chunks.py @@ -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)