mailimage2mysql/m2m
author Tomas Zeman <tomas@functionals.cz>
Tue, 15 Dec 2020 09:22:21 +0100
changeset 60 4267602e8494
parent 34 bd1ec89560e0
permissions -rwxr-xr-x
fs2json: directory structure -> json object converter. E.g. to be used instead of erica / py-Couchapp (interaction with couchdb is left to the user).

#!/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