Skip to content

Commit

Permalink
Merge pull request #33 from PhilippMundhenk/pedropombeiro/fix-delete-…
Browse files Browse the repository at this point in the history
…temp-dir

Fix temp directories left behind
  • Loading branch information
PhilippMundhenk authored Sep 22, 2024
2 parents ea5467b + 4e04b26 commit 5c4fb30
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 116 deletions.
115 changes: 57 additions & 58 deletions files/runScanner.sh
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
#!/bin/bash
echo "setting up user & logfile:"
if [[ -z ${UID} ]]; then
UID=1000
UID=1000
fi
if [[ -z ${GID} ]]; then
GID=1000
GID=1000
fi
groupadd --gid $GID NAS
adduser $USERNAME --uid $UID --gid $GID --disabled-password --force-badname --gecos ""
groupadd --gid "$GID" NAS
adduser "$USERNAME" --uid $UID --gid "$GID" --disabled-password --force-badname --gecos ""
mkdir -p /scans
chmod 777 /scans
touch /var/log/scanner.log
chown $USERNAME /var/log/scanner.log
env > /opt/brother/scanner/env.txt
chown "$USERNAME" /var/log/scanner.log
env >/opt/brother/scanner/env.txt
chmod -R 777 /opt/brother
echo "-----"

echo "setting up interface:"
subnet=$(echo $IPADDRESS | sed 's/\([0-9]*\.[0-9]*\.\)[0-9]*\.[0-9]*/\1/')
interface=$(ip addr show | grep -B10 $subnet | grep mtu | tail -1 | sed 's/[0-9]*: \(.*\): .*/\1/')
subnet=$(echo "$IPADDRESS" | sed 's/\([0-9]*\.[0-9]*\.\)[0-9]*\.[0-9]*/\1/')
interface=$(ip addr show | grep -B10 "$subnet" | grep mtu | tail -1 | sed 's/[0-9]*: \(.*\): .*/\1/')
sed -i 's/^eth=.*//' /opt/brother/scanner/brscan-skey/brscan-skey.config
# if found an interface for scanner subnet. Will use this to contact scanner.
if [[ -z "$interface" ]]; then
# if scanner subnet (roughly) not found in interfaces, assuming network_mode="host" is not set and using Docker default interface.
interface="eth0"
# if scanner subnet (roughly) not found in interfaces, assuming network_mode="host" is not set and using Docker default interface.
interface="eth0"
fi
echo "eth=$interface" >> /opt/brother/scanner/brscan-skey/brscan-skey.config
echo "eth=$interface" >>/opt/brother/scanner/brscan-skey/brscan-skey.config
echo "using interface: $interface"
echo "-----"

echo "setting up host IP:"
sed -i 's/^ip_address=.*//' /opt/brother/scanner/brscan-skey/brscan-skey.config
if [[ -z "$HOST_IPADDRESS" ]]; then
echo "no host IP configured, using default discovery"
echo "no host IP configured, using default discovery"
else
echo "ip_address=$HOST_IPADDRESS" >> /opt/brother/scanner/brscan-skey/brscan-skey.config
echo "ip_address=$HOST_IPADDRESS" >>/opt/brother/scanner/brscan-skey/brscan-skey.config
fi
echo "-----"

Expand All @@ -43,63 +43,62 @@ cat /opt/brother/scanner/brscan-skey/brscan-skey.config
echo "-----"

echo "starting scanner drivers..."
su - $USERNAME -c "/usr/bin/brsaneconfig4 -a name=$NAME model=$MODEL ip=$IPADDRESS"
su - $USERNAME -c "/usr/bin/brscan-skey"
su - "$USERNAME" -c "/usr/bin/brsaneconfig4 -a name=$NAME model=$MODEL ip=$IPADDRESS"
su - "$USERNAME" -c "/usr/bin/brscan-skey"
echo "-----"

