From 86ae87ebee1cce1a152666474677d7a1f0aeb0f7 Mon Sep 17 00:00:00 2001 From: Dave Shoreman Date: Fri, 5 May 2023 23:39:33 +0100 Subject: [PATCH 1/4] Prompt before overwriting files --- CHANGELOG.md | 2 ++ src/_nextcloud.bash | 29 ++++++++++++++++++++++++----- src/main.bash | 5 +++-- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 111c7a3..9290388 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added +* NextCloud uploads can now be aborted if they would overwrite an existing file ## [1.4.4] - 2023-05-05 diff --git a/src/_nextcloud.bash b/src/_nextcloud.bash index 0a7e48a..dace35f 100644 --- a/src/_nextcloud.bash +++ b/src/_nextcloud.bash @@ -22,17 +22,36 @@ make_url() { || echo "${server}/index.php${*}" } +nc_overwrite_check() { + local reqUrl status + + echo "Checking for file on Nextcloud..." >&2 + reqUrl="$(make_url "remote.php/dav/files/${username}/${savedir}/${1// /%20}")" + status="$(curl -u "$username":"$password" "$reqUrl" -Lw "%{http_code}" -X PROPFIND -so/dev/null)" + + if [ ! "$status" = 404 ]; then + echo "File already exists! Continue with upload? [yN]" >&2 + read -rn1 proceed && echo >&2 + + if [[ ! "${proceed,,}" =~ ^(y|yes)$ ]]; then + echo "Upload cancelled!" >&2 && exit 1 + fi + fi + + echo "$1" +} + nc_upload() { - local filename output respCode reqUrl url; read -r filename + local filename output proceed respCode reqUrl url; read -r filename echo -e "\nUploading screenshot..." >&2 - reqUrl="$(make_url "remote.php/dav/files/${username}/${savedir}/${filename// /%20}")" + reqUrl="$(make_url "remote.php/dav/files/${username}/${savedir}/${1// /%20}")" [ "$debug" = true ] && output="$_CACHE_DIR/curlout" || output=/dev/null [ "$debug" = true ] && echo "Sending request to ${reqUrl}..." >&2 - respCode=$(curl -u "$username":"$password" "$reqUrl" \ - -L --post301 --upload-file "$_CACHE_DIR/$filename" -#o "$output" -w "%{http_code}") + respCode=$(curl -u "$username":"$password" "$reqUrl" -Lw "%{http_code}" \ + --post301 --upload-file "$_CACHE_DIR/$filename" -# -o "$output") if [ "$respCode" = 204 ]; then [ "$debug" = true ] && echo "Expected 201 but server returned a 204 response" >&2 @@ -43,7 +62,7 @@ nc_upload() { echo "Upload failed. Expected 201 but server returned a $respCode response" >&2 && exit 1 fi - url="$(make_url "/apps/gallery/#${savedir}/${filename}")" + url="$(make_url "/apps/gallery/#${savedir}/${1}")" echo "Screenshot uploaded to ${url// /%20}" >&2 echo "$filename" } diff --git a/src/main.bash b/src/main.bash index c9db549..1238415 100755 --- a/src/main.bash +++ b/src/main.bash @@ -106,7 +106,7 @@ usage() { } main() { - local debug=false image filename json url + local debug=false image filename json ncfilename url output_mode="nextcloud" check_bash_version && setup @@ -126,7 +126,8 @@ main() { to_clipboard image < "$_CACHE_DIR/$image" && \ send_notification "Your image is ready to paste!" else - filename="$(echo "$image" | nc_upload)" + ncfilename="$(nc_overwrite_check "$image")" + filename="$(echo "$image" | nc_upload "$ncfilename")" json=$(nc_share "$filename") url="$(echo "$json" | make_share_url)" From ba31578ce91c76b1b5c8a5f33102c7776b5182c0 Mon Sep 17 00:00:00 2001 From: Dave Shoreman Date: Sat, 6 May 2023 14:37:33 +0100 Subject: [PATCH 2/4] Add overwrite prompt for GUI flow --- src/_nextcloud.bash | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/_nextcloud.bash b/src/_nextcloud.bash index dace35f..3f1690c 100644 --- a/src/_nextcloud.bash +++ b/src/_nextcloud.bash @@ -23,22 +23,30 @@ make_url() { } nc_overwrite_check() { - local reqUrl status + local line1 line2 reqUrl status echo "Checking for file on Nextcloud..." >&2 reqUrl="$(make_url "remote.php/dav/files/${username}/${savedir}/${1// /%20}")" status="$(curl -u "$username":"$password" "$reqUrl" -Lw "%{http_code}" -X PROPFIND -so/dev/null)" - if [ ! "$status" = 404 ]; then + if [ "$status" = 404 ]; then + echo "$1" && return + elif is_interactive; then echo "File already exists! Continue with upload? [yN]" >&2 read -rn1 proceed && echo >&2 - if [[ ! "${proceed,,}" =~ ^(y|yes)$ ]]; then - echo "Upload cancelled!" >&2 && exit 1 + if [[ "${proceed,,}" =~ ^(y|yes)$ ]]; then + echo "$1" && return + fi + elif has yad; then + line1="The file '$1' already exists on NextCloud! If you continue, it will be overwritten." + line2="Proceed with upload?" + if yad --title "NextCloud File Conflict" --text "\n${line1}\n\n${line2}"; then + echo "$1" && return fi fi - echo "$1" + echo "Upload cancelled!" >&2 && exit 1 } nc_upload() { From 46c75a288251237d31f1b1139a1608dd6e78f824 Mon Sep 17 00:00:00 2001 From: Dave Shoreman Date: Sun, 7 May 2023 00:14:03 +0100 Subject: [PATCH 3/4] Add ability to rename conflicting files --- CHANGELOG.md | 2 +- src/_nextcloud.bash | 47 ++++++++++++++++++++++++++++++++++----------- 2 files changed, 37 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9290388..ce5f40f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Added -* NextCloud uploads can now be aborted if they would overwrite an existing file +* NextCloud uploads can now be aborted or renamed if they would overwrite another file ## [1.4.4] - 2023-05-05 diff --git a/src/_nextcloud.bash b/src/_nextcloud.bash index 3f1690c..48512c1 100644 --- a/src/_nextcloud.bash +++ b/src/_nextcloud.bash @@ -23,7 +23,7 @@ make_url() { } nc_overwrite_check() { - local line1 line2 reqUrl status + local line1 line2 newname proceed reqUrl status echo "Checking for file on Nextcloud..." >&2 reqUrl="$(make_url "remote.php/dav/files/${username}/${savedir}/${1// /%20}")" @@ -32,17 +32,42 @@ nc_overwrite_check() { if [ "$status" = 404 ]; then echo "$1" && return elif is_interactive; then - echo "File already exists! Continue with upload? [yN]" >&2 - read -rn1 proceed && echo >&2 - - if [[ "${proceed,,}" =~ ^(y|yes)$ ]]; then - echo "$1" && return - fi + echo "File '$1' already exists!" >&2 + + while true; do case "$proceed" in + a|A) + break ;; #noop + r|R) + while [ -z "$newname" ]; do + echo -n " New filename: " >&2 && read -r newname + done + + nc_overwrite_check "$newname" && return ;; + o|O) + echo "$1" && return ;; + *) + echo -n " Press 'a' to abort, 'r' to rename, or 'o' to overwrite: " >&2 + read -rn1 proceed && echo >&2 ;; + esac; done elif has yad; then - line1="The file '$1' already exists on NextCloud! If you continue, it will be overwritten." - line2="Proceed with upload?" - if yad --title "NextCloud File Conflict" --text "\n${line1}\n\n${line2}"; then - echo "$1" && return + line1="The file $1 already exists on NextCloud!" + line2="How would you like to proceed?" + + if yad --title "NextCloud File Conflict" --text "\n${line1}\n\n${line2}\n" \ + --button="Rename!document-edit:0" --button="Abort!dialog-cancel:1" \ + --button="Overwrite!document-replace:2" --borders=10 + then + while [ -z "$newname" ]; do + newname="$(yad --entry --title "Rename File" --button="Save!document-save" \ + --entry-text="$1" --text="\nEnter new filename:" --borders=10 2>/dev/null)" + done + + nc_overwrite_check "$newname" && return + else + case "$?" in + 2) echo "$1" && return ;; + 1|70|252) ;; #noop + esac fi fi From 80cf2a6a03ae7c61e960e92a148ecf74bd1865c2 Mon Sep 17 00:00:00 2001 From: Dave Shoreman Date: Sun, 7 May 2023 00:41:13 +0100 Subject: [PATCH 4/4] Make sure the right file is shared --- src/main.bash | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.bash b/src/main.bash index 1238415..b6d92d9 100755 --- a/src/main.bash +++ b/src/main.bash @@ -129,7 +129,7 @@ main() { ncfilename="$(nc_overwrite_check "$image")" filename="$(echo "$image" | nc_upload "$ncfilename")" - json=$(nc_share "$filename") + json=$(nc_share "$ncfilename") url="$(echo "$json" | make_share_url)" echo "$url" | to_clipboard && send_notification