Sanitize the filename given to writeToTemporaryFileWithName.
Fold whitespace when removing unsafe characters, and trim whitespace at the
ends of each filename part.