2019-09-18 22:23:25 +00:00
|
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
|
|
# A simple bash script that reads a configuration file to know where
|
|
|
|
|
# to upload a given file.
|
|
|
|
|
#
|
|
|
|
|
# The config file contains anonymous upload urls to docspell. All
|
2020-05-24 21:04:45 +00:00
|
|
|
|
# files given to this script are uploaded to all those urls.
|
2019-09-18 22:23:25 +00:00
|
|
|
|
#
|
2020-05-24 21:04:45 +00:00
|
|
|
|
# The default location for the config file is
|
2019-09-18 22:23:25 +00:00
|
|
|
|
# `~/.config/docspell/ds.conf'.
|
|
|
|
|
#
|
|
|
|
|
# The config file must contain lines of the form:
|
|
|
|
|
#
|
|
|
|
|
# url.1=http://localhost:7880/api/v1/open/upload/item/<source-id>
|
|
|
|
|
# url.2=...
|
|
|
|
|
#
|
|
|
|
|
# Lines starting with a `#' are ignored.
|
2020-02-27 19:03:46 +00:00
|
|
|
|
#
|
|
|
|
|
# The `-e|--exists' option allows to skip uploading and only check
|
|
|
|
|
# whether a given file exists in docspell.
|
2019-09-18 22:23:25 +00:00
|
|
|
|
|
|
|
|
|
# saner programming env: these switches turn some bugs into errors
|
|
|
|
|
set -o errexit -o pipefail -o noclobber -o nounset
|
|
|
|
|
|
|
|
|
|
CURL_CMD="curl"
|
|
|
|
|
GREP_CMD="grep"
|
|
|
|
|
MKTEMP_CMD="mktemp"
|
2019-12-31 22:17:07 +00:00
|
|
|
|
SHA256_CMD="sha256sum"
|
2019-09-18 22:23:25 +00:00
|
|
|
|
|
|
|
|
|
! getopt --test > /dev/null
|
|
|
|
|
if [[ ${PIPESTATUS[0]} -ne 4 ]]; then
|
|
|
|
|
echo 'I’m sorry, `getopt --test` failed in this environment.'
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
2020-02-27 19:03:46 +00:00
|
|
|
|
OPTIONS=c:hsde
|
|
|
|
|
LONGOPTS=config:,help,skip,delete,exists
|
2019-09-18 22:23:25 +00:00
|
|
|
|
|
|
|
|
|
! PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTS --name "$0" -- "$@")
|
|
|
|
|
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
|
|
|
|
|
# e.g. return value is 1
|
|
|
|
|
# then getopt has complained about wrong arguments to stdout
|
|
|
|
|
exit 2
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# read getopt’s output this way to handle the quoting right:
|
|
|
|
|
eval set -- "$PARSED"
|
|
|
|
|
|
2020-02-27 19:03:46 +00:00
|
|
|
|
exists=n delete=n help=n config="${XDG_CONFIG_HOME:-$HOME/.config}/docspell/ds.conf"
|
2019-09-18 22:23:25 +00:00
|
|
|
|
while true; do
|
|
|
|
|
case "$1" in
|
|
|
|
|
-h|--help)
|
|
|
|
|
help=y
|
|
|
|
|
shift
|
|
|
|
|
;;
|
|
|
|
|
-c|--config)
|
|
|
|
|
config="$2"
|
|
|
|
|
shift 2
|
|
|
|
|
;;
|
|
|
|
|
-d|--delete)
|
|
|
|
|
delete="y"
|
|
|
|
|
shift
|
|
|
|
|
;;
|
2020-02-27 19:03:46 +00:00
|
|
|
|
-e|--exists)
|
|
|
|
|
exists=y
|
|
|
|
|
shift
|
|
|
|
|
;;
|
2019-09-18 22:23:25 +00:00
|
|
|
|
--)
|
|
|
|
|
shift
|
|
|
|
|
break
|
|
|
|
|
;;
|
|
|
|
|
*)
|
|
|
|
|
echo "Programming error"
|
|
|
|
|
exit 3
|
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
info() {
|
|
|
|
|
echo "$1"
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-31 22:17:07 +00:00
|
|
|
|
checksum() {
|
|
|
|
|
$SHA256_CMD "$1" | cut -d' ' -f1 | xargs
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
checkFile() {
|
|
|
|
|
local url=$(echo "$1" | sed 's,upload/item,checkfile,g')
|
|
|
|
|
local file="$2"
|
|
|
|
|
$CURL_CMD -XGET -s "$url/$(checksum "$file")" | (2>&1 1>/dev/null grep '"exists":true')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
upload_file() {
|
2019-09-18 22:23:25 +00:00
|
|
|
|
tf=$($MKTEMP_CMD) rc=0
|
|
|
|
|
$CURL_CMD -# -o "$tf" --stderr "$tf" -w "%{http_code}" -XPOST -F file=@"$1" "$2" | (2>&1 1>/dev/null grep 200)
|
|
|
|
|
rc=$(expr $rc + $?)
|
|
|
|
|
cat $tf | (2>&1 1>/dev/null grep '{"success":true')
|
|
|
|
|
rc=$(expr $rc + $?)
|
|
|
|
|
if [ $rc -ne 0 ]; then
|
|
|
|
|
info "Upload failed. Exit code: $rc"
|
|
|
|
|
cat "$tf"
|
|
|
|
|
echo ""
|
|
|
|
|
rm "$tf"
|
|
|
|
|
return $rc
|
|
|
|
|
else
|
|
|
|
|
rm "$tf"
|
|
|
|
|
return 0
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-31 22:17:07 +00:00
|
|
|
|
upload() {
|
|
|
|
|
checkFile "$2" "$1"
|
|
|
|
|
if [ $? -eq 0 ]; then
|
|
|
|
|
info "File already exists at url $2"
|
|
|
|
|
return 0
|
|
|
|
|
else
|
|
|
|
|
upload_file "$1" "$2"
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-18 22:23:25 +00:00
|
|
|
|
showUsage() {
|
|
|
|
|
info "Upload files to docspell"
|
|
|
|
|
info ""
|
|
|
|
|
info "Usage: $0 [options] file [file ...]"
|
|
|
|
|
info ""
|
|
|
|
|
info "Options:"
|
|
|
|
|
info " -c | --config Provide a config file. (value: $config)"
|
|
|
|
|
info " -d | --delete Delete the files when successfully uploaded (value: $delete)"
|
|
|
|
|
info " -h | --help Prints this help text. (value: $help)"
|
2020-02-27 19:03:46 +00:00
|
|
|
|
info " -e | --exists Checks for the existence of a file instead of uploading (value: $exists)"
|
2019-09-18 22:23:25 +00:00
|
|
|
|
info ""
|
|
|
|
|
info "Arguments:"
|
2020-02-27 19:03:46 +00:00
|
|
|
|
info " One or more files to check for existence or upload."
|
2019-09-18 22:23:25 +00:00
|
|
|
|
info ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if [ "$help" = "y" ]; then
|
|
|
|
|
showUsage
|
|
|
|
|
exit 0
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# handle non-option arguments
|
|
|
|
|
if [[ $# -eq 0 ]]; then
|
|
|
|
|
echo "$0: No files given."
|
|
|
|
|
exit 4
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Read the config file
|
|
|
|
|
declare -a urls
|
|
|
|
|
while IFS="=" read -r k v
|
|
|
|
|
do
|
|
|
|
|
if [[ $k == url* ]]; then
|
|
|
|
|
urls+=($(echo "$v" | xargs))
|
|
|
|
|
fi
|
|
|
|
|
done <<< $($GREP_CMD -v '^#.*' "$config")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Main
|
|
|
|
|
IFS=$'\n'
|
|
|
|
|
for file in $*; do
|
2020-02-20 20:52:33 +00:00
|
|
|
|
for url in "${urls[@]}"; do
|
2020-02-27 19:03:46 +00:00
|
|
|
|
if [ "$exists" = "y" ]; then
|
|
|
|
|
if checkFile "$url" "$file"; then
|
|
|
|
|
info "$url $file: true"
|
|
|
|
|
else
|
|
|
|
|
info "$url $file: false"
|
|
|
|
|
fi
|
|
|
|
|
else
|
|
|
|
|
info "Uploading '$file' to '$url'"
|
|
|
|
|
set +e
|
|
|
|
|
upload "$file" "$url"
|
|
|
|
|
set -e
|
|
|
|
|
if [ "$delete" = "y" ] && [ $? -eq 0 ]; then
|
|
|
|
|
info "Deleting file: $file"
|
|
|
|
|
rm -f "$file"
|
|
|
|
|
fi
|
2020-02-20 20:52:33 +00:00
|
|
|
|
fi
|
|
|
|
|
done
|
2019-09-18 22:23:25 +00:00
|
|
|
|
done
|