Repository navigation
Expand file tree
/
Copy pathscan.sh
More file actions
executable file
·80 lines (62 loc) · 2.33 KB
/
Copy pathscan.sh
File metadata and controls
executable file
·80 lines (62 loc) · 2.33 KB
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
73
74
75
76
77
78
79
80
#!/bin/bash
if [[ "$1" == "--help" || "$1" == "-h" ]]; then
echo "Usage: $0 [-p OUTPUT_PATH] [-t DOCKER_DURATION] [-o OUTPUT_NAME]"
echo " -p OUTPUT_PATH: Path where the scanned document will be saved (default: $(pwd))"
echo " -t DOCKER_DURATION: Duration in seconds to wait for scanning (default: 60)"
echo " -o OUTPUT_NAME: Name of the output file (default: DOC_<timestamp>.pdf)."
exit 0
fi
if [ "$EUID" -ne 0 ]; then
echo "This script must be run as root."
exit 1
fi
if ! command -v uuidgen &> /dev/null; then
echo "Error: uuidgen is not installed. Please install it and try again."
exit 1
fi
while getopts "o:p:t" opt; do
case $opt in
o) OUTPUT_NAME="$OPTARG" ;;
p) OUTPUT_PATH="$OPTARG" ;;
t) DOCKER_DURATION="$OPTARG" ;;
*) echo "Invalid option: -$OPTARG" >&2; exit 1 ;;
esac
done
OUTPUT_PATH=${OUTPUT_PATH:-"$(pwd)"}
TMP_OUTPUT_FOLDER=OUTPUT_$(uuidgen)
OUTPUT_NAME=${OUTPUT_NAME:-"DOC_$(date +%Y%m%d%H%M%S).pdf"}
DOCKER_DURATION=${DOCKER_DURATION:-60}
if ! [[ "$DOCKER_DURATION" =~ ^[0-9]+$ ]]; then
echo "Error: DOCKER_DURATION must be a valid number."
exit 1
fi
if [ ! -d "$OUTPUT_PATH" ]; then
mkdir -p "$OUTPUT_PATH"
if [ $? -ne 0 ]; then
echo "Error: Unable to create output directory $OUTPUT_PATH."
exit 1
fi
fi
echo "Your will find your scanned document in $OUTPUT_PATH/$OUTPUT_NAME"
mkdir -p "$OUTPUT_PATH/$TMP_OUTPUT_FOLDER"
if [ $? -ne 0 ]; then
echo "Error: Unable to create output folder $TMP_OUTPUT_FOLDER."
exit 1
fi
docker build -t ftpscan_image -f Dockerfile .
docker run -d --name ftpscan_container -p 8000:8000 -v "$OUTPUT_PATH/$TMP_OUTPUT_FOLDER:/var/www/html/output" ftpscan_image
echo "You have $DOCKER_DURATION seconds to scan your document. Passed this time, the script will stop the container and remove it."
sleep $DOCKER_DURATION
echo "Stopping the Docker container..."
docker stop ftpscan_container
docker rm ftpscan_container
docker rmi ftpscan_image
rm -rf $(ls DOC_*.LCK 2>/dev/null)\"
mv "$OUTPUT_PATH/$TMP_OUTPUT_FOLDER"/*.pdf "$OUTPUT_PATH/$OUTPUT_NAME" 2>/dev/null
if [ $? -ne 0 ]; then
echo "Error: Unable to save document to $OUTPUT_PATH/$OUTPUT_NAME. The scan may have failed."
exit 1
else
echo "Scanned document saved as $OUTPUT_PATH/$OUTPUT_NAME"
fi
rm -rf "$OUTPUT_PATH/$TMP_OUTPUT_FOLDER"