diff -r edd04723b99a -r 27a0248f5f27 mailimage2mysql/m2m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mailimage2mysql/m2m Thu Sep 19 13:23:11 2013 +0200 @@ -0,0 +1,73 @@ +#!/bin/bash -e +# +# Copyright (c) 2013, Tomas Zeman +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted providing that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR +# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY +# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING +# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# + +mysql_cmd=${MYSQLCMD:-"mysql test"} +imgfolder=${IMGFOLDER:-"./images"} +imgtable=${IMGTABLE:-"images"} + +[ -d $imgfolder ] || mkdir -p $imgfolder + +mfile=`mktemp -t m2m.XXXXXX` +mdir=`mktemp -d -t m2m.XXXXXX` + +cleanup() +{ + rm -fr $mfile $mdir +} + +trap cleanup EXIT + +cat > $mfile + +munpack -q -C $mdir < $mfile > /dev/null + +mfrom=$(822field from < $mfile | sed -e 's/^ \+//') +mdate=$(822date < $mfile | head -2 | tail -1 | sed -e 's/ \++[0-9]\+$//') +mmsgid=$(822field message-id < $mfile | sed -e 's/^ \+//') + +process_images() +{ + pat="*.$1" + while IFS= read -d $'\0' -r f; do + mt=$(file -b --mime-type "$f") + if [ `expr match "$mt" "image/"` -gt 0 ]; then + on=$(basename "$f") + ext=$(echo "${on##*.}" | tr [A-Z] [a-z]) + fn="$(date '+%s.%N').$$.$ext" + ts=$(date '+%F %T') + cat "$f" > $imgfolder/$fn.tmp + mv $imgfolder/$fn.tmp $imgfolder/$fn + 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 + fi + done < <(find $mdir -type f -name "$pat" -print0) +} + +process_images '[Jj][Pp][Gg]' +process_images '[Pp][Nn][Gg]' +process_images '[Tt][Ii][Ff][Ff]' + +exit 0