From 8fa6e6b81b115e581d3d967b90a130f1edd7c438 Mon Sep 17 00:00:00 2001 From: Andreas Romeyke <art1@andreas-romeyke.de> Date: Wed, 12 Oct 2022 11:36:12 +0200 Subject: [PATCH] - added debug() - added error() - added hash to collect all validators - added get_mimetype() - added prepare_cmd() - added prepare_ftype() - added estimate_mode() if MODE=any is used - extended scan_file() handling stages --- validate_workflow.sh | 144 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 126 insertions(+), 18 deletions(-) diff --git a/validate_workflow.sh b/validate_workflow.sh index e20785f..a609c5c 100755 --- a/validate_workflow.sh +++ b/validate_workflow.sh @@ -57,8 +57,12 @@ #hh #hh -#DEFAULTS +# expected programs: +# file, ... + +# DEFAULTS WITH_DAEMON=0 +WITH_DEBUG=0 WITH_PIPE=0 STAGE=any MODE=auto @@ -70,6 +74,22 @@ RESULT_FOLDER="" VALID_FOLDER="" INVALID_FOLDER="" +# PREDEFINED VALIDATORS +declare -A validators +#validators[workflow][filetype][stage] +# workflow has max 11 chars +# filetype has max 4 chars +# stage has max 9 chars +validators[__mediathek_mkv__current]="/usr/bin/mediaconch -ft -p /etc/mediaconch/profile,xml FILE" +validators[__mediathek_mkv_upcoming]="/usr/bin/mediaconch -ft -p /etc/mediaconch/profile,xml FILE" +validators[_____kitodo_tif__current]="/usr/bin/checkit_tiff_current /etc/checkit_tiff/retromono_current FILE" +validators[_____kitodo_tif_upcoming]="/usr/bin/checkit_tiff_upcoming /etc/checkit_tiff/retromono_upcoming FILE" +validators[___fotothek_tif__current]="/usr/bin/checkit_tiff_current /etc/checkit_tiff/retrofoto_current FILE" +validators[___fotothek_tif_upcoming]="/usr/bin/checkit_tiff_upcoming /etc/checkit_tiff/retrofoto_upcoming FILE" + + + + set -o nounset # Treat unset variables as an error # Don't just call this function "help()", as that's a reserved command in Bash. @@ -81,6 +101,22 @@ print_statistics() { echo "Not implemented yet" } +debug() { + if [ $WITH_DEBUG -eq 1 ]; then + >&2 echo "DEBUG: $1" + fi +} + +error() { + >&2 echo "ERROR: $1" +} + +get_mimetype() { + filename=$1 + res=$(file --mime-type "$filename") + echo "$res" +} + get_cli_args() { while [[ $# -gt 0 ]]; do case ${1} in @@ -135,19 +171,23 @@ get_cli_args() { WITH_PIPE=1 shift ;; + -d | --debug) + WITH_DEBUG=1 + shift + ;; *) - >&2 echo "'${1}' ist kein gültiger Parameter. Bitte benutzen Sie '$(basename "${0}") --help'." + error "'${1}' is invalid param. Please, give '$(basename "${0}") --help' a chance!" exit 1 ;; esac done if [ "$FILES_MODE" != "sort" -a "$FILES_MODE" != "delete" -a "$FILES_MODE" != "nothing" ]; then - >&2 echo "param --files-mode must be 'sort', 'delete' or 'nothing'!" + error "param --files-mode must be 'sort', 'delete' or 'nothing'!" exit 1 fi if [ "$MODE" != "auto" -a "$MODE" != "mediathek" -a "$MODE" != "fotothek" \ -a "$MODE" != "save" -a "$MODE" != "kitodo" -a "$MODE" != "lfulg" ]; then - >&2 echo "param --mode must be 'auto', 'mediathek', 'fotothek', 'save', 'kitodo' or 'lfulg'!" + error "param --mode must be 'auto', 'mediathek', 'fotothek', 'save', 'kitodo' or 'lfulg'!" exit 1 fi if [ "$WITH_PIPE" -eq 1 ]; then @@ -159,48 +199,115 @@ get_cli_args() { || [ "$INVALID_FOLDER" ] \ || [ "$FILES_MODE" = "sort" ] \ ; then - >&2 echo "param --pipe not combineable with params --daemon, --result-folder, --watch-folder, --valid-folder, --invalid-folder, --files-mode" + error "param --pipe not combineable with params --daemon, --result-folder, --watch-folder, --valid-folder, --invalid-folder, --files-mode" exit 1 fi else if [ "$WITH_DAEMON" -eq 1 -a "$FILES_MODE" = "sort" ]; then - >&2 echo "param --daemon does only work with param --mode='delete' or --mode='nothing'!" + error "param --daemon does only work with param --mode='delete' or --mode='nothing'!" exit 1 fi if [ "$STAGE" != "current" -a "$STAGE" != "upcoming" -a "$STAGE" != "any" ]; then - >&2 echo "--param stage must be 'any', 'current' or 'upcoming'!" + error "--param stage must be 'any', 'current' or 'upcoming'!" exit 1 fi if [ ! -d "$WATCH_FOLDER" ]; then - >&2 echo "watch folder '$WATCH_FOLDER' does not exist!" + error "watch folder '$WATCH_FOLDER' does not exist!" exit 1 fi if [ ! -d "$RESULT_FOLDER" ]; then - >&2 echo "result folder '$RESULT_FOLDER' does not exist!" + error "result folder '$RESULT_FOLDER' does not exist!" exit 1 fi if [ "$FILES_MODE" = "sort" ]; then if [ ! -d "$VALID_FOLDER" ]; then - >&2 echo "valid folder '$VALID_FOLDER' does not exist!" + error "valid folder '$VALID_FOLDER' does not exist!" exit 1 fi if [ ! -d "$INVALID_FOLDER" ]; then - >&2 echo "invalid folder '$INVALID_FOLDER' does not exist!" + error "invalid folder '$INVALID_FOLDER' does not exist!" exit 1 fi fi fi } +prepare_cmd() { + mode=$1 + ftype=$2 + stage=$3 + key=$(printf "%11s%4s%9s" $MODE $ftype $stage|sed -e "y/ /_/") + debug "prepare_cmd, key=$key" + cmd=${validators[$key]}; + debug "prepare_cmd, cmd=$cmd" + echo "$cmd" +} + +prepare_ftype() { + mimetype=$1 + debug "prepare_ftype, using mimetype: $mimetype" + case ${mimetype} in + "image/tiff") + ftype="tif" + ;; + "video/x-matroska") + ftype="mkv" + ;; + *) + error "unknown file format '$mimetype'" + exit 1 + ;; + esac + debug "prepare_ftype, detect ftype: $ftype" + echo "$ftype" +} + +estimate_mode() { + mimetype=$1 + debug "estimate_mode, using mimetype: $mimetype" + case ${mimetype} in + "image/tiff") + MODE="kitodo" + ;; + "video/x-matroska") + MODE="save" + ;; + *) + error "workflow not detectable" + exit 1 + ;; + esac + debug "estimate_mode, detected mode: $MODE" + echo $MODE +} + scan_file() { filename="$1" - echo "$filename" + debug "scan_file, using filename: $filename" + mimetype=$(get_mimetype "$filename" | cut -d " " -f 2) + ftype=$(prepare_ftype "$mimetype") + if [ "$MODE" = "auto" ]; then + # try best guess + MODE=$(estimate_mode "$mimetype") + fi + if [ "$STAGE" = "any" ]; then + for stage in current upcoming; do + debug "scan_file, using stage: $stage (STAGE mode '$STAGE')" + cmd=$(prepare_cmd "$MODE" "$ftype" "$stage") + echo "cmd=$cmd" + done + else + debug "scan_file, using stage: $STAGE" + cmd=$(prepare_cmd "$MODE" "$ftype" "$STAGE") + echo "cmd=$cmd" + fi + } scan_dir() { find "$1" -type f -print0| while IFS= read -r -d '' filename; do - scan_file "$filename" - done + scan_file "$filename" +done } #### MAIN @@ -208,18 +315,19 @@ scan_dir() { get_cli_args "$@" if [ "$WITH_PIPE" -eq 1 ]; then #cli mode, use stdin - echo "checking stream" + debug "checking stream" filename=$(mktemp validate_wrg.XXXX) + cat - > $filename scan_file "$filename" rm -f $filename else if [ "$WITH_DAEMON" -eq 1 ]; then # echo daemon mode, use inotify to watch changes - echo "starting daemon" - echo "stopping daemon" + debug "starting daemon" + debug "stopping daemon" else # cli mode, scan watch folder once - echo "checking dir $WATCH_FOLDER" + debug "checking dir $WATCH_FOLDER" scan_dir "$WATCH_FOLDER" fi fi -- GitLab