delete download when stopping

This commit is contained in:
SpikeHD
2022-05-12 22:11:47 -07:00
parent 1eb2ae02cc
commit 9085424eb6
7 changed files with 51 additions and 5 deletions

1
src-tauri/Cargo.lock generated
View File

@@ -643,6 +643,7 @@ version = "0.1.0"
dependencies = [
"futures-util",
"hudsucker",
"lazy_static",
"opener",
"registry",
"reqwest",

View File

@@ -19,6 +19,9 @@ serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
tauri = { version = "1.0.0-rc.9", features = ["api-all"] }
# For creating a "global" downloads list
lazy_static = "1.4.0"
# Access to the Windows Registry.
registry = "1.2.1"
# Program opener.

View File

@@ -1,12 +1,26 @@
use lazy_static::lazy_static;
use tauri::App;
use tauri::Manager;
use std::sync::Mutex;
use std::cmp::min;
use std::collections::HashMap;
use std::fs::File;
use std::io::Write;
use futures_util::StreamExt;
// This will create a downloads list that will be used to check if we should continue downloading the file
lazy_static!{
static ref DOWNLOADS: Mutex<Vec<String>> = {
let mut m = Vec::new();
Mutex::new(m)
};
}
// Lots of help from: https://gist.github.com/giuliano-oliveira/4d11d6b3bb003dba3a1b53f43d81b30d
// and docs ofc
#[tauri::command]
pub async fn download_file(window: tauri::Window, url: &str, path: &str) -> Result<(), String> {
// Reqwest setup
@@ -35,6 +49,9 @@ pub async fn download_file(window: tauri::Window, url: &str, path: &str) -> Resu
// File stream
let mut stream = res.bytes_stream();
// Assuming all goes well, add to the downloads list
DOWNLOADS.lock().unwrap().push(path.to_string());
// Await chunks
while let Some(item) = stream.next().await {
let chunk = match item {
@@ -101,4 +118,21 @@ pub fn emit_download_err(window: tauri::Window, msg: std::string::String, path:
);
window.emit("download_error", &res_hash).unwrap();
}
#[tauri::command]
pub fn stop_download(path: String) {
// Check if the path is in the downloads list
let mut downloads = DOWNLOADS.lock().unwrap();
let index = downloads.iter().position(|x| x == &path);
// Remove from list
if let Some(i) = index {
downloads.remove(i);
}
// Delete the file from disk
if let Err(_e) = std::fs::remove_file(&path) {
// Do nothing
}
}

View File

@@ -16,6 +16,7 @@ fn main() {
disconnect,
run_program,
downloader::download_file,
downloader::stop_download,
lang::get_lang
])
.run(tauri::generate_context!())