-
Notifications
You must be signed in to change notification settings - Fork 1
/
couchdb-bash.sh
executable file
·193 lines (161 loc) · 5.39 KB
/
couchdb-bash.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/bin/bash
# These functions require a CouchDB instance, xdg-mime, curl,
trim() {
# Determine if 'extglob' is currently on.
local extglobWasOff=1
shopt extglob >/dev/null && extglobWasOff=0
(( extglobWasOff )) && shopt -s extglob # Turn 'extglob' on, if currently turned off.
# Trim leading and trailing whitespace
local var=$1
var=${var##+([[:space:]])}
var=${var%%+([[:space:]])}
(( extglobWasOff )) && shopt -u extglob # If 'extglob' was off before, turn it back off.
echo -n "$var" # Output trimmed string.
}
rawurldecode() {
# This is perhaps a risky gambit, but since all escape characters must be
# encoded, we can replace %NN with \xNN and pass the lot to printf -b, which
# will decode hex for us
printf -v REPLY '%b' "${1//%/\\x}" # You can either set a return variable (FASTER)
echo "${REPLY}" #+or echo the result (EASIER)... or both... :p
}
couch-head() {
local url="$1"
curl -I "$url"
}
doc-revision() {
local url="$1"
local etag=$(couch-head "$url" | grep ETag)
echo "${etag/ETag: /}"
}
couch-get() {
local url="$1"
curl -X GET "$url"
}
couch-push() {
local http_type="$2"
local url="$1"
local file="$3"
echo curl -X "$http_type" "$url" -H "Content-Type: application/json" -d @"$file"
curl -X "$http_type" "$url" -H "Content-Type: application/json" -d @"$file"
}
couch-post() {
local url="$1"
local json="$2"
couch-push "$url" POST "$json"
}
couch-put() {
local url="$1"
local json="$2"
couch-push "$url" PUT "$json"
}
couch-upload-file() {
local db_url="$1"
local file_path="$2"
local mime="$3"
local rev=$(doc-revision "$db_ur"l)
rev=$(doc-revision "$db_url")
echo "rev = $rev"
local rev_no_quotes=$(trim "${rev//\"}")
echo "file name = $file_path"
local attachment_url="${db_url}/${file_path}?rev=${rev_no_quotes}"
echo "$attachment_url"
curl -X PUT "${attachment_url}" -H "Content-Type: ${mime}" --data-binary "'" "@${file_path}" "'"
}
couch-upload-json-file() {
local db_url="$1"
local json="$2"
local rev=$(doc-revision "$db_url")
rev=$(doc-revision "$db_url")
echo "rev = $rev"
local rev_no_quotes=$(trim "${rev//\"}")
local attachment_url="${db_url}/${file_path}?rev=${rev_no_quotes}"
curl -X PUT "${attachment_url}" -H "Content-Type: application/json" --data-binary "@${json}"
}
couch-upload-dir() {
local url="$1"
local upload_dir="$2"
cd "$upload_dir"
find . -type f | while read file; do
local file_rel_path="${file:2}"
local mimetype=$(xdg-mime query filetype "$file_rel_path")
couch-upload-file "$url" "$file_rel_path" "$mimetype"
done
cd -
}
base64_attachment() {
local file="$1";
local file_name="${file:2}";
local mime_type=$(xdg-mime query filetype "$file");
local file_base64=$(base64 -w 0 "$file");
echo '"'"$file_name"'"' : \
{'"content_type"' : '"'"$mime_type"'"' \
,'"data"' : '"'"$file_base64"'"' }
}
md5_maker() {
local file="$1";
local file_name="${file:2}";
local md5=$(echo "$file" | md5deep) ;
echo '"'"$file_name"'"' : { '"md5"' : '"'"$md5"'"' }
}
dir-to-couchdb-json() {
local url="$1"
local upload_dir="$2"
local file_dump="$3"
cd "$upload_dir"
doc=$(couch-get "$url")
#get an array of all of the files we are going to upload to couchdb
#mapfile -t files < <(find . -type f) ;
#attachments removed
local doc_no_attachments=$(underscore extend --data "$doc" '{"_attachments": undefined}')
#attachments extracted
local original_attachments=$(echo "$doc" | underscore extract _attachments)
echo
echo original_attachments
echo "$original_attachments"
#FIX this line produces [null] when there are no md5s to pluck...
#did a little fix, need to test this thing out! (was testing with bad
#data beforels -lh
local attachment_md5s=$(underscore pluck --data "$doc" attachments_md5s)
echo
echo attachment_md5s
echo "$attachment_md5s"
local original_attachments_trimmed_back="${original_attachments%'}'}"
local original_attachments_trimmed="${original_attachments_trimmed_back#'{'}"
doc_no_attachments_len="${doc_no_attachments}"
doc_no_attachments_beginning="${doc_no_attachments%'}'} ,"
echo "$doc_no_attachments_beginning" >> "$file_dump"
#### md5 processing #####
echo '"'"attachments_md5s"'"' ': {' >> "$file_dump" ;
local first_md5='';
find . -type f | {
while read file ; do
if [ "$first_md5" ] ;
then
local md5=$(md5_maker "$file") ;
echo "$md5" ',' >> "$file_dump" ;
else
first_md5=$(md5_maker "$file") ;
fi
done
echo "$first_md5" '},' >> "$file_dump"
}
### attachment processing ####
echo '"'"_attachments"'"' ': {' >> "$file_dump" ;
echo "$original_attachments_trimmed" ',' >> "$file_dump" ;
local first_attachment='';
find . -type f | {
while read file ; do
if [ "$first_attachment" ] ;
then
local attachment=$(base64_attachment "$file") ;
echo "$attachment" ',' >> "$file_dump" ;
else
first_attachment=$(base64_attachment "$file") ;
fi
done
echo "$first_attachment" '}' >> "$file_dump"
}
echo '}' >> "$file_dump" ;
cd -
}