Skip to content

Commit

Permalink
Merge pull request #5 from lunatic-fox/develop
Browse files Browse the repository at this point in the history
This pull request:
- Add temporary filenames to files reconverted from PDF
- Change log messages presentation
  • Loading branch information
lunatic-fox authored Nov 17, 2022
2 parents 3640d42 + 05137c5 commit b28f373
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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

Expand All @@ -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";;
*)
Expand All @@ -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"
Expand All @@ -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";;
Expand All @@ -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

0 comments on commit b28f373

Please sign in to comment.