paperless-client/paperless.sh

56 lines
1.7 KiB
Bash
Raw Normal View History

2024-11-14 09:40:12 +01:00
#! /bin/sh
# SPDX-FileCopyrightText: 2024 Tobias Schmidl
#
# SPDX-License-Identifier: AGPL-3.0-or-later
2024-11-21 20:46:55 +01:00
VERSION="1.0.0"
# load_env loads the .env file and checks if the necessary parameters are present.
2024-11-14 10:11:49 +01:00
load_env () {
[ ! -r "./.env" ] && { echo "env file not readable, please make sure a .env is present." >&2; exit 1; }
. "./.env"
[ -z "$TOKEN" ] && { echo "Parameter TOKEN is not present in env file, please fill it with a valid paperless API token." >&2; exit 1; }
[ -z "$BASE_URL" ] && { echo "Parameter BASE_URL is not present in env file, please fill it with the URL of your paperless server." >&2; exit 1; }
}
2024-11-14 09:40:12 +01:00
2024-11-21 20:46:55 +01:00
# print_usage prints the usage of the script.
2024-11-14 10:12:32 +01:00
print_usage () {
cat << END_OF_COMMENT
$(basename "$0") get_tasks | put_document <file>
2024-11-14 10:32:15 +01:00
commands:
2024-11-14 10:12:32 +01:00
get_tasks Get the list of tasks. This is not formatted, i.e. you'll
have to pipe it yourself through a formatter.
put_document Uploads the document under <file> to your paperless
instance.
2024-11-14 10:32:15 +01:00
return values:
2024-11-14 10:12:32 +01:00
If everything went alright, $(basename "$0") returns 0. Every other return
value indicates an error.
END_OF_COMMENT
}
2024-11-14 09:40:12 +01:00
AUTH_PARM="Authorization: Token $TOKEN"
2024-11-21 20:46:55 +01:00
echo "$(basename "$0") v${VERSION}"
echo ""
[ "$#" -lt 1 ] && { print_usage; exit 1; }
2024-11-14 09:40:12 +01:00
case "$1" in
2024-11-14 10:11:49 +01:00
get_tasks)
load_env
curl -H "$AUTH_PARM" "${BASE_URL}/api/tasks/"
;;
put_document)
load_env
[ ! -r "$2" ] && { echo "file not readable." >&2; exit 1; }
curl -H "$AUTH_PARM" -X "POST" "${BASE_URL}/api/documents/post_document/" -H 'Accept: application/json' --form "document=@${2}"
;;
2024-11-14 10:12:32 +01:00
*)
print_usage
exit 1
;;
2024-11-14 09:40:12 +01:00
esac