echo "setting up webserver:"
if [ "$WEBSERVER" == "true" ]; then
echo "starting webserver for API & GUI..."
{
echo "<?php"
echo "\$UID=$UID;"
echo "\$MODEL=\"$MODEL\";"
if [[ -n "$RENAME_GUI_SCANTOFILE" ]]; then
echo "\$RENAME_GUI_SCANTOFILE=$RENAME_GUI_SCANTOFILE;"
fi
if [[ -n "$RENAME_GUI_SCANTOEMAIL" ]]; then
echo "\$RENAME_GUI_SCANTOEMAIL=$RENAME_GUI_SCANTOEMAIL;"
fi
if [[ -n "$RENAME_GUI_SCANTOIMAGE" ]]; then
echo "\$RENAME_GUI_SCANTOIMAGE=$RENAME_GUI_SCANTOIMAGE;"
fi
if [[ -n "$RENAME_GUI_SCANTOOCR" ]]; then
echo "\$RENAME_GUI_SCANTOOCR=$RENAME_GUI_SCANTOOCR;"
fi
if [[ -n "$DISABLE_GUI_SCANTOFILE" ]]; then
echo "\$DISABLE_GUI_SCANTOFILE=$DISABLE_GUI_SCANTOFILE;"
fi
if [[ -n "$DISABLE_GUI_SCANTOEMAIL" ]]; then
echo "\$DISABLE_GUI_SCANTOEMAIL=$DISABLE_GUI_SCANTOEMAIL;"
fi
if [[ -n "$DISABLE_GUI_SCANTOIMAGE" ]]; then
echo "\$DISABLE_GUI_SCANTOIMAGE=$DISABLE_GUI_SCANTOIMAGE;"
fi
if [[ -n "$DISABLE_GUI_SCANTOOCR" ]]; then
echo "\$DISABLE_GUI_SCANTOOCR=$DISABLE_GUI_SCANTOOCR;"
fi
echo "?>"
} > /var/www/html/config.php
chown www-data /var/www/html/config.php
if [[ -z ${PORT} ]]; then
PORT=80
fi
echo "running on port $PORT"
sed -i "s/server.port\W*= 80/server.port = $PORT/" /etc/lighttpd/lighttpd.conf
/usr/sbin/lighttpd -f /etc/lighttpd/lighttpd.conf
echo "webserver started"
echo "starting webserver for API & GUI..."
{
echo "<?php"
echo "\$UID=$UID;"
echo "\$MODEL=\"$MODEL\";"
if [[ -n "$RENAME_GUI_SCANTOFILE" ]]; then
echo "\$RENAME_GUI_SCANTOFILE=$RENAME_GUI_SCANTOFILE;"
fi
if [[ -n "$RENAME_GUI_SCANTOEMAIL" ]]; then
echo "\$RENAME_GUI_SCANTOEMAIL=$RENAME_GUI_SCANTOEMAIL;"
fi
if [[ -n "$RENAME_GUI_SCANTOIMAGE" ]]; then
echo "\$RENAME_GUI_SCANTOIMAGE=$RENAME_GUI_SCANTOIMAGE;"
fi
if [[ -n "$RENAME_GUI_SCANTOOCR" ]]; then
echo "\$RENAME_GUI_SCANTOOCR=$RENAME_GUI_SCANTOOCR;"
fi
if [[ -n "$DISABLE_GUI_SCANTOFILE" ]]; then
echo "\$DISABLE_GUI_SCANTOFILE=$DISABLE_GUI_SCANTOFILE;"
fi
if [[ -n "$DISABLE_GUI_SCANTOEMAIL" ]]; then
echo "\$DISABLE_GUI_SCANTOEMAIL=$DISABLE_GUI_SCANTOEMAIL;"
fi
if [[ -n "$DISABLE_GUI_SCANTOIMAGE" ]]; then
echo "\$DISABLE_GUI_SCANTOIMAGE=$DISABLE_GUI_SCANTOIMAGE;"
fi
if [[ -n "$DISABLE_GUI_SCANTOOCR" ]]; then
echo "\$DISABLE_GUI_SCANTOOCR=$DISABLE_GUI_SCANTOOCR;"
fi
echo "?>"

} >/var/www/html/config.php
chown www-data /var/www/html/config.php
if [[ -z ${PORT} ]]; then
PORT=80
fi
echo "running on port $PORT"
sed -i "s/server.port\W*= 80/server.port = $PORT/" /etc/lighttpd/lighttpd.conf
/usr/sbin/lighttpd -f /etc/lighttpd/lighttpd.conf
echo "webserver started"
else
echo "webserver not configured"
echo "webserver not configured"
fi
echo "-----"

