-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan-upload
executable file
·72 lines (51 loc) · 1.96 KB
/
scan-upload
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/bin/bash
#
# Use a scanner to obtain a pdf of a given file or files, then upload them.
SCAN_DIR="/tmp/scanner"
#SECRETS="~/.script-secrets.json"
SECRETS="./secrets.json"
mkdir -p $SCAN_DIR
declare -a SCANNED_IMAGES
scan_image() {
local FILE_NAME="${SCAN_DIR}/scan_$(date +%s).pdf"
sudo scanimage --format=pdf -o $FILE_NAME
sudo chown $USER:$USER $FILE_NAME
echo $FILE_NAME
}
ask_continue() {
read -p "Is there another page? (Y/n) " choice
[[ "${choice}" == "" || "${choice,,}" == "y" || ${choice,,} == "yes" ]]
}
ask_delete_temp_files() {
read -p "Should we clean up our temporary files? (Y/n) " choice
[[ "${choice}" == "" || "${choice,,}" == "y" || ${choice,,} == "yes" ]]
}
while true; do
img=$(scan_image)
SCANNED_IMAGES+=($img)
if ! ask_continue; then
break
fi
done
echo $SCANNED_IMAGES
OUTPUT_PDF=output.pdf
gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile="$OUTPUT_PDF" "${SCANNED_IMAGES[@]}"
PAPERLESS_ENABLED=$(jq -r '.paperless.enabled' $SECRETS)
echo "$PAPERLESS_ENABLED"
if [[ "${PAPERLESS_ENABLED,,}" == "true" ]]; then
PAPERLESS_URL=$(jq -r ".paperless.url" $SECRETS)
PAPERLESS_USERNAME=$(jq -r ".paperless.username" $SECRETS)
PAPERLESS_PASSWORD=$(jq -r ".paperless.password" $SECRETS)
PAPERLESS_TAGS=$(jq -r ".paperless.tags" $SECRETS)
AUTH_TOKEN=$(curl -s -X POST "${PAPERLESS_URL}/api/token/" -H "Content-Type: application/json" -d "{\"username\": \"${PAPERLESS_USERNAME}\", \"password\": \"${PAPERLESS_PASSWORD}\"}")
AUTH_TOKEN=$(echo $AUTH_TOKEN | jq -r ".token")
UPLOAD_RESPONSE=$(curl -s -X POST "${PAPERLESS_URL}/api/documents/post_document/" -H "Authorization: Token ${AUTH_TOKEN}" -F "document=@${OUTPUT_PDF}" -F "tags=${PAPERLESS_TAGS}")
echo "Reply: ${UPLOAD_RESPONSE}"
fi
if ask_delete_temp_files; then
rm -rf $SCAN_DIR
echo "Temporary files cleaned up"
else
echo "Temporary files kept in $FILE_DIR"
fi
echo "Document saved as $OUTPUT_PDF"