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.
pull/223/head
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(),
None,
None,
Some("eml"),
true,
);
@ -1867,7 +1868,7 @@ impl Component for Composer {
));
return false;
}
let f = create_temp_file(&[], None, None, true);
let f = create_temp_file(&[], None, None, None, true);
match Command::new("sh")
.args(["-c", command])
.stdin(Stdio::null())
@ -2409,7 +2410,7 @@ pub fn send_draft_async(
))))
.unwrap();
} 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!(
"store_sent_mail is false; stored sent mail to {}",
f.path().display()

View File

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

View File

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

View File

@ -152,8 +152,15 @@ impl Component for HtmlView {
} else {
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 {
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 =
super::desktop_exec_to_command(&command, p.path.display().to_string(), false);
match Command::new("sh")

View File

@ -1231,7 +1231,7 @@ impl Account {
if let Some(mailbox_hash) = saved_at {
Ok(mailbox_hash)
} 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());
log::info!(
"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() {
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::info!(
"Message was stored in {} so that you can restore it manually.",

View File

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

View File

@ -72,7 +72,8 @@ impl File {
pub fn create_temp_file(
bytes: &[u8],
filename: Option<&str>,
path: Option<&PathBuf>,
path: Option<&mut PathBuf>,
extension: Option<&str>,
delete_on_drop: bool,
) -> File {
let mut dir = std::env::temp_dir();
@ -89,10 +90,13 @@ pub fn create_temp_file(
let u = Uuid::new_v4();
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 mut permissions = metadata.permissions();