echo "capabilities:"
scanimage -A

echo "startup successful"
while true;
do
while true; do
tail -f /var/log/scanner.log
done
exit 0
44 changes: 26 additions & 18 deletions script/scanRear.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,44 @@ if [ "$USE_JPEG_COMPRESSION" = "true" ]; then
gm_opts+=(-compress JPEG -quality 80)
fi

# device=$1
device="$1"
script_dir="/opt/brother/scanner/brscan-skey/script"

mkdir -p /tmp
cd /tmp || exit
date=$(ls -rd */ | grep "$(date +"%Y-%m-%d")" | head -1)
date=${date%/}
filename_base="/tmp/$date/${date}-back-page"
output_file="${filename_base}%04d.pnm"
tmp_dir="/tmp/${date}"
filename_base="${tmp_dir}/${date}-back-page"
tmp_output_file="${filename_base}%04d.pnm"
tmp_output_pdf_file="${tmp_dir}/${date}.pdf"
output_pdf_file="/scans/${date}.pdf"

cd "/tmp/${date}" || exit
cd "$tmp_dir" || exit

pkill -P "$(cat scan_pid)"
rm scan_pid

#sthg is wrong with device name, probably escaping, use default printer:
#scan_cmd="scanimage -l 0 -t 0 -x 215 -y 297 --device-name=$device --resolution=$resolution --batch=$output_file"
scan_cmd="scanimage -l 0 -t 0 -x 215 -y 297 --resolution=$resolution --batch=$output_file"
function scan_cmd() {
# `brother4:net1;dev0` device name gets passed to scanimage, which it refuses as an invalid device name for some reason.
# Let's use the default scanner for now
# scanimage -l 0 -t 0 -x 215 -y 297 --device-name="$1" --resolution="$2" --batch="$3"
scanimage -l 0 -t 0 -x 215 -y 297 --format=pnm --resolution="$2" --batch="$3"
}

if [ "$(which usleep 2>/dev/null)" != '' ]; then
usleep 100000
else
sleep 0.1
fi
eval "$scan_cmd"
scan_cmd "$device" "$resolution" "$tmp_output_file"
if [ ! -s "${filename_base}0001.pnm" ]; then
if [ "$(which usleep 2>/dev/null)" != '' ]; then
usleep 1000000
else
sleep 1
fi
eval "$scan_cmd"
scan_cmd "$device" "$resolution" "$tmp_output_file"
fi

