melib/compose: don't base64 encode unless it's not ascii

jmap-eventsource
Manos Pitsidianakis 2020-10-09 11:51:34 +03:00
parent 59e60f8d28
commit afe7eed9ef
Signed by: Manos Pitsidianakis
GPG Key ID: 73627C2F690DF710
1 changed files with 24 additions and 14 deletions

View File

@ -287,14 +287,14 @@ impl Draft {
fn build_multipart(ret: &mut String, kind: MultipartType, parts: Vec<AttachmentBuilder>) { fn build_multipart(ret: &mut String, kind: MultipartType, parts: Vec<AttachmentBuilder>) {
let boundary = ContentType::make_boundary(&parts); let boundary = ContentType::make_boundary(&parts);
ret.extend( ret.push_str(&format!(
format!( r#"Content-Type: {}; charset="utf-8"; boundary="{}""#,
"Content-Type: {}; charset=\"utf-8\"; boundary=\"{}\"\r\n", kind, boundary
kind, boundary ));
) if kind == MultipartType::Encrypted {
.chars(), ret.push_str(r#"; protocol="application/pgp-encrypted""#);
); }
ret.push_str("\r\n"); ret.push_str("\r\n\r\n");
/* rfc1341 */ /* rfc1341 */
ret.push_str("This is a MIME formatted message with attachments. Use a MIME-compliant client to view it properly.\r\n"); ret.push_str("This is a MIME formatted message with attachments. Use a MIME-compliant client to view it properly.\r\n");
for sub in parts { for sub in parts {
@ -325,14 +325,14 @@ fn print_attachment(ret: &mut String, a: AttachmentBuilder) {
ret.push_str("\r\n"); ret.push_str("\r\n");
} }
Multipart { Multipart {
boundary: _boundary, boundary: _,
kind, kind,
parts: subparts, parts,
} => { } => {
build_multipart( build_multipart(
ret, ret,
kind, kind,
subparts parts
.into_iter() .into_iter()
.map(|s| s.into()) .map(|s| s.into())
.collect::<Vec<AttachmentBuilder>>(), .collect::<Vec<AttachmentBuilder>>(),
@ -361,8 +361,11 @@ fn print_attachment(ret: &mut String, a: AttachmentBuilder) {
ret.push_str("\r\n"); ret.push_str("\r\n");
} }
_ => { _ => {
let content_transfer_encoding: ContentTransferEncoding = let content_transfer_encoding: ContentTransferEncoding = if a.raw().is_ascii() {
ContentTransferEncoding::Base64; ContentTransferEncoding::_8Bit
} else {
ContentTransferEncoding::Base64
};
if let Some(name) = a.content_type().name() { if let Some(name) = a.content_type().name() {
ret.extend( ret.extend(
format!( format!(
@ -386,7 +389,14 @@ fn print_attachment(ret: &mut String, a: AttachmentBuilder) {
.chars(), .chars(),
); );
ret.push_str("\r\n"); ret.push_str("\r\n");
ret.push_str(&BASE64_MIME.encode(a.raw()).trim()); if content_transfer_encoding == ContentTransferEncoding::Base64 {
ret.push_str(&BASE64_MIME.encode(a.raw()).trim());
} else {
for l in unsafe { std::str::from_utf8_unchecked(a.raw()) }.lines() {
ret.push_str(l);
ret.push_str("\r\n");
}
}
ret.push_str("\r\n"); ret.push_str("\r\n");
} }
} }