~miconoco/send-files

52728bc2ebf8ca60de2629efa3600f2a7f9fcd02 — Christoph Polcin 4 years ago 982ffe5
use pipes instead of temp files
1 files changed, 47 insertions(+), 22 deletions(-)

M send-files
M send-files => send-files +47 -22
@@ 42,37 42,62 @@ fi

[ $# -lt 1 ] || [ ! -e "$1" ] && _usage

# TODO remove tmp files and stream directly
_fname="$(echo "$_subject" | sed 's/[^-[:alnum:]]\+/-/g')"
_fname="${_fname#-}"
_fname="$(date +%F-%H%M)-${_fname%-}.${COMP}.gpg"

_tmp="$(mktemp --suffix=".${COMP}")"
_out="${_tmp}.gpg"
trap 'rm -- "$_tmp" "$_out"' EXIT
_archive() { # comp files...
	case "$1" in
		*gz)
			c=--gzip ;;
		*bz2|*bzip2)
			c=--bzip2 ;;
		*zstd)
			c=--zstd ;;
		*xz)
			c=--xz ;;
		*)
			c=--no-auto-compress
		;;
	esac

tar --sparse \
	--no-acls --no-xattrs \
	--exclude-caches --exclude-vcs-ignores \
	--auto-compress \
	-cvf "$_tmp" "$@"
	shift

pass "$SUBF/$_to" | gpg \
	--batch --passphrase-fd 0 \
	--cipher-algo "$ALGO" \
	--sign \
	--symmetric \
	--no-symkey-cache -o "${_out}" "$_tmp"
	tar --sparse \
		--no-acls --no-xattrs \
		--exclude-caches --exclude-vcs-ignores \
		-c "${c}" -v -f - -- "$@"
}

_fname="$(echo "$_subject" | sed 's/[^-[:alnum:]]\+/-/g')"
_fname="${_fname#-}"
_fname="$(date +%F-%H%M)-${_fname%-}.${COMP}.gpg"
_pass() {
	pass "$SUBF/$_to"
}

_encrypt() { # algo
	gpg --batch --passphrase-fd 3 \
		--cipher-algo "$1" \
		--sign \
		--symmetric \
		--no-symkey-cache \
3<<EOF
$(_pass)
EOF
}

cat <<EOF| $SEND
_email() {
cat <<EOF
To: ${_to}
Subject: ${_subject}
Mime-Version:1.0
Content-Type: $(mimetype -b "${_out}"); name="${_fname}"
Mime-Version: 1.0
Content-Type: application/pgp-encrypted; name="${_fname}"
Content-Description: ${_fname}
Content-Disposition: attachment; filename="${_fname}"
Content-Transfer-Encoding: base64

$(openssl base64 -e -in "$_out")
$(openssl base64 -e)
EOF
}

_pass > /dev/null 2>&1 || { echo "fail to get password for $_to" 1>&2; exit 1; }

_archive "$COMP" "$@" | _encrypt "$ALGO" | _email | $SEND