|
1 #!/bin/bash -e |
|
2 # |
|
3 # Copyright (c) 2013, Tomas Zeman <tzeman@volny.cz> |
|
4 # All rights reserved. |
|
5 # |
|
6 # Redistribution and use in source and binary forms, with or without |
|
7 # modification, are permitted providing that the following conditions |
|
8 # are met: |
|
9 # 1. Redistributions of source code must retain the above copyright |
|
10 # notice, this list of conditions and the following disclaimer. |
|
11 # 2. Redistributions in binary form must reproduce the above copyright |
|
12 # notice, this list of conditions and the following disclaimer in the |
|
13 # documentation and/or other materials provided with the distribution. |
|
14 # |
|
15 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR |
|
16 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
17 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
18 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
|
19 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|
20 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
|
21 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|
22 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
|
23 # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
|
24 # IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
25 # POSSIBILITY OF SUCH DAMAGE. |
|
26 # |
|
27 |
|
28 mysql_cmd=${MYSQLCMD:-"mysql test"} |
|
29 imgfolder=${IMGFOLDER:-"./images"} |
|
30 imgtable=${IMGTABLE:-"images"} |
|
31 |
|
32 [ -d $imgfolder ] || mkdir -p $imgfolder |
|
33 |
|
34 mfile=`mktemp -t m2m.XXXXXX` |
|
35 mdir=`mktemp -d -t m2m.XXXXXX` |
|
36 |
|
37 cleanup() |
|
38 { |
|
39 rm -fr $mfile $mdir |
|
40 } |
|
41 |
|
42 trap cleanup EXIT |
|
43 |
|
44 cat > $mfile |
|
45 |
|
46 munpack -q -C $mdir < $mfile > /dev/null |
|
47 |
|
48 mfrom=$(822field from < $mfile | sed -e 's/^ \+//') |
|
49 mdate=$(822date < $mfile | head -2 | tail -1 | sed -e 's/ \++[0-9]\+$//') |
|
50 mmsgid=$(822field message-id < $mfile | sed -e 's/^ \+//') |
|
51 |
|
52 process_images() |
|
53 { |
|
54 pat="*.$1" |
|
55 while IFS= read -d $'\0' -r f; do |
|
56 mt=$(file -b --mime-type "$f") |
|
57 if [ `expr match "$mt" "image/"` -gt 0 ]; then |
|
58 on=$(basename "$f") |
|
59 ext=$(echo "${on##*.}" | tr [A-Z] [a-z]) |
|
60 fn="$(date '+%s.%N').$$.$ext" |
|
61 ts=$(date '+%F %T') |
|
62 cat "$f" > $imgfolder/$fn.tmp |
|
63 mv $imgfolder/$fn.tmp $imgfolder/$fn |
|
64 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 |
|
65 fi |
|
66 done < <(find $mdir -type f -name "$pat" -print0) |
|
67 } |
|
68 |
|
69 process_images '[Jj][Pp][Gg]' |
|
70 process_images '[Pp][Nn][Gg]' |
|
71 process_images '[Tt][Ii][Ff][Ff]' |
|
72 |
|
73 exit 0 |