-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshare-with-nextcloud
executable file
·134 lines (111 loc) · 3.71 KB
/
share-with-nextcloud
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env bash
. "$(dirname "$BASH_SOURCE")/lib/bridge-util.sh"
{
if [[ -z "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" ]]
then
NAUTILUS_SCRIPT_SELECTED_FILE_PATHS="$1"
fi
echo ""
echo "Running share-with-nextcloud on files:"
echo "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS"
echo ""
SETUP_SCRIPT="
{
. '$(realpath "$(dirname "$BASH_SOURCE")/lib/bridge-util.sh")';
SCRIPT_NAME='Share with Nextcloud'
SCRIPT_FILE_NAME='share-with-nextcloud'
declare -A DEPENDENCIES
DEPENDENCIES_APIT=('curl' 'python3')
DEPENDENCIES_DNF=('curl' 'python3')
CONFIG_ITEMS=(
'nc_url' 'Please enter your nextcloud url (e.g. https://my-domain.com/nextcloud)'
'user' 'Please enter your nextcloud user')
SECRETS=('Nextcloud Password' 'Please enter a valid Nextcloud user or app password:'
'account nextcloud id nextcloud-password')
show_setup_dialog
} 2>&1 | tee -a "$(get_log_path share-with-nextcloud)"
"
PW="$(secret-tool lookup account nextcloud service nautilus-bridge-tools id nextcloud-password)"
if [[ -z "$PW" ]] || ! [[ -f "$HOME/.config/nautilus-bridge-tools/.share-with-nextcloud.env" ]]
then
msg="share-with-nextcloud is not configured. Opening setup dialog..."
notify-send -a "share-with-nextcloud" "$msg"
echo "NOTICE: $msg" 1>&2
open-in-terminal bash -c "$SETUP_SCRIPT"
exit 1
else
. "$HOME/.config/nautilus-bridge-tools/.share-with-nextcloud.env"
fi
dav_url="$nc_url/remote.php/dav/files"
#set -x
urlencode() {
python3 -c "import urllib; print(urllib.parse.quote('''$1'''))"
}
create_nautilus_shares_directory() {
curl -X MKCOL -u "$user:$PW" "$dav_url/$user/nautilus-share" 2> /dev/null \
| grep '<s:message>The resource you tried to create already exists</s:message>' > /dev/null 2>&1
RESULT=("${PIPESTATUS[@]}")
if [[ "${RESULT[0]}" != 0 ]]
then
msg="An error occurred while creating directory nautilus-share"
echo "$msg" >&2
notify-send -a "share-with-nextcloud" "$msg"
exit 1
elif [[ "${RESULT[1]}" == 0 ]]
then
echo "Notice: The nautilus-share directory already exists" >&2
fi
}
upload_file() {
echo "file to upload: '$1' -> '$2'" >&2
if curl -X PROPFIND -u "$user:$PW" "$dav_url/$user/nautilus-share/" -H "Depth: 1" 2> /dev/null | grep "$2" > /dev/null 2>&1
then
msg="A file named '$2' already exists in $nc_url/index.php/apps/files/?dir=nautilus-share !"
echo "$msg" >&2
notify-send -a "share-with-nextcloud" "$msg"
return 1
fi
# Upload file
curl -T "$1" -u "$user:$PW" "$dav_url/$user/nautilus-share/$(urlencode "$2")"
}
share_file() {
# Create pulic share
local url response
response="$(curl -H "OCS-APIRequest: true" -u "$user:$PW" -H "Content-Type: application/json" -X POST \
--data "{\"path\": \"nautilus-share/$1\", \"shareType\": \"3\", \"publicUpload\": \"false\", \"permissions\": \"1\"}" \
"$nc_url/ocs/v2.php/apps/files_sharing/api/v1/shares")"
url="$(echo "$response" | grep '<url>')"
url="${response##*<url>}"
url="${url%%</url>*}"
if [[ -z "$url" ]]
then
msg="An error occurred while creating public share for file $file_name!"
echo "$msg" >&2
echo "----------------------" >&2
echo "$response" >&2
echo "----------------------" >&2
notify-send -a "share-with-nextcloud" "$msg"
return 1
else
echo "$url"
fi
}
create_nautilus_shares_directory
while read -r file
do
if [[ -f "$file" ]]
then
echo "Sharing file '$file'..."
file_name="$(basename "$file")"
echo "Uploading to 'nautilus-share/$file_name'..."
upload_file "$file" "$file_name" || exit 1
echo "Retrieving public share..."
url="$(share_file "$file_name")"
if [[ $? -ne 0 ]]
then
exit 2
fi
xdg-open "$url"
fi
done <<< "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS"
} 2>&1 | tee -a "$(get_log_path share-with-nextcloud)"