-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnc_tag.sh
executable file
·112 lines (92 loc) · 3.05 KB
/
nc_tag.sh
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
#!/bin/bash
_HOST="nextcloud.example.com"
_USERNAME="user"
_PASSWORD="password"
get_fileprop_by_path() {
local path="$1"
local prop="$2"
curl -u $_USERNAME:$_PASSWORD "https://$_HOST/remote.php/dav/files/$_USERNAME/$path" -X PROPFIND --data '<?xml version="1.0" encoding="UTF-8"?>
<d:propfind xmlns:d="DAV:">
<d:prop xmlns:oc="http://owncloud.org/ns">
<oc:'"$prop"' />
</d:prop>
</d:propfind>' 2> /dev/null | xmlstarlet sel -t -v "//oc:$prop"
}
get_tags_from_server() {
curl -s -u $_USERNAME:$_PASSWORD "https://$_HOST/remote.php/dav/systemtags" -X PROPFIND --data '<?xml version="1.0" encoding="utf-8" ?>
<a:propfind xmlns:a="DAV:" xmlns:oc="http://owncloud.org/ns">
<a:prop>
<oc:display-name/>
<oc:user-visible/>
<oc:user-assignable/>
<oc:id/>
</a:prop>
</a:propfind>' | xmllint --format - | xmlstarlet sel -t -v "//oc:display-name | //oc:id" | grep -Pv '^$' | xargs -n2 -d'\n'
}
reload_tags() {
_TAGS="`get_tags_from_server`"
}
validate_tagname() {
local tag="$1"
echo $tag | grep -P '[^0-9a-zA-Z\-]' &> /dev/null && echo "INVALID TAGNAME $tag" && return 1
}
tag_exists() {
local needle="$1"
validate_tagname "$tag"
echo $_TAGS | grep -P "(^|\s)$needle [0-9]+" &> /dev/null
return $?
}
get_id_for_tag() {
local tag="$1"
tag_exists "$tag" || return 1
validate_tagname "$tag"
echo $_TAGS | grep -Po "(^|\s)$tag [0-9]+" | awk '{print $2}'
return $?
}
get_tag_for_id() {
local tagid="$1"
echo $_TAGS | grep -Po "(^|\s)[a-zA-Z0-9\-]+ $tagid(\s|$)" | awk '{print $1}'
}
get_tags_from_file() {
local path="$1"
fileid="`get_fileprop_by_path \"$path\" fileid`"
echo $fileid | grep -P '[^0-9]' &> /dev/null && return 1
[ -z "$fileid" ] && return 2
curl -s -u $_USERNAME:$_PASSWORD "https://$_HOST/remote.php/dav/systemtags-relations/files/$fileid" -X PROPFIND --data '<?xml version="1.0" encoding="utf-8" ?>
<a:propfind xmlns:a="DAV:" xmlns:oc="http://owncloud.org/ns">
<a:prop>
<oc:display-name/>
<oc:user-visible/>
<oc:user-assignable/>
<oc:id/>
</a:prop>
</a:propfind>' | xmlstarlet sel -t -v "//oc:display-name" | grep -vP '^$'
}
file_has_tag() {
local path="$1"
local tag="$2"
validate_tagname "$tag"
get_tags_from_file "$path" | grep -P "^$tag$" && return 0
return 1
}
add_tag_to_file() {
local path="$1"
local tag="$2"
validate_tagname "$tag"
file_has_tag "$path" "$tag" && return 1
fileid="`get_fileprop_by_path \"$path\" fileid`"
echo $fileid | grep -P '[^0-9]' &> /dev/null && return 1
[ -z "$fileid" ] && return 2
tagid="`get_id_for_tag \"$tag\"`"
echo $tagid | grep -P '[^0-9]' &> /dev/null && return 1
[ -z "$tagid" ] && return 2
curl -s -u $_USERNAME:$_PASSWORD "https://$_HOST/remote.php/dav/systemtags-relations/files/$fileid/$tagid" -X PUT
return $?
}
add_tag_to_server() {
local tag="$1"
validate_tagname "$tag"
tag_exists "$tag" && return 1
curl -s -u $_USERNAME:$_PASSWORD "https://$_HOST/remote.php/dav/systemtags/" -X POST -H 'Content-Type: application/json' --data "{\"userVisible\":true,\"userAssignable\":true,\"canAssign\":true,\"name\":\"$tag\"}"
return $?
}