-
Notifications
You must be signed in to change notification settings - Fork 0
/
smallify
executable file
·235 lines (205 loc) · 6.75 KB
/
smallify
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/bin/bash
#v1.0
FILE="$1"
QUALITY="$2"
MAXHEIGHT=$3
REPLACE=1 # 1 is False
WORKING="Working"
FOLDER="Smaller_comics"
PORTRAITSIZE=1000 # in ko
PAYSAGESIZE=2000 # in ko
usage="$(cat <<-EOF
$(basename "$0") file quality height [-h] [-r] [-f folder]
Example :
$(basename "$0") myfile.cbz 70 1920
By default, it saves smallified comics in 'Smaller_comics' default folder.
$(basename "$0") myfile.cbz 70 1920 -r
will REPLACE your comics ! Be careful !!
$(basename "$0") myfile.cbz 70 1920 -f \"my smaller comics\"
will save your smallified comics in \"my smaller comics\"
where:
-h show this help text
-r, --replace set the 'Replace' flag on ! It replace your comics !
Be very careful with this
-f, --folder set the folder where to save smallified comics
EOF
)"
shift 3
while :; do
case $1 in
-h|-\?|--help)
#show_help # Display a usage synopsis.
echo "$usage"
exit
;;
-r|--replace) # Takes an option argument; ensure it has been specified.
REPLACE=0
;;
-f|--folder) # Takes an option argument; ensure it has been specified.
if [ "$2" ]; then
FOLDER="$2"
# echo "------Destination folder : '$FOLDER'"
shift
else
die 'ERROR: "--folder" requires a non-empty option argument.'
fi
;;
--) # End of all options.
shift
break
;;
-?*)
printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2
;;
*) # Default case: No more options, so break out of the loop.
break
esac
shift
done
function unarchive-comic {
# If / Elif : unarchive file (rar or zip)
# Test if archive is RAR
if [[ $(file --mime-type -b "$FILE") == application/x-rar ]]; then
echo "------$FILE is rared"
DIR="${FILE%.*}"
mkdir -p "$WORKING/$DIR"
echo "------Extraction de $FILE"
unrar x ./"$FILE" -o "$WORKING/$DIR" -idq
# Test if archive is ZIP
elif [[ $(file --mime-type -b "$FILE") == application/zip ]]; then
echo "------$FILE is zipped"
echo "------Nettoyage de __MACOSX, .DS_Store, desktop.ini, Thumbs.db"
zip -d "$FILE" "__MACOSX*" &>/dev/null
zip -d "$FILE" "*.DS_Store" &>/dev/null
zip -d "$FILE" "*desktop.ini" &>/dev/null
zip -d "$FILE" "*Thumbs.db" &>/dev/null
DIR="${FILE%.*}"
mkdir -p "$WORKING/$DIR"
echo "------Extraction de $FILE"
unzip -q "$FILE" -d "$WORKING/$DIR"
else
echo "------WARNING : $FILE is not RAR or ZIP, skipping."
exit 1
fi
}
function smallify-jpeg {
IMG=$1
WIDTH=$(identify -format "%w" "$IMG")> /dev/null || return 1
HEIGHT=$(identify -format "%h" "$IMG")> /dev/null || return 1
SIZE=$(du -k "$IMG" | cut -f1) || return
PORTRAIT=true
if (( HEIGHT > WIDTH )); then PORTRAIT=true; else PORTRAIT=false; fi
# echo "------Dimensions : $WIDTH x $HEIGHT. Poids = $SIZE ko" Portrait ? $PORTRAIT
if $PORTRAIT ; then
# echo portrait
MAXSIZE=$PORTRAITSIZE
else
# echo ------paysage
MAXSIZE=$PAYSAGESIZE
fi
# HEIGHT > MAX and SIZE > MAX ?
if [[ $HEIGHT -gt $MAXHEIGHT && $SIZE -gt $MAXSIZE ]]; then
# echo "------magick size and quality !"
convert "$IMG" -quality "$QUALITY" -geometry x"$MAXHEIGHT" "$IMG" || return 1
# HEIGHT > MAX but SIZE already ok ?
elif [[ $HEIGHT -gt $MAXHEIGHT && $SIZE -le $MAXSIZE ]]; then
# echo "------magick size only"
convert "$IMG" -geometry x"$MAXHEIGHT" "$IMG" || return 1
# HEIGHT is ok (< MAX) but SIZE is too big ?
elif [[ $SIZE -gt $MAXSIZE ]]; then
# echo "------magick quality only"
convert "$IMG" -quality "$QUALITY" "$IMG" || return 1
else
# echo "------Nothing to do, jpeg already small"
:
fi
}
function smallify-png {
IMG=$1
HEIGHT=$(identify -format "%h" "$IMG")> /dev/null || return 1
if [[ $HEIGHT > $MAXHEIGHT ]]; then
# echo "------magick size only (png)"
convert "$IMG" -geometry x"$MAXHEIGHT" "$IMG" || return 1
fi
}
function archive-comic {
if [[ $FILE == *.cbz ]]; then
echo "------Création archive $FILE"
pushd "$WORKING/$DIR" 1>/dev/null || return
zip -q -r "../../$FOLDER/$FILE" -- * || return 1
popd 1>/dev/null || return
rm -r "${WORKING:?}/$DIR"
elif [[ $FILE == *.cbr ]]; then
echo "------Création archive $FILE"
rar a -ma4 -r -ep1 "$FOLDER/$FILE" "$WORKING/$DIR/" -idq || return 1
rm -r "${WORKING:?}/$DIR" || return
else
echo "------ERROR : Fichier non pris en charge"
return 1
fi
}
function check-comic {
pushd "$FOLDER" 1>/dev/null || return
# if [[ $FILE == *.cbz ]]; then
if [[ $(file --mime-type -b "$FILE") == application/zip ]]; then
if unzip -tq "$FILE"; then
echo "------$FILE is OK"
# 0 = true
popd 1>/dev/null || return
return 0
else
# 1 = false
echo "------$FILE is KO !!!!!!!!!"
popd 1>/dev/null || return
return 1
fi
elif [[ $(file --mime-type -b "$FILE") == application/x-rar ]]; then
if unrar t "$FILE" -idq; then
echo "------$FILE is OK"
# 0 = true
popd 1>/dev/null || return
return 0
else
echo "------$FILE is KO !!!!!!!!!"
# 1 = false
popd 1>/dev/null || return
return 1
fi
else
echo "------ERROR $FILE is not RAR or ZIP and has not been smallified !!!"
exit 1
fi
}
function move-comic {
echo "------moving $FILE"
mv -f "$FOLDER/$FILE" .
}
echo "[`date`]"
echo $(realpath "$FILE")
unarchive-comic || exit 1
# Smallify jpeg with Image Magick voodoo
echo "------Réduction des images"
pushd "$WORKING/$DIR" 1>/dev/null || return
# smallify all jpg
find . -type f -iname "*.jpg" -print0 | \
(while read -d $'\0' i; do smallify-jpeg "$i"; done)
# smallify all jpeg
find . -type f -iname "*.jpeg" -print0 | \
(while read -d $'\0' i; do smallify-jpeg "$i"; done)
# smallify all png
find . -type f -iname "*.png" -print0 | \
(while read -d $'\0' i; do smallify-png "$i"; done)
popd 1>/dev/null || return
mkdir -p "$FOLDER"
# make new archive
archive-comic
if [[ $REPLACE -eq 0 ]]; then
echo "------Remplacement des fichiers !!!!"
# Check integrity and move
if check-comic; then move-comic; fi
rm -rf "$FOLDER"
else
if check-comic; then echo "------Fichiers non remplacés, situés dans '$FOLDER'"; fi
fi
# rmdir "$FOLDER"
rm -rf $WORKING