I'm probably nitpicking, but there's a predictable tempfile naming vulnerability here; maybe make up some random data to append, instead?