From 9255205da708504048aad50f58d0d206a5011f7a Mon Sep 17 00:00:00 2001 From: Pedro Pombeiro Date: Sun, 22 Sep 2024 16:45:22 +0200 Subject: [PATCH 01/20] Fix temp directory not being deleted --- script/scantofile-0.2.4-1.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/script/scantofile-0.2.4-1.sh b/script/scantofile-0.2.4-1.sh index 6ed0f11..5294e64 100755 --- a/script/scantofile-0.2.4-1.sh +++ b/script/scantofile-0.2.4-1.sh @@ -15,6 +15,7 @@ # device=$1 date=$(date +%Y-%m-%d-%H-%M-%S) + tmp_dir="/tmp/$date" mkdir -p "/tmp/$date" cd "/tmp/$date" || exit @@ -57,7 +58,7 @@ 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." From ae0714e2efdcebdeeda88edbb9bb1c7310f6ef7a Mon Sep 17 00:00:00 2001 From: Pedro Pombeiro Date: Sun, 22 Sep 2024 16:45:43 +0200 Subject: [PATCH 02/20] Clean up script by using variables --- script/scanRear.sh | 44 +++++++++++++++++++++--------------- script/scantofile-0.2.4-1.sh | 43 +++++++++++++++++++---------------- 2 files changed, 50 insertions(+), 37 deletions(-) diff --git a/script/scanRear.sh b/script/scanRear.sh index 85eb9bf..2addbc1 100755 --- a/script/scanRear.sh +++ b/script/scanRear.sh @@ -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 ( @@ -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 ) & diff --git a/script/scantofile-0.2.4-1.sh b/script/scantofile-0.2.4-1.sh index 5294e64..a238459 100755 --- a/script/scantofile-0.2.4-1.sh +++ b/script/scantofile-0.2.4-1.sh @@ -13,33 +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: @@ -52,9 +57,9 @@ ( 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 @@ -65,15 +70,15 @@ 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 ) & From 3bcdcbaf68ae84119fa849af41216cfcff4b9161 Mon Sep 17 00:00:00 2001 From: Pedro Pombeiro Date: Sun, 22 Sep 2024 17:09:46 +0200 Subject: [PATCH 03/20] Run shellfmt on runScanner.sh --- files/runScanner.sh | 115 ++++++++++++++++++++++---------------------- 1 file changed, 57 insertions(+), 58 deletions(-) diff --git a/files/runScanner.sh b/files/runScanner.sh index bffb90a..a3d82a0 100755 --- a/files/runScanner.sh +++ b/files/runScanner.sh @@ -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 "-----" @@ -43,54 +43,54 @@ 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 "" - - } > /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 "" + + } >/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 "-----" @@ -98,8 +98,7 @@ echo "capabilities:" scanimage -A echo "startup successful" -while true; -do +while true; do tail -f /var/log/scanner.log done exit 0 From 4e04b2666706dc3c0a25f1b6ce705549cd23df83 Mon Sep 17 00:00:00 2001 From: Pedro Pombeiro Date: Sun, 22 Sep 2024 17:10:34 +0200 Subject: [PATCH 04/20] Run shellfmt on new scripts --- script/scantoemail-0.2.4-1.sh | 15 +++++++++------ script/scantoimage-0.2.4-1.sh | 12 ++++++------ script/scantoocr-0.2.4-1.sh | 10 +++++----- script/trigger_inotify.sh | 3 +-- script/trigger_telegram.sh | 2 +- 5 files changed, 22 insertions(+), 20 deletions(-) diff --git a/script/scantoemail-0.2.4-1.sh b/script/scantoemail-0.2.4-1.sh index c6f1a49..2090633 100755 --- a/script/scantoemail-0.2.4-1.sh +++ b/script/scantoemail-0.2.4-1.sh @@ -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 diff --git a/script/scantoimage-0.2.4-1.sh b/script/scantoimage-0.2.4-1.sh index 465afe8..fc86ca2 100755 --- a/script/scantoimage-0.2.4-1.sh +++ b/script/scantoimage-0.2.4-1.sh @@ -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 \ No newline at end of file + 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 diff --git a/script/scantoocr-0.2.4-1.sh b/script/scantoocr-0.2.4-1.sh index 6ed72c8..fc86ca2 100755 --- a/script/scantoocr-0.2.4-1.sh +++ b/script/scantoocr-0.2.4-1.sh @@ -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 \ No newline at end of file +} >>/var/log/scanner.log 2>&1 diff --git a/script/trigger_inotify.sh b/script/trigger_inotify.sh index 4c6aeaa..4077b22 100755 --- a/script/trigger_inotify.sh +++ b/script/trigger_inotify.sh @@ -7,11 +7,10 @@ file=$5 if [ -z "${user}" ] || [ -z "${password}" ] || [ -z "${address}" ] || [ -z "${filepath}" ]; then echo "SSH environment variables not set, skipping inotify trigger." else - if sshpass -p "$password" ssh -o StrictHostKeyChecking=no $user@$address "sed \"\" -i $filepath/$file"; then + if sshpass -p "$password" ssh -o StrictHostKeyChecking=no "$user"@"$address" "sed \"\" -i $filepath/$file"; then echo "trigger inotify successful" else echo "trigger inotify failed" exit 1 fi fi - diff --git a/script/trigger_telegram.sh b/script/trigger_telegram.sh index 6280167..337953a 100755 --- a/script/trigger_telegram.sh +++ b/script/trigger_telegram.sh @@ -17,4 +17,4 @@ MESSAGE="Scanner: $1" ENCODED_MESSAGE=$(echo "$MESSAGE" | jq -sRr @uri) # Send the message using wget -wget -qO- --post-data="chat_id=$CHAT_ID&text=$ENCODED_MESSAGE" "https://api.telegram.org/$TOKEN/sendMessage" > /dev/null +wget -qO- --post-data="chat_id=$CHAT_ID&text=$ENCODED_MESSAGE" "https://api.telegram.org/$TOKEN/sendMessage" >/dev/null From b53bdaa3a40688c19c20c3741d77c1414133af1b Mon Sep 17 00:00:00 2001 From: Pedro Pombeiro Date: Sun, 22 Sep 2024 17:08:24 +0200 Subject: [PATCH 05/20] Add support for removing of blank pages --- Dockerfile | 52 +++++++++++++++++++++++------------------- README.md | 35 ++++++++++++++++++++++------ script/remove_blank.sh | 32 ++++++++++++++++++++++++++ script/scanRear.sh | 8 ++++++- 4 files changed, 95 insertions(+), 32 deletions(-) create mode 100755 script/remove_blank.sh diff --git a/Dockerfile b/Dockerfile index d12c0b4..f31ad4c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,33 +7,36 @@ RUN apt-get update && apt-get install -y --no-install-recommends apt-utils && ap RUN apt-get -y update && apt-get -y upgrade && apt-get -y clean RUN apt-get -y install \ - sane \ - sane-utils \ - ghostscript \ - netpbm \ - x11-common \ - wget \ - graphicsmagick \ - curl \ - ssh \ - sshpass \ - lighttpd \ - php-cgi \ - php-curl \ - sudo \ - iproute2 \ - jq \ - && apt-get -y clean + sane \ + sane-utils \ + ghostscript \ + netpbm \ + x11-common \ + wget \ + graphicsmagick \ + curl \ + ssh \ + sshpass \ + lighttpd \ + php-cgi \ + php-curl \ + sudo \ + iproute2 \ + jq \ + bc \ + pdftk \ + poppler-utils \ + && apt-get -y clean RUN cd /tmp && \ - wget https://download.brother.com/welcome/dlf105200/brscan4-0.4.11-1.amd64.deb && \ - dpkg -i /tmp/brscan4-0.4.11-1.amd64.deb && \ - rm /tmp/brscan4-0.4.11-1.amd64.deb + wget https://download.brother.com/welcome/dlf105200/brscan4-0.4.11-1.amd64.deb && \ + dpkg -i /tmp/brscan4-0.4.11-1.amd64.deb && \ + rm /tmp/brscan4-0.4.11-1.amd64.deb RUN cd /tmp && \ - wget https://download.brother.com/welcome/dlf006652/brscan-skey-0.3.1-2.amd64.deb && \ - dpkg -i /tmp/brscan-skey-0.3.1-2.amd64.deb && \ - rm /tmp/brscan-skey-0.3.1-2.amd64.deb + wget https://download.brother.com/welcome/dlf006652/brscan-skey-0.3.1-2.amd64.deb && \ + dpkg -i /tmp/brscan-skey-0.3.1-2.amd64.deb && \ + rm /tmp/brscan-skey-0.3.1-2.amd64.deb ADD files/runScanner.sh /opt/brother/runScanner.sh COPY script /opt/brother/scanner/brscan-skey/script @@ -50,6 +53,7 @@ ENV NAME="Scanner" ENV MODEL="MFC-L2700DW" ENV IPADDRESS="192.168.1.123" ENV USERNAME="NAS" +ENV REMOVE_BLANK_THRESHOLD="0.3" #only set these variables in the compose file, if inotify needs to be triggered (e.g., for Synology Drive): ENV SSH_USER="" @@ -67,7 +71,7 @@ ENV TELEGRAM_TOKEN="" ENV TELEGRAM_CHATID="" # Make sure this ends in a slash. -ENV FTP_PATH="/scans/" +ENV FTP_PATH="/scans/" EXPOSE 54925 EXPOSE 54921 diff --git a/README.md b/README.md index 5074e80..5429e22 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,24 @@ # Dockerized Brother Scanner + This is a dockerized scanner setup for Brother scanners. It allows you to run your scan server in a Docker environment and thus also on devices such as a Synology DiskStation. Additionally, some scripts are included that allow you to easily create duplex documents on non-duplex scanners. A configurable web-interface is provided, allowing you to trigger scans from your smartphone or PC. ## Setup + You have two options to set up your container: Preferred and Fallback. The preferred method is more complex but is able to address more situations, whereas the fallback method is much simpler, but might not work in all scenarios. Both are described in the following. ### Preferred + The preferred setup is slightly more complex, but can be applied in a larger number of settings, such as containers running in virtual machines, etc. Here, we require the IP address under which the container is reachable, as it will be contacted by the scanner, when scanning via the shortcut buttons. This may be the IP address of the Docker host, your virtual machine containing the Docker environment, etc. Additionally, we will need to forward the correct ports in Docker. Consider the following docker-compose file as an example for the preferred setup: + ```yaml version: '3' @@ -22,7 +26,7 @@ services: brother-scanner: image: ghcr.io/philippmundhenk/brotherscannerdocker volumes: - - /path/on/host:/scans + - /path/on/host:/scans ports: - 54925:54925/udp # mandatory, for scanner tools - 54921:54921 # mandatory, for scanner tools @@ -42,6 +46,7 @@ Here, the scanner (an MFC-L2700DW), is running on IP 192.168.1.10 and the contai The startup scripts will automatically configure the included Brother tooling, to set up the scanner accordingly. ### Fallback + The fallback setup might be a little more stable, but requires that your container can be bridged to the host network, rather than using Docker NAT. This is not possible in all situations (e.g., Docker on Win/Mac, limited underlying VM configuration, etc.). Consider the following docker-compose file: @@ -53,7 +58,7 @@ services: brother-scanner: image: ghcr.io/philippmundhenk/brotherscannerdocker volumes: - - /path/on/host:/scans + - /path/on/host:/scans environment: - NAME=Scanner - MODEL=MFC-L2700DW @@ -69,6 +74,7 @@ Note, that we do not need to specify the host IP address in this case, as we ass The startup scripts automatically tries to guess the host interface and adjust the Brother driver settings correctly. ### Further Notes + Note that the mounted folder /scans needs to have the correct permissions. By default, the scanner will run with user uid 1000 and gid 1000. You may change this through setting the environment variables UID and GID. @@ -82,6 +88,7 @@ If OCR, FTP, SSH options are specified, these will be executed, as well. There are a number of additional options explained in the following. ## Options + You can configure the tool via environment variables: | Variable | Type | Description | @@ -90,6 +97,7 @@ You can configure the tool via environment variables: | MODEL | mandatory | Model of your scanner (e.g., MFC-L2700DW) | | IPADDRESS | mandatory | IP Address of your scanner | | RESOLUTION | optional | DPI resolution of scan, refer to capabilities of printer on startup | +| REMOVE_BLANK_THRESHOLD | optional | Percentage of content in page until which a page is considered blank. Blank pages are removed if this variable is defined | | FTP_USER | optional | Username of an FTP(S) server to upload the completed scan to (see below) | | FTP_PASSWORD | optional | Username of an FTP(S) server to upload the completed scan to (see below) | | FTP_HOST | optional | Address of an FTP(S) server to upload the completed scan to (see below) | @@ -115,10 +123,11 @@ You can configure the tool via environment variables: | TELEGRAM_TOKEN | optional | If TELEGRAM_TOKEN and TELEGRAM_CHATID are set, then this sends notification | | TELEGRAM_CHATID | optional | If TELEGRAM_TOKEN and TELEGRAM_CHATID are set, then this sends notification | - ### FTPS upload + In addition to the storage in the mounted volume, you can use FTPS (Secure FTP) Upload. To do so, set the following environment variables to your values: + ``` - FTP_USER="scanner" - FTP_PASSWORD="scanner" @@ -129,40 +138,48 @@ To do so, set the following environment variables to your values: This only works with the scripts offered here in folder script/ (see Customize). ### Automatic Synchronization Solutions + Many automatic synchronization solutions, such as Synology CloudStation, are notified -about changes in the filesystem through inotify (see http://man7.org/linux/man-pages/man7/inotify.7.html). +about changes in the filesystem through inotify (see ). As the volume is mounted in Docker, the security mechanisms isolate the host and container filesystem. This means that such systems do not work. To solve this issue, a simple 'sed "" -i' can be performed on the file. The scripts in folder script/ use SSH to execute this command. This generates an inotify event, in turn starting synchronisation. To use this option, set the following variables to your values: + ``` - SSH_USER="admin" - SSH_PASSWORD="admin" - SSH_HOST="localhost" - SSH_PATH="/path/to/scans/folder/" ``` + Of course this requires SSH access to the host. If this is not available, consider the FTPS option. ### OCR + This image is prepared to utilize an OCR service, such as [my TesseractOCRMicroservice](https://github.com/PhilippMundhenk/TesseractOCRMicroservice). This uploads, waits for OCR to complete and downloads the file again. The resulting PDF file is saved in the /scans directory, with the appendix "-ocr" in the filename. To use this option, set the following variables to your values: + ``` - OCR_SERVER=192.168.1.101 - OCR_PORT=8080 - OCR_PATH=ocr.php ``` -This will call the OCR service at https://192.168.1.101:8080/ocr.php. + +This will call the OCR service at . ### Webserver + This image comes with an integrated webserver, allowing you to control the scanning functions also via API or GUI. To activate the webserver, you need to set an according environment variable. By default, the image uses port 80, but you may configure that. Additionally, for the GUI, you can rename and hide individual functions. here is an example of the environment: + ``` - WEBSERVER=true # optional, activates GUI & API - PORT=33355 # optional, sets port for webserver (default: 80) @@ -173,21 +190,24 @@ here is an example of the environment: ``` #### GUI + You can access the GUI under the IP of your container and the set port (or 80 in default case). With the full config example below, the result will look something like this: -![Screenshot of main web interface](doc/gui-main.jpg) +![Screenshot of main web interface](doc/gui-main.jpg) ![Screenshot of file list web interface](doc/gui-filelist.jpg) Note that the interface does not block when pressing a button. Thus, make sure to wait for your scan to complete, before pressing another button. #### API + The GUI uses a minimal "API" at the backend, which you can also use from other tooling (e.g., Home Assistant or a control panel near your printer). To scan, simply call `http://:/scan.php?target=` Also check out the endpoints `list.php`, `download.php`, `active.php`. Maybe one day an OpenAPI Spec will be included. ## Full Docker Compose Example + This docker-compose file can be run with minimal adaptions (environment variables MODEL, IPADDRESS, HOST_IPADDRESS & volume where files are to be stored): ```yaml @@ -231,12 +251,13 @@ services: ``` ## Customize Scan Scripts + As the standard scripts might not working particularly well for your purpose, you may customize them to your needs. You may also add additional scripts, as currently "Scan to Image" and "Scan to OCR" are not being used. Have a look in the folder `script/` in this repository for ideas. These scripts show some examples on how one might use the buttons on the printer. If you change these scripts, make sure to leave the filename as is, as the Brother drivers will call these scripts (or adapt /opt/brother/scanner/brscan-skey/brscan-skey.config). -Each script corresponds to a shortcut button on the scanner. +Each script corresponds to a shortcut button on the scanner. This way you can customize the actions running on your scanner. Hint: These scripts don't necessarily need to do scanning tasks. diff --git a/script/remove_blank.sh b/script/remove_blank.sh new file mode 100755 index 0000000..9a25c1b --- /dev/null +++ b/script/remove_blank.sh @@ -0,0 +1,32 @@ +#!/bin/bash +# remove_blank - git.waldenlabs.net/calvinrw/brother-paperless-workflow +# Heavily based on from Anthony Street's (and other contributors') +# StackExchange answer: https://superuser.com/a/1307895 + +if [ -n "$REMOVE_BLANK_THRESHOLD" ]; then + IN="$1" + FILENAME="$(basename "${IN}")" + FILENAME="${FILENAME%.*}" + SCRIPTNAME="remove_blank.sh" + PAGES="$(pdfinfo "$IN" | grep ^Pages: | tr -dc '0-9')" + echo "$SCRIPTNAME: threshold=$REMOVE_BLANK_THRESHOLD; analyzing $PAGES pages" + + cd "$(dirname "$IN")" || exit + pwd + + function non_blank() { + for i in $(seq 1 "$PAGES"); do + PERCENT=$(gs -o - -dFirstPage="${i}" -dLastPage="${i}" -sDEVICE=ink_cov "$IN" | grep CMYK | nawk 'BEGIN { sum=0; } {sum += $1 + $2 + $3 + $4;} END { printf "%.5f\n", sum } ') + if [ $(echo "$PERCENT > $REMOVE_BLANK_THRESHOLD" | bc) -eq 1 ]; then + echo "$i" + echo "Page $i: keep" 1>&2 + else + echo "Page $i: delete" 1>&2 + fi + done | tee "$FILENAME.tmp" + } + + set +x + pdftk "${IN}" cat $(non_blank) output "${FILENAME}_noblank.pdf" && + mv "${FILENAME}_noblank.pdf" "$IN" +fi diff --git a/script/scanRear.sh b/script/scanRear.sh index 2addbc1..0569739 100755 --- a/script/scanRear.sh +++ b/script/scanRear.sh @@ -14,6 +14,7 @@ fi device="$1" script_dir="/opt/brother/scanner/brscan-skey/script" +remove_blank="${script_dir}/remove_blank.sh" mkdir -p /tmp cd /tmp || exit @@ -78,10 +79,15 @@ fi ( echo "converting to PDF for $date..." - gm convert ${gm_opts[@]} ./*.pnm "$output_pdf_file" + gm convert ${gm_opts[@]} ./*.pnm "$tmp_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" + $remove_blank "$tmp_output_pdf_file" + mv "$tmp_output_pdf_file" "$output_pdf_file" + + $script_dir/trigger_inotify.sh "${SSH_USER}" "${SSH_PASSWORD}" "${SSH_HOST}" "${SSH_PATH}" "${output_pdf_file}" + echo "cleaning up for $date..." cd /scans || exit rm -rf "$tmp_dir" From a33e46e323d79a637dd461e722962c08709f9641 Mon Sep 17 00:00:00 2001 From: Philipp Mundhenk Date: Sun, 22 Sep 2024 19:53:12 +0200 Subject: [PATCH 06/20] Update scanRear.sh revert to old kill method --- script/scanRear.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script/scanRear.sh b/script/scanRear.sh index 85eb9bf..b4094c0 100755 --- a/script/scanRear.sh +++ b/script/scanRear.sh @@ -22,7 +22,7 @@ output_file="${filename_base}%04d.pnm" cd "/tmp/${date}" || exit -pkill -P "$(cat scan_pid)" +kill -9 "$(cat scan_pid)" rm scan_pid #sthg is wrong with device name, probably escaping, use default printer: From 66a63b8d58ce5f3fa148e8eacc2f1fcabde966a3 Mon Sep 17 00:00:00 2001 From: Philipp Mundhenk Date: Sun, 22 Sep 2024 20:12:11 +0200 Subject: [PATCH 07/20] Update scanRear.sh too lazy to rebase --- script/scanRear.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script/scanRear.sh b/script/scanRear.sh index 2addbc1..db7fea5 100755 --- a/script/scanRear.sh +++ b/script/scanRear.sh @@ -27,7 +27,7 @@ output_pdf_file="/scans/${date}.pdf" cd "$tmp_dir" || exit -pkill -P "$(cat scan_pid)" +kill -9 "$(cat scan_pid)" rm scan_pid function scan_cmd() { From fffcc0568846120e37c8acdb3774379def512763 Mon Sep 17 00:00:00 2001 From: Pedro Pombeiro Date: Sun, 22 Sep 2024 19:22:13 +0200 Subject: [PATCH 08/20] Exit script if scan cancelled --- script/scanRear.sh | 6 ++++-- script/scantofile-0.2.4-1.sh | 6 +++++- script/trigger_telegram.sh | 23 +++++++++++------------ 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/script/scanRear.sh b/script/scanRear.sh index db7fea5..76423a3 100755 --- a/script/scanRear.sh +++ b/script/scanRear.sh @@ -15,8 +15,10 @@ fi device="$1" script_dir="/opt/brother/scanner/brscan-skey/script" +set -e # Exit on error + mkdir -p /tmp -cd /tmp || exit +cd /tmp date=$(ls -rd */ | grep "$(date +"%Y-%m-%d")" | head -1) date=${date%/} tmp_dir="/tmp/${date}" @@ -25,7 +27,7 @@ tmp_output_file="${filename_base}%04d.pnm" tmp_output_pdf_file="${tmp_dir}/${date}.pdf" output_pdf_file="/scans/${date}.pdf" -cd "$tmp_dir" || exit +cd "$tmp_dir" kill -9 "$(cat scan_pid)" rm scan_pid diff --git a/script/scantofile-0.2.4-1.sh b/script/scantofile-0.2.4-1.sh index a238459..247287d 100755 --- a/script/scantofile-0.2.4-1.sh +++ b/script/scantofile-0.2.4-1.sh @@ -21,8 +21,12 @@ tmp_output_file="${filename_base}%04d.pnm" output_pdf_file="/scans/${date}.pdf" + set -e # Exit on error + mkdir -p "$tmp_dir" - cd "$tmp_dir" || exit + cd "$tmp_dir" + filename_base="/tmp/${date}/${date}-front-page" + output_file="${filename_base}%04d.pnm" echo "filename: $tmp_output_file" function scan_cmd() { diff --git a/script/trigger_telegram.sh b/script/trigger_telegram.sh index 337953a..f96f6e6 100755 --- a/script/trigger_telegram.sh +++ b/script/trigger_telegram.sh @@ -3,18 +3,17 @@ # Check if TELEGRAM_TOKEN and TELEGRAM_CHATID are both set if [ -z "${TELEGRAM_TOKEN}" ] || [ -z "${TELEGRAM_CHATID}" ]; then echo "TELEGRAM_TOKEN or TELEGRAM_CHATID is not set" - exit 1 -fi - -# Use the environment variables TELEGRAM_TOKEN and TELEGRAM_CHATID -TOKEN="$TELEGRAM_TOKEN" -CHAT_ID="$TELEGRAM_CHATID" +else + # Use the environment variables TELEGRAM_TOKEN and TELEGRAM_CHATID + TOKEN="$TELEGRAM_TOKEN" + CHAT_ID="$TELEGRAM_CHATID" -# The message is passed as a parameter -MESSAGE="Scanner: $1" + # The message is passed as a parameter + MESSAGE="Scanner: $1" -# URL encode the message to handle spaces and special characters -ENCODED_MESSAGE=$(echo "$MESSAGE" | jq -sRr @uri) + # URL encode the message to handle spaces and special characters + ENCODED_MESSAGE=$(echo "$MESSAGE" | jq -sRr @uri) -# Send the message using wget -wget -qO- --post-data="chat_id=$CHAT_ID&text=$ENCODED_MESSAGE" "https://api.telegram.org/$TOKEN/sendMessage" >/dev/null + # Send the message using wget + wget -qO- --post-data="chat_id=$CHAT_ID&text=$ENCODED_MESSAGE" "https://api.telegram.org/$TOKEN/sendMessage" >/dev/null +fi From e2f1ca60294ce71de028283b7c22b77a6e275162 Mon Sep 17 00:00:00 2001 From: Philipp Mundhenk Date: Sun, 22 Sep 2024 19:53:12 +0200 Subject: [PATCH 09/20] Update scanRear.sh revert to old kill method --- script/scanRear.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script/scanRear.sh b/script/scanRear.sh index 0569739..db6329d 100755 --- a/script/scanRear.sh +++ b/script/scanRear.sh @@ -28,7 +28,7 @@ output_pdf_file="/scans/${date}.pdf" cd "$tmp_dir" || exit -pkill -P "$(cat scan_pid)" +kill -9 "$(cat scan_pid)" rm scan_pid function scan_cmd() { From 110fbde6114cb4818b7ed87a0f785434237b52c5 Mon Sep 17 00:00:00 2001 From: Pedro Pombeiro Date: Sun, 22 Sep 2024 20:49:18 +0200 Subject: [PATCH 10/20] Add default value to README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5429e22..20846a8 100644 --- a/README.md +++ b/README.md @@ -97,7 +97,7 @@ You can configure the tool via environment variables: | MODEL | mandatory | Model of your scanner (e.g., MFC-L2700DW) | | IPADDRESS | mandatory | IP Address of your scanner | | RESOLUTION | optional | DPI resolution of scan, refer to capabilities of printer on startup | -| REMOVE_BLANK_THRESHOLD | optional | Percentage of content in page until which a page is considered blank. Blank pages are removed if this variable is defined | +| REMOVE_BLANK_THRESHOLD | optional | Percentage of content in page until which a page is considered blank. A good default is 0.3. Blank pages are removed if this variable is defined | | FTP_USER | optional | Username of an FTP(S) server to upload the completed scan to (see below) | | FTP_PASSWORD | optional | Username of an FTP(S) server to upload the completed scan to (see below) | | FTP_HOST | optional | Address of an FTP(S) server to upload the completed scan to (see below) | From fe11cb73607d4329f3381a5a92aff9051d27e441 Mon Sep 17 00:00:00 2001 From: Pedro Pombeiro Date: Sun, 22 Sep 2024 19:06:52 +0200 Subject: [PATCH 11/20] Make Telegram message match inotify message --- script/trigger_telegram.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script/trigger_telegram.sh b/script/trigger_telegram.sh index f96f6e6..ea4e3c2 100755 --- a/script/trigger_telegram.sh +++ b/script/trigger_telegram.sh @@ -2,7 +2,7 @@ # Check if TELEGRAM_TOKEN and TELEGRAM_CHATID are both set if [ -z "${TELEGRAM_TOKEN}" ] || [ -z "${TELEGRAM_CHATID}" ]; then - echo "TELEGRAM_TOKEN or TELEGRAM_CHATID is not set" + echo "TELEGRAM_TOKEN or TELEGRAM_CHATID environment variables not set, skipping Telegram trigger." else # Use the environment variables TELEGRAM_TOKEN and TELEGRAM_CHATID TOKEN="$TELEGRAM_TOKEN" From 7383d3e0802eb53dc04e219254e1341996e5a08c Mon Sep 17 00:00:00 2001 From: Philipp Mundhenk Date: Mon, 23 Sep 2024 17:52:19 +0200 Subject: [PATCH 12/20] removed master from branches, added PRs --- .github/workflows/publishBranches.yml | 5 ++- .github/workflows/publishPRs.yml | 52 +++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/publishPRs.yml diff --git a/.github/workflows/publishBranches.yml b/.github/workflows/publishBranches.yml index ea4646b..195d2d4 100644 --- a/.github/workflows/publishBranches.yml +++ b/.github/workflows/publishBranches.yml @@ -3,7 +3,10 @@ name: Create and publish a Docker image on a branch # Configures this workflow to run every time a change is pushed to the branch called `master`. on: - push + push: + branches: + - '*' + - '!master' # Defines two custom environment variables for the workflow. These are used for the Container registry domain, and a name for the Docker image that this workflow builds. env: diff --git a/.github/workflows/publishPRs.yml b/.github/workflows/publishPRs.yml new file mode 100644 index 0000000..d20757b --- /dev/null +++ b/.github/workflows/publishPRs.yml @@ -0,0 +1,52 @@ +# +name: Create and publish a Docker image on a branch + +# Configures this workflow to run every time a change is pushed to the branch called `master`. +on: + pull_request: + +# Defines two custom environment variables for the workflow. These are used for the Container registry domain, and a name for the Docker image that this workflow builds. +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + +# There is a single job in this workflow. It's configured to run on the latest available version of Ubuntu. +jobs: + build-and-push-image: + runs-on: ubuntu-latest + # Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job. + permissions: + contents: read + packages: write + # + steps: + - name: Checkout repository + uses: actions/checkout@v3 + # Uses the `docker/login-action` action to log in to the Container registry registry using the account and password that will publish the packages. Once published, the packages are scoped to the account defined here. + - name: Log in to the Container registry + uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + # This step uses [docker/metadata-action](https://github.com/docker/metadata-action#about) to extract tags and labels that will be applied to the specified image. The `id` "meta" allows the output of this step to be referenced in a subsequent step. The `images` value provides the base name for the tags and labels. + - name: Extract branch name + shell: bash + run: echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> $GITHUB_OUTPUT + id: extract_branch + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + tags: ${{ steps.extract_branch.outputs.branch }} + # This step uses the `docker/build-push-action` action to build the image, based on your repository's `Dockerfile`. If the build succeeds, it pushes the image to GitHub Packages. + # It uses the `context` parameter to define the build's context as the set of files located in the specified path. For more information, see "[Usage](https://github.com/docker/build-push-action#usage)" in the README of the `docker/build-push-action` repository. + # It uses the `tags` and `labels` parameters to tag and label the image with the output from the "meta" step. + - name: Build and push Docker image + uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4 + with: + context: . + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} From c7fd1e6772d912d1a676ffd1ec831fd979c235f3 Mon Sep 17 00:00:00 2001 From: Philipp Mundhenk Date: Mon, 23 Sep 2024 17:54:57 +0200 Subject: [PATCH 13/20] merged branches and PRs into one workflow --- ...ishBranches.yml => publishBranchesPRs.yml} | 1 + .github/workflows/publishPRs.yml | 52 ------------------- 2 files changed, 1 insertion(+), 52 deletions(-) rename .github/workflows/{publishBranches.yml => publishBranchesPRs.yml} (99%) delete mode 100644 .github/workflows/publishPRs.yml diff --git a/.github/workflows/publishBranches.yml b/.github/workflows/publishBranchesPRs.yml similarity index 99% rename from .github/workflows/publishBranches.yml rename to .github/workflows/publishBranchesPRs.yml index 195d2d4..4042baf 100644 --- a/.github/workflows/publishBranches.yml +++ b/.github/workflows/publishBranchesPRs.yml @@ -3,6 +3,7 @@ name: Create and publish a Docker image on a branch # Configures this workflow to run every time a change is pushed to the branch called `master`. on: + pull_request push: branches: - '*' diff --git a/.github/workflows/publishPRs.yml b/.github/workflows/publishPRs.yml deleted file mode 100644 index d20757b..0000000 --- a/.github/workflows/publishPRs.yml +++ /dev/null @@ -1,52 +0,0 @@ -# -name: Create and publish a Docker image on a branch - -# Configures this workflow to run every time a change is pushed to the branch called `master`. -on: - pull_request: - -# Defines two custom environment variables for the workflow. These are used for the Container registry domain, and a name for the Docker image that this workflow builds. -env: - REGISTRY: ghcr.io - IMAGE_NAME: ${{ github.repository }} - -# There is a single job in this workflow. It's configured to run on the latest available version of Ubuntu. -jobs: - build-and-push-image: - runs-on: ubuntu-latest - # Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job. - permissions: - contents: read - packages: write - # - steps: - - name: Checkout repository - uses: actions/checkout@v3 - # Uses the `docker/login-action` action to log in to the Container registry registry using the account and password that will publish the packages. Once published, the packages are scoped to the account defined here. - - name: Log in to the Container registry - uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1 - with: - registry: ${{ env.REGISTRY }} - username: ${{ github.actor }} - password: ${{ secrets.GITHUB_TOKEN }} - # This step uses [docker/metadata-action](https://github.com/docker/metadata-action#about) to extract tags and labels that will be applied to the specified image. The `id` "meta" allows the output of this step to be referenced in a subsequent step. The `images` value provides the base name for the tags and labels. - - name: Extract branch name - shell: bash - run: echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> $GITHUB_OUTPUT - id: extract_branch - - name: Extract metadata (tags, labels) for Docker - id: meta - uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7 - with: - images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} - tags: ${{ steps.extract_branch.outputs.branch }} - # This step uses the `docker/build-push-action` action to build the image, based on your repository's `Dockerfile`. If the build succeeds, it pushes the image to GitHub Packages. - # It uses the `context` parameter to define the build's context as the set of files located in the specified path. For more information, see "[Usage](https://github.com/docker/build-push-action#usage)" in the README of the `docker/build-push-action` repository. - # It uses the `tags` and `labels` parameters to tag and label the image with the output from the "meta" step. - - name: Build and push Docker image - uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4 - with: - context: . - push: true - tags: ${{ steps.meta.outputs.tags }} - labels: ${{ steps.meta.outputs.labels }} From 25f60ce1dc192bc557d4199f79956678ac584db3 Mon Sep 17 00:00:00 2001 From: Philipp Mundhenk Date: Mon, 23 Sep 2024 19:21:04 +0200 Subject: [PATCH 14/20] fixed syntax error --- .github/workflows/publishBranchesPRs.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/publishBranchesPRs.yml b/.github/workflows/publishBranchesPRs.yml index 4042baf..42912db 100644 --- a/.github/workflows/publishBranchesPRs.yml +++ b/.github/workflows/publishBranchesPRs.yml @@ -1,9 +1,9 @@ # name: Create and publish a Docker image on a branch -# Configures this workflow to run every time a change is pushed to the branch called `master`. +# Configures this workflow to run every time a change is pushed to any branch not called `master` and on PR event. on: - pull_request + pull_request: push: branches: - '*' From 4a9be2781da608abdda60c8651d6dece278cd2ae Mon Sep 17 00:00:00 2001 From: Philipp Mundhenk Date: Mon, 23 Sep 2024 19:25:04 +0200 Subject: [PATCH 15/20] removed branch builds, use PR instead, avoids double builds --- .github/workflows/publish.yml | 2 +- .github/workflows/publishBranchesPRs.yml | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index e5082cf..2d11974 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -1,5 +1,5 @@ # -name: Create and publish a Docker image on master +name: Master: Create and publish a Docker image # Configures this workflow to run every time a change is pushed to the branch called `master`. on: diff --git a/.github/workflows/publishBranchesPRs.yml b/.github/workflows/publishBranchesPRs.yml index 42912db..11ddba7 100644 --- a/.github/workflows/publishBranchesPRs.yml +++ b/.github/workflows/publishBranchesPRs.yml @@ -1,13 +1,13 @@ # -name: Create and publish a Docker image on a branch +name: PR: Create and publish a Docker image -# Configures this workflow to run every time a change is pushed to any branch not called `master` and on PR event. +# Build when a PR is created on: pull_request: - push: - branches: - - '*' - - '!master' + #push: + # branches: + # - '*' + # - '!master' # Defines two custom environment variables for the workflow. These are used for the Container registry domain, and a name for the Docker image that this workflow builds. env: From fba33636711c43f69a51e08f61bd9e541667d0bf Mon Sep 17 00:00:00 2001 From: Philipp Mundhenk Date: Mon, 23 Sep 2024 19:25:47 +0200 Subject: [PATCH 16/20] fixed syntax error --- .github/workflows/publish.yml | 2 +- .github/workflows/publishBranchesPRs.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 2d11974..8abddac 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -1,5 +1,5 @@ # -name: Master: Create and publish a Docker image +name: Master - Create and publish a Docker image # Configures this workflow to run every time a change is pushed to the branch called `master`. on: diff --git a/.github/workflows/publishBranchesPRs.yml b/.github/workflows/publishBranchesPRs.yml index 11ddba7..09148d8 100644 --- a/.github/workflows/publishBranchesPRs.yml +++ b/.github/workflows/publishBranchesPRs.yml @@ -1,5 +1,5 @@ # -name: PR: Create and publish a Docker image +name: PR - Create and publish a Docker image # Build when a PR is created on: From 04994c4343e44fde3028ebf616d15143ab2cb609 Mon Sep 17 00:00:00 2001 From: Philipp Mundhenk Date: Mon, 23 Sep 2024 19:39:32 +0200 Subject: [PATCH 17/20] added separate tag publishing for correc image labeling --- .github/workflows/publishTags.yml | 54 +++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 .github/workflows/publishTags.yml diff --git a/.github/workflows/publishTags.yml b/.github/workflows/publishTags.yml new file mode 100644 index 0000000..26cbb5f --- /dev/null +++ b/.github/workflows/publishTags.yml @@ -0,0 +1,54 @@ +# +name: PR - Create and publish a Docker image + +# Build when a tag is created +on: + push: + tags: + - '**' + +# Defines two custom environment variables for the workflow. These are used for the Container registry domain, and a name for the Docker image that this workflow builds. +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + +# There is a single job in this workflow. It's configured to run on the latest available version of Ubuntu. +jobs: + build-and-push-image: + runs-on: ubuntu-latest + # Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job. + permissions: + contents: read + packages: write + # + steps: + - name: Checkout repository + uses: actions/checkout@v3 + # Uses the `docker/login-action` action to log in to the Container registry registry using the account and password that will publish the packages. Once published, the packages are scoped to the account defined here. + - name: Log in to the Container registry + uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + # This step uses [docker/metadata-action](https://github.com/docker/metadata-action#about) to extract tags and labels that will be applied to the specified image. The `id` "meta" allows the output of this step to be referenced in a subsequent step. The `images` value provides the base name for the tags and labels. + - name: Extract branch name + shell: bash + run: echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> $GITHUB_OUTPUT + id: extract_branch + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + tags: ${{ github.ref_name }} + # This step uses the `docker/build-push-action` action to build the image, based on your repository's `Dockerfile`. If the build succeeds, it pushes the image to GitHub Packages. + # It uses the `context` parameter to define the build's context as the set of files located in the specified path. For more information, see "[Usage](https://github.com/docker/build-push-action#usage)" in the README of the `docker/build-push-action` repository. + # It uses the `tags` and `labels` parameters to tag and label the image with the output from the "meta" step. + - name: Build and push Docker image + uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4 + with: + context: . + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} From d35ebd2bfdbbcff615363dd99414868dc12d5e73 Mon Sep 17 00:00:00 2001 From: Philipp Mundhenk Date: Mon, 23 Sep 2024 19:41:29 +0200 Subject: [PATCH 18/20] fixed naming --- .github/workflows/{publish.yml => publishMaster.yml} | 0 .github/workflows/publishTags.yml | 6 +----- 2 files changed, 1 insertion(+), 5 deletions(-) rename .github/workflows/{publish.yml => publishMaster.yml} (100%) diff --git a/.github/workflows/publish.yml b/.github/workflows/publishMaster.yml similarity index 100% rename from .github/workflows/publish.yml rename to .github/workflows/publishMaster.yml diff --git a/.github/workflows/publishTags.yml b/.github/workflows/publishTags.yml index 26cbb5f..4dd9245 100644 --- a/.github/workflows/publishTags.yml +++ b/.github/workflows/publishTags.yml @@ -1,5 +1,5 @@ # -name: PR - Create and publish a Docker image +name: Tags - Create and publish a Docker image # Build when a tag is created on: @@ -32,10 +32,6 @@ jobs: username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} # This step uses [docker/metadata-action](https://github.com/docker/metadata-action#about) to extract tags and labels that will be applied to the specified image. The `id` "meta" allows the output of this step to be referenced in a subsequent step. The `images` value provides the base name for the tags and labels. - - name: Extract branch name - shell: bash - run: echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> $GITHUB_OUTPUT - id: extract_branch - name: Extract metadata (tags, labels) for Docker id: meta uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7 From da70dac75f670bcda5de0081cbb0e574b93d43d7 Mon Sep 17 00:00:00 2001 From: Philipp Mundhenk Date: Mon, 23 Sep 2024 19:58:47 +0200 Subject: [PATCH 19/20] fixed FTP uploads --- script/scanRear.sh | 8 +++++++- script/scantofile-0.2.4-1.sh | 10 ++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/script/scanRear.sh b/script/scanRear.sh index d9388fa..eb3e40f 100755 --- a/script/scanRear.sh +++ b/script/scanRear.sh @@ -84,6 +84,12 @@ fi gm convert ${gm_opts[@]} ./*.pnm "$tmp_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" + ${script_dir}/sendtoftps.sh \ + "${FTP_USER}" \ + "${FTP_PASSWORD}" \ + "${FTP_HOST}" \ + "${FTP_PATH}" \ + "${output_pdf_file}" $remove_blank "$tmp_output_pdf_file" mv "$tmp_output_pdf_file" "$output_pdf_file" @@ -107,7 +113,7 @@ fi "${FTP_PASSWORD}" \ "${FTP_HOST}" \ "${FTP_PATH}" \ - "${output_pdf_file}" + "/scans/${date}-ocr.pdf" ) & fi ) & diff --git a/script/scantofile-0.2.4-1.sh b/script/scantofile-0.2.4-1.sh index 247287d..3a4cdf8 100755 --- a/script/scantofile-0.2.4-1.sh +++ b/script/scantofile-0.2.4-1.sh @@ -64,7 +64,13 @@ 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" - + ${script_dir}/sendtoftps.sh \ + "${FTP_USER}" \ + "${FTP_PASSWORD}" \ + "${FTP_HOST}" \ + "${FTP_PATH}" \ + "${output_pdf_file}" + echo "cleaning up for $date..." cd /scans || exit rm -rf "$tmp_dir" @@ -82,7 +88,7 @@ "${FTP_PASSWORD}" \ "${FTP_HOST}" \ "${FTP_PATH}" \ - "${output_pdf_file}" + "/scans/${date}-ocr.pdf" ) & fi ) & From 970d0f7557a39703bb3a618adbb56fbafe8f690c Mon Sep 17 00:00:00 2001 From: Philipp Mundhenk Date: Mon, 23 Sep 2024 20:45:56 +0200 Subject: [PATCH 20/20] actually using NAME variable --- README.md | 2 +- files/runScanner.sh | 14 ++++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 20846a8..4aa062f 100644 --- a/README.md +++ b/README.md @@ -93,7 +93,7 @@ You can configure the tool via environment variables: | Variable | Type | Description | | ------------- | ------------- | ------------- | -| NAME | mandatory | Arbitrary name to give your scanner. Displayed on scanner, if multiple servers are running. | +| NAME | mandatory | Arbitrary (avoid spaces) name to give your scanner. Displayed on scanner, if multiple servers are running. | | MODEL | mandatory | Model of your scanner (e.g., MFC-L2700DW) | | IPADDRESS | mandatory | IP Address of your scanner | | RESOLUTION | optional | DPI resolution of scan, refer to capabilities of printer on startup | diff --git a/files/runScanner.sh b/files/runScanner.sh index a3d82a0..7cabf45 100755 --- a/files/runScanner.sh +++ b/files/runScanner.sh @@ -1,5 +1,11 @@ #!/bin/bash echo "setting up user & logfile:" + +if [[ $NAME == *" "* ]]; then + echo "Do not use spaces in NAME!" + exit -1 +fi + if [[ -z ${UID} ]]; then UID=1000 fi @@ -7,11 +13,11 @@ if [[ -z ${GID} ]]; then GID=1000 fi groupadd --gid "$GID" NAS -adduser "$USERNAME" --uid $UID --gid "$GID" --disabled-password --force-badname --gecos "" +adduser "$NAME" --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 +chown "$NAME" /var/log/scanner.log env >/opt/brother/scanner/env.txt chmod -R 777 /opt/brother echo "-----" @@ -43,8 +49,8 @@ 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 - "$NAME" -c "/usr/bin/brsaneconfig4 -a name=$NAME model=$MODEL ip=$IPADDRESS" +su - "$NAME" -c "/usr/bin/brscan-skey" echo "-----" echo "setting up webserver:"