Skip to content

Commit

Permalink
Merge pull request #83 from dshoreman/feature/dupecheck
Browse files Browse the repository at this point in the history
Prompt before overwriting files
  • Loading branch information
dshoreman committed May 6, 2023
2 parents 050c3c2 + 80cf2a6 commit 6931355
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 or renamed if they would overwrite another file


## [1.4.4] - 2023-05-05
Expand Down
62 changes: 57 additions & 5 deletions src/_nextcloud.bash
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,69 @@ make_url() {
|| echo "${server}/index.php${*}"
}

nc_overwrite_check() {
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}")"
status="$(curl -u "$username":"$password" "$reqUrl" -Lw "%{http_code}" -X PROPFIND -so/dev/null)"

if [ "$status" = 404 ]; then
echo "$1" && return
elif is_interactive; then
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 <b>$1</b> 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

echo "Upload cancelled!" >&2 && exit 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
Expand All @@ -43,7 +95,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"
}
Expand Down
7 changes: 4 additions & 3 deletions src/main.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -126,9 +126,10 @@ 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")
json=$(nc_share "$ncfilename")
url="$(echo "$json" | make_share_url)"

echo "$url" | to_clipboard && send_notification
Expand Down

0 comments on commit 6931355

Please sign in to comment.