mailimage2mysql/m2m
changeset 38 3afc2ae852e5
parent 34 bd1ec89560e0
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mailimage2mysql/m2m	Wed Nov 06 14:34:41 2013 +0100
@@ -0,0 +1,57 @@
+#!/bin/bash -e
+#
+# (c) 2013, Tomas Zeman <tzeman@volny.cz>
+#
+# License:
+#  This program is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+#
+
+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
+			chmod 644 $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