forked from meli/meli
1
Fork 0

Set file extensions to temp files, use `open` in macos

If html_filter fails, meli unwraps it. Also, if it can't find an xdg default app it also fails.

So use xdg-open and open as failsaifes.

But that requires `open` to know it's an html file, so implemented setting temp file extensions as well.
master
Manos Pitsidianakis 2023-05-30 21:36:24 +03:00
parent aebff3d3d9
commit 954329d848
7 changed files with 36 additions and 22 deletions

View File

@ -1774,6 +1774,7 @@ impl Component for Composer {
self.draft.to_edit_string().as_str().as_bytes(), self.draft.to_edit_string().as_str().as_bytes(),
None, None,
None, None,
Some("eml"),
true, true,
); );
@ -1867,7 +1868,7 @@ impl Component for Composer {
)); ));
return false; return false;
} }
let f = create_temp_file(&[], None, None, true); let f = create_temp_file(&[], None, None, None, true);
match Command::new("sh") match Command::new("sh")
.args(["-c", command]) .args(["-c", command])
.stdin(Stdio::null()) .stdin(Stdio::null())
@ -2409,7 +2410,7 @@ pub fn send_draft_async(
)))) ))))
.unwrap(); .unwrap();
} else if !store_sent_mail && is_ok { } else if !store_sent_mail && is_ok {
let f = create_temp_file(message.as_bytes(), None, None, false); let f = create_temp_file(message.as_bytes(), None, None, Some("eml"), false);
log::info!( log::info!(
"store_sent_mail is false; stored sent mail to {}", "store_sent_mail is false; stored sent mail to {}",
f.path().display() f.path().display()

View File

@ -796,7 +796,11 @@ impl MailView {
.args(["-c", filter_invocation]) .args(["-c", filter_invocation])
.stdin(Stdio::piped()) .stdin(Stdio::piped())
.stdout(Stdio::piped()) .stdout(Stdio::piped())
.spawn(); .spawn()
.and_then(|mut cmd| {
cmd.stdin.as_mut().unwrap().write_all(&bytes)?;
Ok(String::from_utf8_lossy(&cmd.wait_with_output()?.stdout).to_string())
});
match command_obj { match command_obj {
Err(err) => { Err(err) => {
context.replies.push_back(UIEvent::Notification( context.replies.push_back(UIEvent::Notification(
@ -819,21 +823,11 @@ impl MailView {
text, text,
}); });
} }
Ok(mut html_filter) => { Ok(text) => {
html_filter
.stdin
.as_mut()
.unwrap()
.write_all(&bytes)
.expect("Failed to write to stdin");
let comment = Some(format!( let comment = Some(format!(
"Text piped through `{}`. Press `v` to open in web browser. \n\n", "Text piped through `{}`. Press `v` to open in web browser. \n\n",
filter_invocation filter_invocation
)); ));
let text = String::from_utf8_lossy(
&html_filter.wait_with_output().unwrap().stdout,
)
.to_string();
acc.push(AttachmentDisplay::InlineText { acc.push(AttachmentDisplay::InlineText {
inner: Box::new(a.clone()), inner: Box::new(a.clone()),
comment, comment,
@ -2306,6 +2300,7 @@ impl Component for MailView {
&attachment.decode(Default::default()), &attachment.decode(Default::default()),
filename.as_deref(), filename.as_deref(),
None, None,
None,
true, true,
); );
let exec_cmd = desktop_exec_to_command( let exec_cmd = desktop_exec_to_command(

View File

@ -443,6 +443,7 @@ impl Component for EnvelopeView {
&u.decode(Default::default()), &u.decode(Default::default()),
filename.as_deref(), filename.as_deref(),
None, None,
None,
true, true,
); );
let exec_cmd = super::desktop_exec_to_command( let exec_cmd = super::desktop_exec_to_command(

View File

@ -152,8 +152,15 @@ impl Component for HtmlView {
} else { } else {
query_default_app("text/html").ok() query_default_app("text/html").ok()
}; };
let command = if cfg!(target_os = "macos") {
command.or_else(|| Some("open".into()))
} else if cfg!(target_os = "linux") {
command.or_else(|| Some("xdg-open".into()))
} else {
command
};
if let Some(command) = command { if let Some(command) = command {
let p = create_temp_file(&self.bytes, None, None, true); let p = create_temp_file(&self.bytes, None, None, Some("html"), true);
let exec_cmd = let exec_cmd =
super::desktop_exec_to_command(&command, p.path.display().to_string(), false); super::desktop_exec_to_command(&command, p.path.display().to_string(), false);
match Command::new("sh") match Command::new("sh")

View File

@ -1231,7 +1231,7 @@ impl Account {
if let Some(mailbox_hash) = saved_at { if let Some(mailbox_hash) = saved_at {
Ok(mailbox_hash) Ok(mailbox_hash)
} else { } else {
let file = crate::types::create_temp_file(bytes, None, None, false); let file = crate::types::create_temp_file(bytes, None, None, Some("eml"), false);
debug!("message saved in {}", file.path.display()); debug!("message saved in {}", file.path.display());
log::info!( log::info!(
"Message was stored in {} so that you can restore it manually.", "Message was stored in {} so that you can restore it manually.",
@ -1873,7 +1873,8 @@ impl Account {
} => { } => {
if let Ok(Some(Err(err))) = handle.chan.try_recv() { if let Ok(Some(Err(err))) = handle.chan.try_recv() {
log::error!("Could not save message: {err}"); log::error!("Could not save message: {err}");
let file = crate::types::create_temp_file(bytes, None, None, false); let file =
crate::types::create_temp_file(bytes, None, None, Some("eml"), false);
log::debug!("message saved in {}", file.path.display()); log::debug!("message saved in {}", file.path.display());
log::info!( log::info!(
"Message was stored in {} so that you can restore it manually.", "Message was stored in {} so that you can restore it manually.",

View File

@ -165,8 +165,13 @@ impl MailcapEntry {
.map(|arg| match *arg { .map(|arg| match *arg {
"%s" => { "%s" => {
needs_stdin = false; needs_stdin = false;
let _f = let _f = create_temp_file(
create_temp_file(&a.decode(Default::default()), None, None, true); &a.decode(Default::default()),
None,
None,
None,
true,
);
let p = _f.path().display().to_string(); let p = _f.path().display().to_string();
f = Some(_f); f = Some(_f);
p p

View File

@ -72,7 +72,8 @@ impl File {
pub fn create_temp_file( pub fn create_temp_file(
bytes: &[u8], bytes: &[u8],
filename: Option<&str>, filename: Option<&str>,
path: Option<&PathBuf>, path: Option<&mut PathBuf>,
extension: Option<&str>,
delete_on_drop: bool, delete_on_drop: bool,
) -> File { ) -> File {
let mut dir = std::env::temp_dir(); let mut dir = std::env::temp_dir();
@ -89,10 +90,13 @@ pub fn create_temp_file(
let u = Uuid::new_v4(); let u = Uuid::new_v4();
dir.push(u.as_hyphenated().to_string()); dir.push(u.as_hyphenated().to_string());
} }
&dir &mut dir
}); });
if let Some(ext) = extension {
path.set_extension(ext);
}
let mut f = std::fs::File::create(path).unwrap(); let mut f = std::fs::File::create(&path).unwrap();
let metadata = f.metadata().unwrap(); let metadata = f.metadata().unwrap();
let mut permissions = metadata.permissions(); let mut permissions = metadata.permissions();