(
Expand Down Expand Up @@ -70,28 +78,28 @@ fi

(
echo "converting to PDF for $date..."
gm convert ${gm_opts[@]} ./*.pnm "/scans/${date}.pdf"
/opt/brother/scanner/brscan-skey/script/trigger_inotify.sh "${SSH_USER}" "${SSH_PASSWORD}" "${SSH_HOST}" "${SSH_PATH}" "${date}.pdf"
/opt/brother/scanner/brscan-skey/script/trigger_telegram.sh "${date}.pdf (rear) scanned"
gm convert ${gm_opts[@]} ./*.pnm "$output_pdf_file"
${script_dir}/trigger_inotify.sh "${SSH_USER}" "${SSH_PASSWORD}" "${SSH_HOST}" "${SSH_PATH}" "${output_pdf_file}"
${script_dir}/trigger_telegram.sh "${date}.pdf (rear) scanned"

echo "cleaning up for $date..."
cd /scans || exit
rm -rf "$date"
rm -rf "$tmp_dir"

if [ -z "${OCR_SERVER}" ] || [ -z "${OCR_PORT}" ] || [ -z "${OCR_PATH}" ]; then
echo "OCR environment variables not set, skipping OCR."
else
echo "starting OCR for $date..."
(
curl -F "userfile=@/scans/$date.pdf" -H "Expect:" -o /scans/"$date"-ocr.pdf "${OCR_SERVER}":"${OCR_PORT}"/"${OCR_PATH}"
/opt/brother/scanner/brscan-skey/script/trigger_inotify.sh "${SSH_USER}" "${SSH_PASSWORD}" "${SSH_HOST}" "${SSH_PATH}" "${date}-ocr.pdf"
/opt/brother/scanner/brscan-skey/script/trigger_telegram.sh "${date}-ocr.pdf (rear) OCR finished"
/opt/brother/scanner/brscan-skey/script/sendtoftps.sh \
curl -F "userfile=@${output_pdf_file}" -H "Expect:" -o "/scans/${date}-ocr.pdf" "${OCR_SERVER}":"${OCR_PORT}"/"${OCR_PATH}"
${script_dir}/trigger_inotify.sh "${SSH_USER}" "${SSH_PASSWORD}" "${SSH_HOST}" "${SSH_PATH}" "${date}-ocr.pdf"
${script_dir}/trigger_telegram.sh "${date}-ocr.pdf (rear) OCR finished"
${script_dir}/sendtoftps.sh \
"${FTP_USER}" \
"${FTP_PASSWORD}" \
"${FTP_HOST}" \
"${FTP_PATH}" \
"${date}.pdf"
"${output_pdf_file}"
) &
fi
) &
Expand Down
15 changes: 9 additions & 6 deletions script/scantoemail-0.2.4-1.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
# $2 = friendly name

{
echo "scantoemail.sh triggered"
#override environment, as brscan is screwing it up:
export $(grep -v '^#' /opt/brother/scanner/env.txt | xargs)
echo "scantoemail.sh triggered"
#override environment, as brscan is screwing it up:
export $(grep -v '^#' /opt/brother/scanner/env.txt | xargs)

SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"
/bin/bash $SCRIPTPATH/scanRear.sh $@
SCRIPTPATH="$(
cd "$(dirname "$0")" || exit
pwd -P
)"
/bin/bash "$SCRIPTPATH"/scanRear.sh $@

} >> /var/log/scanner.log 2>&1
} >>/var/log/scanner.log 2>&1
46 changes: 26 additions & 20 deletions script/scantofile-0.2.4-1.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,38 @@
gm_opts+=(-compress JPEG -quality 80)
fi

# device=$1
device="$1"
date=$(date +%Y-%m-%d-%H-%M-%S)
script_dir="/opt/brother/scanner/brscan-skey/script"
tmp_dir="/tmp/$date"
filename_base="${tmp_dir}/${date}-front-page"
tmp_output_file="${filename_base}%04d.pnm"
output_pdf_file="/scans/${date}.pdf"

mkdir -p "/tmp/$date"
cd "/tmp/$date" || exit
filename_base="/tmp/${date}/${date}-front-page"
output_file="${filename_base}%04d.pnm"
echo "filename: $output_file"
mkdir -p "$tmp_dir"
cd "$tmp_dir" || exit
echo "filename: $tmp_output_file"

#sthg is wrong with device name, probably escaping, use default printer:
#scan_cmd="scanimage -l 0 -t 0 -x 215 -y 297 --device-name=$device --resolution=$resolution --batch=$output_file"
scan_cmd="scanimage -l 0 -t 0 -x 215 -y 297 --resolution=$resolution --batch=$output_file"
function scan_cmd() {
# `brother4:net1;dev0` device name gets passed to scanimage, which it refuses as an invalid device name for some reason.
# Let's use the default scanner for now
# scanimage -l 0 -t 0 -x 215 -y 297 --device-name="$1" --resolution="$2" --batch="$3"
scanimage -l 0 -t 0 -x 215 -y 297 --format=pnm --resolution="$2" --batch="$3"
}

if [ "$(which usleep 2>/dev/null)" != '' ]; then
usleep 100000
else
sleep 0.1
fi
eval "$scan_cmd"
scan_cmd "$device" "$resolution" "$tmp_output_file"
if [ ! -s "${filename_base}0001.pnm" ]; then
if [ "$(which usleep 2>/dev/null)" != '' ]; then
usleep 1000000
else
sleep 1
fi
eval "$scan_cmd"
scan_cmd "$device" "$resolution" "$tmp_output_file"
fi

#only convert when no back pages are being scanned:
Expand All @@ -51,28 +57,28 @@

(
echo "converting to PDF for $date..."
gm convert ${gm_opts[@]} "$filename_base"*.pnm "/scans/${date}.pdf"
/opt/brother/scanner/brscan-skey/script/trigger_inotify.sh "${SSH_USER}" "${SSH_PASSWORD}" "${SSH_HOST}" "${SSH_PATH}" "${date}.pdf"
/opt/brother/scanner/brscan-skey/script/trigger_telegram.sh "${date}.pdf (front) scanned"
gm convert ${gm_opts[@]} "$filename_base"*.pnm "$output_pdf_file"
${script_dir}/trigger_inotify.sh "${SSH_USER}" "${SSH_PASSWORD}" "${SSH_HOST}" "${SSH_PATH}" "${output_pdf_file}"
${script_dir}/trigger_telegram.sh "${date}.pdf (front) scanned"

echo "cleaning up for $date..."
cd /scans || exit
rm -rf "$date"
rm -rf "$tmp_dir"

if [ -z "${OCR_SERVER}" ] || [ -z "${OCR_PORT}" ] || [ -z "${OCR_PATH}" ]; then
echo "OCR environment variables not set, skipping OCR."
else
echo "starting OCR for $date..."
(
curl -F "userfile=@/scans/$date.pdf" -H "Expect:" -o /scans/"$date"-ocr.pdf "${OCR_SERVER}":"${OCR_PORT}"/"${OCR_PATH}"
/opt/brother/scanner/brscan-skey/script/trigger_inotify.sh "${SSH_USER}" "${SSH_PASSWORD}" "${SSH_HOST}" "${SSH_PATH}" "${date}-ocr.pdf"
/opt/brother/scanner/brscan-skey/script/trigger_telegram.sh "${date}-ocr.pdf (front) OCR finished"
/opt/brother/scanner/brscan-skey/script/sendtoftps.sh \
curl -F "userfile=@${output_pdf_file}" -H "Expect:" -o "/scans/${date}-ocr.pdf" "${OCR_SERVER}":"${OCR_PORT}"/"${OCR_PATH}"
${script_dir}/trigger_inotify.sh "${SSH_USER}" "${SSH_PASSWORD}" "${SSH_HOST}" "${SSH_PATH}" "${date}-ocr.pdf"
${script_dir}/trigger_telegram.sh "${date}-ocr.pdf (front) OCR finished"
${script_dir}/sendtoftps.sh \
"${FTP_USER}" \
"${FTP_PASSWORD}" \
"${FTP_HOST}" \
"${FTP_PATH}" \
"${date}.pdf"
"${output_pdf_file}"
) &
fi
) &
Expand Down
12 changes: 6 additions & 6 deletions script/scantoimage-0.2.4-1.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

{

echo "ERROR!"
echo "This function is not implemented."
echo "You may implement your own script and mount under $0."
echo "Check out scripts in same folder or https://github.com/PhilippMundhenk/BrotherScannerDocker for examples."
} >> /var/log/scanner.log 2>&1
echo "ERROR!"
echo "This function is not implemented."
echo "You may implement your own script and mount under $0."
echo "Check out scripts in same folder or https://github.com/PhilippMundhenk/BrotherScannerDocker for examples."

} >>/var/log/scanner.log 2>&1
10 changes: 5 additions & 5 deletions script/scantoocr-0.2.4-1.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

{

echo "ERROR!"
echo "This function is not implemented."
echo "You may implement your own script and mount under $0."
echo "Check out scripts in same folder or https://github.com/PhilippMundhenk/BrotherScannerDocker for examples."
echo "ERROR!"
echo "This function is not implemented."
echo "You may implement your own script and mount under $0."
echo "Check out scripts in same folder or https://github.com/PhilippMundhenk/BrotherScannerDocker for examples."

} >> /var/log/scanner.log 2>&1
} >>/var/log/scanner.log 2>&1
Loading

0 comments on commit 5c4fb30

Please sign in to comment.