diff --git a/entrypoint.sh b/entrypoint.sh index e06410c..10479e2 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -10,11 +10,11 @@ format="$3" outputFormats="jpeg|bmp|tiff|png|pdf|svg|ps" # Echo with foreground color -# $1 message; $2 red; $3 green; $4 blue -color() { echo -e "\033[38;2;$2;$3;$4m$1\033[0m"; } +# $1 tag; $2 message; $3 red; $4 green; $5 blue +color() { echo -e "\033[38;2;0;0;0m\033[48;2;$3;$4;$5m $1 \033[0m \033[38;2;$3;$4;$5m$2\033[0m"; } error() { - color ":: Error :: $1" 255 0 0 + color "Error" "$1" 255 0 0 exit 1 } @@ -27,16 +27,12 @@ replace() { echo $1 | sed -E "s/$2/$3/"; } # Change format # $1 path; $2 extension -changeFmt() { echo "$(replace $1 '(.+\/)+(.+)\..+$' '\1\2').$2"; } +changeFmt() { echo "$(replace "$1" '(.+\/)+(.+)\..+$' '\1\2').$2"; } -removeFmt() { echo $(replace $1 '(.+\/)+(.+)\..+$' '\1\2'); } +rmFmt() { echo $(replace "$1" '(.+\/)+(.+)\..+$' '\1\2'); } # Validates the $format input parameter -isValidFormat() { - if [[ $1 == !($outputFormats) ]]; then - error "Unknown format: \"$1\"" - fi -} +isValidFormat() { if [[ $1 == !($outputFormats) ]]; then error "Unknown format: \"$1\""; fi; } isValidFormat $format @@ -47,13 +43,13 @@ if [ -z $output ]; then if [[ $format == +($outputFormats) ]]; then endFormat=".$format"; fi - output="${output}${endFormat}" - color ":: Info :: Since \"output\" parameter is not defined, the converted file will be outputed to \"$output\"" 221 158 255 + output="$output$endFormat" + color "Info" "Since \"output\" parameter is not defined, the converted file will be outputed to \"$output\"" 66 111 245 fi case $output in +(*/)) - output="${output}${defaultFilename}.$format";; + output="$output$defaultFilename.$format";; !(*/*.*)) output="$output.$format";; *) @@ -62,7 +58,10 @@ case $output in format=$defaultOutputFormat;; esac -if [[ $format == +(jpeg|tiff|png|svg|ps) ]]; then toCairo=true; fi +if [[ $format == +(jpeg|tiff|png|svg|ps) ]]; then + toCairo=true + cks=$(echo -n "$input$output$format" | sha256sum | sed -E "s/^([a-fA-F0-9]+).*/\1/") +fi if [[ $format == +(jpeg|tiff|png|pdf|svg|ps) ]]; then device="pdfwrite" @@ -76,22 +75,26 @@ fi dirOutput=$(replace $output '(.+\/).+$' '\1') -if [ ! -d $dirOutput ]; then mkdir -p $dirOutput; fi +if [[ ! -d $dirOutput ]]; then mkdir -p $dirOutput; fi +if [[ $toCairo ]]; then + output="$(replace $output '(.+\/).+$' '\1')_${cks}_$(replace $output '.+\/(.+)$' '\1')" +fi gs $isBMP -sDEVICE=$device -dEPSCrop -o $output -q $input if [[ $toCairo ]]; then input=$output + rmTmp() { echo $(replace $1 "(.+\/)_${cks}_(.+)$" '\1\2'); } case $fmt in +(svg|ps)) - output=$(changeFmt $input $fmt) + output=$(rmTmp $(changeFmt $input $fmt)) pdftocairo -$fmt $input $output;; +(png|tiff)) - output=$(removeFmt $input); + output=$(rmTmp $(rmFmt $input $fmt)) pdftocairo -singlefile -$fmt -transp $input $output;; jpeg) - output=$(removeFmt $input); + output=$(rmTmp $(rmFmt $input $fmt)) pdftocairo -singlefile -$fmt -jpegopt quality=100,optimize=y $input $output wait mv "$output.jpg" "$output.jpeg";; @@ -107,4 +110,4 @@ if [[ $toCairo ]]; then rm $input fi -color ":: Success :: File \"$output\" created in the workflow!" 66 245 66 +color "Success" "File \"$output\" created in the workflow!" 66 245 66