-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtinypicomatic.sh
executable file
·259 lines (221 loc) · 5.77 KB
/
tinypicomatic.sh
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#! /bin/bash
### CONFIG ###
api_url="https://api.tinify.com/shrink"
failed_file=".tinypicomatic.failed.txt"
### COLORS ###
RED="\033[0;31m"
GREEN="\033[0;32m"
NORMAL="\033[0m"
### SCRIPT ###
function get_args {
while [[ $# -gt 0 ]]
do
arg="$1"
case "$arg" in
-p|--path)
path=$2
shift
shift
;;
-k|--api-key)
api_key=$2
shift
shift
;;
-b|--backup-path)
backup_path=$2
shift
shift
;;
-h|--help)
echo "Usage: tinypicomatic [OPTION]
Use the TinyPNG API to reduce the weight of pictures in a directory.
Example: tinypicomatic -k \"YOUR-API-KEY\" -p ./
Options:
-k, --api-key Your API key for TinyPNG
-p, --path Path of the directory where images are located
-b, --backup-path If specified, images will be copied there before any change is done
-h, --help Display this help and exit
tinypicomatic will search for all .png, .jpg and .jpeg file in the given path recursively.
If no path is specified the default one is the current directory."
exit
;;
*)
echo "Error: Unknow option \"$1\""
exit 1
;;
esac
done
}
function check_args {
if [ -z "$api_key" ]
then
echo "Error: You have to specify an api key"
exit 1
fi
if [ -z "$path" ]
then
path="."
else
if [ ! -d "$path" ]
then
echo "Error: Specified path directory does not exist"
exit 1
fi
path=${path%/}
if [[ ${path:0:2} = "./" ]]
then
path=${path:2}
fi
fi
if [ ! -z "$backup_path" ]
then
backup_path=${backup_path%/}
if [[ ${backup_path:0:2} = "./" ]]
then
backup_path=${backup_path:2}
fi
fi
}
function get_files {
files=()
while IFS= read -r -d $'\0'; do
files+=("$REPLY")
done < <(find "$path" \( -name "*.png" -o -name "*.jpg" -o -name "*.jpeg" \) -print0)
}
function create_backup_dir {
if [ ! -d "$backup_path" ] && [ ! -z "$backup_path" ]
then
mkdir "$backup_path"
fi
}
function check_file {
file_type=$(file "$1" | grep -oh "PNG\|JPEG")
if [ "$file_type" != "PNG" ] && [ "$file_type" != "JPEG" ]
then
echo -e "[${RED}KO${NORMAL}] Check \"$1\" is a valid file"
return 1
else
echo -e "[${GREEN}OK${NORMAL}] Check \"$1\" is a valid file"
fi
}
function backup_file {
if [ -z "$backup_path" ]
then
return
fi
file_name=$(basename "$file")
cp "$file" "$backup_path/$file_name" 2> /dev/null
if [ $? != 0 ]
then
echo -e "[${RED}KO${NORMAL}] Backup \"$file\" in \"$backup_path/$file_name\""
return 1
else
echo -e "[${GREEN}OK${NORMAL}] Backup \"$file\" in \"$backup_path/$file_name\""
fi
}
function upload_file {
location=$(curl -s "$api_url" \
--user api:"$api_key" \
--data-binary @"$file" \
--dump-header /dev/stdout 2> /dev/null \
| grep -i "location" | cut -d" " -f 2 | tr -dc "[:print:]")
if [ -z "$location" ]
then
echo -e "[${RED}KO${NORMAL}] Upload \"$file\" to \"$api_url\""
return 1
else
echo -e "[${GREEN}OK${NORMAL}] Upload \"$file\" to \"$api_url\""
fi
}
function download_file {
file_name=$(basename "$file")
curl -s "$location" \
--user api:"$api_key" \
--output "$file"
if [ $? != 0 ]
then
echo -e "[${RED}KO${NORMAL}] Download \"$file\" from \"$location\""
if [ ! -z "$backup_path" ]
then
mv "$backup_path/$file_name" "$file"
fi
return 1
else
echo -e "[${GREEN}OK${NORMAL}] Download \"$file\" from \"$location\""
fi
}
function verify_file {
file_name=$(basename "$file")
file_type=$(file "$file" | grep -oh "PNG\|JPEG")
if [ "$file_type" != "PNG" ] && [ "$file_type" != "JPEG" ]
then
echo -e "[${RED}KO${NORMAL}] Verify \"$file\" downloaded is a valid file"
if [ ! -z "$backup_path" ]
then
mv "$backup_path/$file_name" "$file"
fi
return 1
else
echo -e "[${GREEN}OK${NORMAL}] Verify \"$file\" downloaded is a valid file"
fi
}
function process_file {
status=0
if ! check_file "$1"; then status=1; fi
if [ $status = 0 ] && ! backup_file "$1"; then status=1; fi
if [ $status = 0 ] && ! upload_file "$1"; then status=1; fi
if [ $status = 0 ] && ! download_file "$1"; then status=1; fi
if [ $status = 0 ] && ! verify_file "$1"; then status=1; fi
if [ $status != 0 ]
then
echo "$1" >> $failed_file
fi
}
function loop_files {
cnt=0
for file in "${files[@]}"
do
process_file "$file" &
if [ $((cnt%10)) = 0 ] && [ $cnt != 0 ]
then
wait
fi
cnt=$((cnt+1))
done
wait
}
function print_failed {
if [ -f "$failed_file" ]
then
echo
echo -e "${RED}The following files failed to be optimized:${NORMAL}"
cat "$failed_file"
fi
}
function finish {
if [ -f "$failed_file" ]
then
echo
echo "The script failed to optimize all the files"
rm -f "$failed_file"
fi
if [ ! -z "$backup_path" ]
then
read -p "Do you want to remove the backup directory ? [y/N] " delete
if [ "$delete" = "n" ]
then
rm -rf "$backup_path"
fi
fi
}
function main {
get_args $@
check_args
get_files
create_backup_dir
loop_files
print_failed
finish
}
main $@