|
1 #!/bin/bash -e |
|
2 # |
|
3 # (c) 2013, Tomas Zeman <tzeman@volny.cz> |
|
4 # |
|
5 # License: |
|
6 # This program is distributed in the hope that it will be useful, |
|
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|
9 # |
|
10 |
|
11 mysql_cmd=${MYSQLCMD:-"mysql test"} |
|
12 imgfolder=${IMGFOLDER:-"./images"} |
|
13 imgtable=${IMGTABLE:-"images"} |
|
14 |
|
15 [ -d $imgfolder ] || mkdir -p $imgfolder |
|
16 |
|
17 mfile=`mktemp -t m2m.XXXXXX` |
|
18 mdir=`mktemp -d -t m2m.XXXXXX` |
|
19 |
|
20 cleanup() |
|
21 { |
|
22 rm -fr $mfile $mdir |
|
23 } |
|
24 |
|
25 trap cleanup EXIT |
|
26 |
|
27 cat > $mfile |
|
28 |
|
29 munpack -q -C $mdir < $mfile > /dev/null |
|
30 |
|
31 mfrom=$(822field from < $mfile | sed -e 's/^ \+//') |
|
32 mdate=$(822date < $mfile | head -2 | tail -1 | sed -e 's/ \++[0-9]\+$//') |
|
33 mmsgid=$(822field message-id < $mfile | sed -e 's/^ \+//') |
|
34 |
|
35 process_images() |
|
36 { |
|
37 pat="*.$1" |
|
38 while IFS= read -d $'\0' -r f; do |
|
39 mt=$(file -b --mime-type "$f") |
|
40 if [ `expr match "$mt" "image/"` -gt 0 ]; then |
|
41 on=$(basename "$f") |
|
42 ext=$(echo "${on##*.}" | tr [A-Z] [a-z]) |
|
43 fn="$(date '+%s.%N').$$.$ext" |
|
44 ts=$(date '+%F %T') |
|
45 cat "$f" > $imgfolder/$fn.tmp |
|
46 mv $imgfolder/$fn.tmp $imgfolder/$fn |
|
47 chmod 644 $imgfolder/$fn |
|
48 printf "INSERT INTO $imgtable (file_name, orig_name, mime_type, mail_from, mail_date, mail_message_id, ts) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s');\n" "$fn" "$on" "$mt" "$mfrom" "$mdate" "$mmsgid" "$ts" | $mysql_cmd |
|
49 fi |
|
50 done < <(find $mdir -type f -name "$pat" -print0) |
|
51 } |
|
52 |
|
53 process_images '[Jj][Pp][Gg]' |
|
54 process_images '[Pp][Nn][Gg]' |
|
55 process_images '[Tt][Ii][Ff][Ff]' |
|
56 |
|
57 exit 0 |