-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost2paperless.sh
50 lines (46 loc) · 1.87 KB
/
post2paperless.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
#!/bin/bash
hostname=$hostname
auth_token=$auth_token
postdoc() {
while [[ $# -gt 0 ]]; do
data="@${1}"
regex='.*\.(.*)\.pdf'
if [[ $data =~ $regex ]]; then
retval=$(curl --retry 99 --retry-all-errors --http1.1 -f -X POST -H "Authorization: Token $auth_token" --form "document_type=${BASH_REMATCH[1]}" --form "document=$data" -sS https://$hostname/api/documents/post_document/ || exit 1)
else
retval=$(curl --retry 99 --retry-all-errors --http1.1 -f -X POST -H "Authorization: Token $auth_token" --form "document=$data" -sS https://$hostname/api/documents/post_document/ || exit 1)
fi
if [[ $? -gt 0 ]]; then
echo "curl failed, see error message above."
exit 1
fi
if [[ "$retval" == \"OK\" ]]; then
echo "WARNING: posting data was successful. This does not mean the file was properly imported. Do your checks before deleting data" >&2
shift
else
# see if this is a uuid we can query the task api with
# make a little loop until the document was properly ingested by paperless
status="unknown"
while [[ $status != "SUCCESS" ]] && [[ ${status} != "FAILURE" ]]; do
taskinfo=$(curl --retry 99 --retry-all-errors --http1.1 -f -X GET -H "Authorization: Token $auth_token" -sS https://${hostname}/api/tasks/?task_id=${retval//\"/})
status=$(jq -r .[].status <<<${taskinfo})
if [[ ${status} == "SUCCESS" ]]; then
echo "Document uploaded as https://${hostname}/documents/$(jq -r .[].related_document <<<$taskinfo)"
shift
elif [[ ${status} == "FAILURE" ]]; then
echo "failed to upload document $data"
jq -r .[].result <<<$taskinfo
exit 1
else
# echo "DEBUG: waiting while task ${retval} is being executed"
sleep 1
fi
done
fi
done
}
if [[ $# -eq 0 ]]; then
postdoc "-"
else
postdoc "$@"
fi