mirror of
https://github.com/TheAnachronism/docspell.git
synced 2025-03-26 09:05:06 +00:00
Add new a tool ds.sh
A commandline script to upload given files to docspell. The URLs can be configured using a configuration file.
This commit is contained in:
parent
c76af5cbf4
commit
30b5c9d7f8
@ -67,3 +67,41 @@ This unit file is just an example, it needs some fiddling. It assumes
|
||||
an existing user `someuser` that is used to run this service. The url
|
||||
`http://localhost:7880/api/v1/open/upload/...` is an anonymous upload
|
||||
url as described [here](./uploading.html).
|
||||
|
||||
|
||||
## ds.sh
|
||||
|
||||
A bash script to quickly upload files from the command line. It reads
|
||||
a configuration file containing the URLs to upload to. Then each file
|
||||
given to the script will be uploaded to al URLs in the config.
|
||||
|
||||
The config file is expected in
|
||||
`$XDG_CONFIG_HOME/docspell/ds.conf`. `$XDG_CONFIG_HOME` defaults to
|
||||
`~/.config`.
|
||||
|
||||
The config file contains lines with key-value pairs, separated by an
|
||||
`=` sign. Lines starting with `#` are ignored. Example:
|
||||
|
||||
```
|
||||
# Config file
|
||||
url.1 = http://localhost:7880/api/v1/open/upload/item/5DxhjkvWf9S-CkWqF3Kr892-WgoCspFWDo7-XBykwCyAUxQ
|
||||
url.2 = http://localhost:7880/api/v1/open/upload/item/6DxhjkvWf9S-CkWqF3Kr892-WgoCspFWDo7-XBykwCyAUxQ
|
||||
```
|
||||
|
||||
The key must start with `url`.
|
||||
|
||||
### Usage
|
||||
|
||||
The `-h` option shows a help overview.
|
||||
|
||||
The script takes a list of files as arguments. It checks the file
|
||||
types and will raise an error (and quit) if a file is included that is
|
||||
not a PDF. The `-s` option can be used to skip them instead.
|
||||
|
||||
The `-c` option allows to specifiy a different config file.
|
||||
|
||||
Example:
|
||||
|
||||
``` bash
|
||||
./ds.sh ~/Downloads/*.pdf
|
||||
```
|
||||
|
@ -97,7 +97,7 @@ info() {
|
||||
|
||||
upload() {
|
||||
tf=$($MKTEMP_CMD) rc=0
|
||||
$CURL_CMD -s -o "$tf" -w "%{http_code}" -XPOST -F file=@"$1" "$2" | (2>&1 1>/dev/null grep 200)
|
||||
$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 + $?)
|
||||
|
173
tools/ds.sh
Executable file
173
tools/ds.sh
Executable file
@ -0,0 +1,173 @@
|
||||
#!/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
|
||||
# files given to this script are uploaded to those urls.
|
||||
#
|
||||
# The default location for the five is
|
||||
# `~/.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.
|
||||
|
||||
# saner programming env: these switches turn some bugs into errors
|
||||
set -o errexit -o pipefail -o noclobber -o nounset
|
||||
|
||||
CURL_CMD="curl"
|
||||
FILE_CMD="file"
|
||||
GREP_CMD="grep"
|
||||
MKTEMP_CMD="mktemp"
|
||||
|
||||
! getopt --test > /dev/null
|
||||
if [[ ${PIPESTATUS[0]} -ne 4 ]]; then
|
||||
echo 'I’m sorry, `getopt --test` failed in this environment.'
|
||||
exit 1
|
||||
fi
|
||||
|
||||
OPTIONS=c:hsd
|
||||
LONGOPTS=config:,help,skip,delete
|
||||
|
||||
! 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"
|
||||
|
||||
delete=n skip=n help=n config="${XDG_CONFIG_HOME:-$HOME/.config}/docspell/ds.conf"
|
||||
while true; do
|
||||
case "$1" in
|
||||
-h|--help)
|
||||
help=y
|
||||
shift
|
||||
;;
|
||||
-c|--config)
|
||||
config="$2"
|
||||
shift 2
|
||||
;;
|
||||
-s|--skip)
|
||||
skip="y"
|
||||
shift
|
||||
;;
|
||||
-d|--delete)
|
||||
delete="y"
|
||||
shift
|
||||
;;
|
||||
--)
|
||||
shift
|
||||
break
|
||||
;;
|
||||
*)
|
||||
echo "Programming error"
|
||||
exit 3
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
|
||||
info() {
|
||||
echo "$1"
|
||||
}
|
||||
|
||||
upload() {
|
||||
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
|
||||
}
|
||||
|
||||
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 " -s | --skip Skip non-PDF files. Otherwise an error is raised. (value: $skip)"
|
||||
info " -d | --delete Delete the files when successfully uploaded (value: $delete)"
|
||||
info " -h | --help Prints this help text. (value: $help)"
|
||||
info ""
|
||||
info "Arguments:"
|
||||
info " One or more PDF files to upload."
|
||||
info ""
|
||||
}
|
||||
|
||||
mimetype() {
|
||||
$FILE_CMD -b --mime-type "$1"
|
||||
}
|
||||
|
||||
isPdf() {
|
||||
mime=$(mimetype "$1")
|
||||
[ "$mime" = "application/pdf" ]
|
||||
}
|
||||
|
||||
|
||||
if [ "$help" = "y" ]; then
|
||||
showUsage
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# handle non-option arguments
|
||||
if [[ $# -eq 0 ]]; then
|
||||
echo "$0: No files given."
|
||||
exit 4
|
||||
fi
|
||||
|
||||
if [ "$skip" = "n" ]; then
|
||||
IFS=$'\n'
|
||||
for file in $*; do
|
||||
if ! isPdf "$file"; then
|
||||
info "Not a PDF file: $file"
|
||||
exit 5
|
||||
fi
|
||||
done
|
||||
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
|
||||
if isPdf "$file"; then
|
||||
for url in "${urls[@]}"; do
|
||||
info "Uploading '$file' to '$url'"
|
||||
set +e
|
||||
upload "$file" "$url"
|
||||
set -e
|
||||
if [ "$delete" = "y" ] && [ $rc -eq 0 ]; then
|
||||
info "Deleting file: $file"
|
||||
rm -f "$file"
|
||||
fi
|
||||
done
|
||||
else
|
||||
info "Skipping non-PDF file: $file"
|
||||
fi
|
||||
done
|
Loading…
x
Reference in New Issue
Block a user