verify downloads before installing them
This commit is contained in:
+34
-20
@@ -275,9 +275,8 @@ async fn download_update(
|
||||
state: State<'_, AppState>,
|
||||
url: String,
|
||||
) -> Result<(), String> {
|
||||
let dest = updates::pending_update_path(&state.app_dir);
|
||||
let app_handle = app.clone();
|
||||
updates::download_to(&url, &dest, move |downloaded, total| {
|
||||
updates::download(&url, &state.app_dir, move |downloaded, total| {
|
||||
drop(app_handle.emit(
|
||||
"update-download-progress",
|
||||
UpdateDownloadProgress { downloaded, total },
|
||||
@@ -295,17 +294,12 @@ async fn download_update(
|
||||
)]
|
||||
#[tauri::command]
|
||||
fn apply_update(app: AppHandle, state: State<'_, AppState>) -> Result<(), String> {
|
||||
use std::os::windows::process::CommandExt;
|
||||
use std::process::Command;
|
||||
if !updates::has_pending_update(&state.app_dir) {
|
||||
return Err("no pending update to apply".to_string());
|
||||
if lock(&state.session).is_some() || state.exporting.load(Ordering::SeqCst)
|
||||
{
|
||||
return Err("finish or discard the current recording before applying the update".to_string());
|
||||
}
|
||||
let bat = updates::write_apply_script(&state.app_dir).map_err(|e| format!("{e:#}"))?;
|
||||
Command::new("cmd")
|
||||
.args(["/c", "start", "", ""])
|
||||
.raw_arg(format!("\"{}\"", bat.to_string_lossy()))
|
||||
.spawn()
|
||||
.map_err(|e| format!("spawning apply script: {e:#}"))?;
|
||||
let _operation = updates::UpdateOperation::begin().map_err(|e| format!("{e:#}"))?;
|
||||
updates::start_install(&state.app_dir).map_err(|e| format!("{e:#}"))?;
|
||||
app.exit(0);
|
||||
Ok(())
|
||||
}
|
||||
@@ -1925,18 +1919,38 @@ fn reveal_in_explorer(path: String) -> Result<(), String> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn open_shell_target(target: &std::ffi::OsStr) -> Result<(), String> {
|
||||
use windows::core::{w, HSTRING, PCWSTR};
|
||||
use windows::Win32::UI::Shell::ShellExecuteW;
|
||||
use windows::Win32::UI::WindowsAndMessaging::SW_SHOWNORMAL;
|
||||
|
||||
let target = HSTRING::from(target);
|
||||
// SAFETY: Both NUL-terminated strings outlive the call.
|
||||
let result = unsafe {
|
||||
ShellExecuteW(
|
||||
None,
|
||||
w!("open"),
|
||||
&target,
|
||||
PCWSTR::null(),
|
||||
PCWSTR::null(),
|
||||
SW_SHOWNORMAL,
|
||||
)
|
||||
};
|
||||
// ShellExecuteW returns an error code when the result is 32 or lower.
|
||||
let code = result.0 as isize;
|
||||
if code <= 32 {
|
||||
return Err(format!("opening the target failed (Windows shell error {code})"));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn open_url(url: String) -> Result<(), String> {
|
||||
use std::os::windows::process::CommandExt;
|
||||
|
||||
if !url.starts_with("http://") && !url.starts_with("https://") {
|
||||
let url = reqwest::Url::parse(&url).map_err(|e| format!("invalid URL: {e}"))?;
|
||||
if !matches!(url.scheme(), "http" | "https") || !url.has_host() {
|
||||
return Err("only http/https URLs are supported".to_string());
|
||||
}
|
||||
std::process::Command::new("cmd")
|
||||
.raw_arg(format!("/C start \"\" \"{url}\""))
|
||||
.spawn()
|
||||
.map_err(|e| format!("opening url: {e:#}"))?;
|
||||
Ok(())
|
||||
open_shell_target(std::ffi::OsStr::new(url.as_str()))
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
|
||||
Reference in New Issue
Block a user