|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +nc_url="https://my-nextcloud.example" |
| 4 | +user="eddie" |
| 5 | +PW="123456" |
| 6 | + |
| 7 | +[[ -f "$HOME/.local/share/nautilus/.share-with-nextcloud.env" ]] \ |
| 8 | + && . "$HOME/.local/share/nautilus/.share-with-nextcloud.env" |
| 9 | +dav_url="$nc_url/remote.php/dav/files" |
| 10 | + |
| 11 | +#set -x |
| 12 | + |
| 13 | +create_nautilus_shares_directory() { |
| 14 | + curl -X MKCOL -u "$user:$PW" "$dav_url/$user/nautilus-share" 2> /dev/null \ |
| 15 | + | grep '<s:message>The resource you tried to create already exists</s:message>' > /dev/null 2>&1 |
| 16 | + |
| 17 | + RESULT=("${PIPESTATUS[@]}") |
| 18 | + if [[ "${RESULT[0]}" != 0 ]] |
| 19 | + then |
| 20 | + msg="An error occurred while creating directory nautilus-share" |
| 21 | + echo "$msg" >&2 |
| 22 | + notify-send -a "share-with-nextcloud" "$msg" |
| 23 | + exit 1 |
| 24 | + elif [[ "${RESULT[1]}" == 0 ]] |
| 25 | + then |
| 26 | + echo "Notice: The nautilus-share directory already exists" >&2 |
| 27 | + fi |
| 28 | +} |
| 29 | + |
| 30 | +upload_file() { |
| 31 | + if curl -X PROPFIND -u "$user:$PW" "$dav_url/$user/nautilus-share/" -H "Depth: 1" 2> /dev/null | grep "$2" > /dev/null 2>&1 |
| 32 | + then |
| 33 | + msg="A file named '$2' already exists in $nc_url/index.php/apps/files/?dir=nautilus-share !" |
| 34 | + echo "$msg" >&2 |
| 35 | + notify-send -a "share-with-nextcloud" "$msg" |
| 36 | + return 1 |
| 37 | + fi |
| 38 | + |
| 39 | + # Upload file |
| 40 | + curl -T "$1" -u "$user:$PW" "$dav_url/$user/nautilus-share/$2" |
| 41 | +} |
| 42 | + |
| 43 | +share_file() { |
| 44 | + # Create pulic share |
| 45 | + local url |
| 46 | + url="$(curl -H "OCS-APIRequest: true" -u "$user:$PW" -H "Content-Type: application/json" -X POST \ |
| 47 | + --data "{\"path\": \"nautilus-share/$1\", \"shareType\": \"3\", \"publicUpload\": \"false\", \"permissions\": \"1\"}" \ |
| 48 | + "$nc_url/ocs/v2.php/apps/files_sharing/api/v1/shares" 2> /dev/null | grep '<url>')" |
| 49 | + url="${url##*<url>}" |
| 50 | + url="${url%%</url>*}" |
| 51 | + |
| 52 | + if [[ -z "$url" ]] |
| 53 | + then |
| 54 | + msg="An error occurred while creating public share for file $file_name!" |
| 55 | + echo "$msg" >&2 |
| 56 | + notify-send -a "share-with-nextcloud" "$msg" |
| 57 | + return 1 |
| 58 | + else |
| 59 | + echo "$url" |
| 60 | + fi |
| 61 | +} |
| 62 | + |
| 63 | +{ |
| 64 | + |
| 65 | +if [[ -z "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" ]] |
| 66 | +then |
| 67 | + NAUTILUS_SCRIPT_SELECTED_FILE_PATHS="$1" |
| 68 | +fi |
| 69 | +create_nautilus_shares_directory |
| 70 | + |
| 71 | +while read -r file |
| 72 | +do |
| 73 | + if [[ -f "$file" ]] |
| 74 | + then |
| 75 | + file_name="$(basename "$file")" |
| 76 | + |
| 77 | + upload_file "$file" "$file_name" || exit 1 |
| 78 | + url="$(share_file "$file_name" || exit 2)" |
| 79 | + xdg-open "$url" |
| 80 | + fi |
| 81 | + |
| 82 | +done <<< "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" |
| 83 | + |
| 84 | +} 2>&1 | tee ~/.share-with-nextcloud.log |
0 commit comments