Skip to content

Instantly share code, notes, and snippets.

@eli-oat
Forked from boxxxie/bash.couchdb.sh
Created March 7, 2016 20:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eli-oat/150223a1b79c3da7634e to your computer and use it in GitHub Desktop.
Save eli-oat/150223a1b79c3da7634e to your computer and use it in GitHub Desktop.
#!/bin/bash
# These functions require a CouchDB instance
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-rev() {
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 file="$2"
couch-push "$url" POST "$file"
}
couch-put() {
local url="$1"
local file="$2"
couch-push "$url" PUT "$file"
}
couch-upload() {
local db_url="$1"
local file_path="$2"
local mime="$3"
local rev=$(doc-rev $db_url)
echo "rev = $rev"
local rev_no_quotes="${rev//\"}"
echo "file name = $file_path"
local attachment_url="${db_url}/${file_path}?rev=${rev_no_quotes}"
echo "$attachment_url"
# echo curl -vX PUT "$attachment_url" -H "Content-Type: $mime" --data-binary @"$file_path"
curl -X PUT "$attachment_url" -H "Content-Type: ${mime}" --data-binary "@${file_path}"
#example
#curl -X PUT http://localhost:5984/test/test/thumbs/small/181.png?rev=894-52ca97d5586a86deab1f03d3f73bc5a9 -H "Content-Type: image/png" --data-binary @test/processed_doc/attachments/thumbs/tiny/111.png
}
couch-upload-dir() {
local url="$1"
local upload_dir="$2"
cd "$upload_dir"
find . | while read file; do
if [ -f "$file" ]
then
local file_rel_path="${file:2}";
echo file "$file_rel_path";
local mimetype=xdg-mime query filetype "$file_rel_path"
couch-upload "$url" "$file_rel_path" "$mimetype"
exit 0
fi
done
cd -
}
couch-upload-dir "$1" "$2"
#./couchdb-bash.sh http://localhost:5984/test/test test/processed_doc/attachments/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment