Skip to content

Commit b28f373

Browse files
authored
Merge pull request #5 from lunatic-fox/develop
This pull request: - Add temporary filenames to files reconverted from PDF - Change log messages presentation
2 parents 3640d42 + 05137c5 commit b28f373

1 file changed

Lines changed: 22 additions & 19 deletions

File tree

entrypoint.sh

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ format="$3"
1010
outputFormats="jpeg|bmp|tiff|png|pdf|svg|ps"
1111

1212
# Echo with foreground color
13-
# $1 message; $2 red; $3 green; $4 blue
14-
color() { echo -e "\033[38;2;$2;$3;$4m$1\033[0m"; }
13+
# $1 tag; $2 message; $3 red; $4 green; $5 blue
14+
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"; }
1515

1616
error() {
17-
color ":: Error :: $1" 255 0 0
17+
color "Error" "$1" 255 0 0
1818
exit 1
1919
}
2020

@@ -27,16 +27,12 @@ replace() { echo $1 | sed -E "s/$2/$3/"; }
2727

2828
# Change format
2929
# $1 path; $2 extension
30-
changeFmt() { echo "$(replace $1 '(.+\/)+(.+)\..+$' '\1\2').$2"; }
30+
changeFmt() { echo "$(replace "$1" '(.+\/)+(.+)\..+$' '\1\2').$2"; }
3131

32-
removeFmt() { echo $(replace $1 '(.+\/)+(.+)\..+$' '\1\2'); }
32+
rmFmt() { echo $(replace "$1" '(.+\/)+(.+)\..+$' '\1\2'); }
3333

3434
# Validates the $format input parameter
35-
isValidFormat() {
36-
if [[ $1 == !($outputFormats) ]]; then
37-
error "Unknown format: \"$1\""
38-
fi
39-
}
35+
isValidFormat() { if [[ $1 == !($outputFormats) ]]; then error "Unknown format: \"$1\""; fi; }
4036

4137
isValidFormat $format
4238

@@ -47,13 +43,13 @@ if [ -z $output ]; then
4743

4844
if [[ $format == +($outputFormats) ]]; then endFormat=".$format"; fi
4945

50-
output="${output}${endFormat}"
51-
color ":: Info :: Since \"output\" parameter is not defined, the converted file will be outputed to \"$output\"" 221 158 255
46+
output="$output$endFormat"
47+
color "Info" "Since \"output\" parameter is not defined, the converted file will be outputed to \"$output\"" 66 111 245
5248
fi
5349

5450
case $output in
5551
+(*/))
56-
output="${output}${defaultFilename}.$format";;
52+
output="$output$defaultFilename.$format";;
5753
!(*/*.*))
5854
output="$output.$format";;
5955
*)
@@ -62,7 +58,10 @@ case $output in
6258
format=$defaultOutputFormat;;
6359
esac
6460

65-
if [[ $format == +(jpeg|tiff|png|svg|ps) ]]; then toCairo=true; fi
61+
if [[ $format == +(jpeg|tiff|png|svg|ps) ]]; then
62+
toCairo=true
63+
cks=$(echo -n "$input$output$format" | sha256sum | sed -E "s/^([a-fA-F0-9]+).*/\1/")
64+
fi
6665

6766
if [[ $format == +(jpeg|tiff|png|pdf|svg|ps) ]]; then
6867
device="pdfwrite"
@@ -76,22 +75,26 @@ fi
7675

7776
dirOutput=$(replace $output '(.+\/).+$' '\1')
7877

79-
if [ ! -d $dirOutput ]; then mkdir -p $dirOutput; fi
78+
if [[ ! -d $dirOutput ]]; then mkdir -p $dirOutput; fi
79+
if [[ $toCairo ]]; then
80+
output="$(replace $output '(.+\/).+$' '\1')_${cks}_$(replace $output '.+\/(.+)$' '\1')"
81+
fi
8082

8183
gs $isBMP -sDEVICE=$device -dEPSCrop -o $output -q $input
8284

8385
if [[ $toCairo ]]; then
8486
input=$output
87+
rmTmp() { echo $(replace $1 "(.+\/)_${cks}_(.+)$" '\1\2'); }
8588

8689
case $fmt in
8790
+(svg|ps))
88-
output=$(changeFmt $input $fmt)
91+
output=$(rmTmp $(changeFmt $input $fmt))
8992
pdftocairo -$fmt $input $output;;
9093
+(png|tiff))
91-
output=$(removeFmt $input);
94+
output=$(rmTmp $(rmFmt $input $fmt))
9295
pdftocairo -singlefile -$fmt -transp $input $output;;
9396
jpeg)
94-
output=$(removeFmt $input);
97+
output=$(rmTmp $(rmFmt $input $fmt))
9598
pdftocairo -singlefile -$fmt -jpegopt quality=100,optimize=y $input $output
9699
wait
97100
mv "$output.jpg" "$output.jpeg";;
@@ -107,4 +110,4 @@ if [[ $toCairo ]]; then
107110
rm $input
108111
fi
109112

110-
color ":: Success :: File \"$output\" created in the workflow!" 66 245 66
113+
color "Success" "File \"$output\" created in the workflow!" 66 245 66

0 commit comments

Comments
 (0)