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

Binary file not shown.

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!())

View File

@@ -25,6 +25,7 @@
.ProgressText {
color: #c5c5c5;
padding: 0px 10px;
width: 30%;
}
.MainProgressBarWrapper {
@@ -46,7 +47,7 @@
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
justify-content: flex-start;
}
.DownloadControls {
@@ -64,4 +65,8 @@
.DownloadControls div img {
height: 100%;
}
.downloadStop:hover {
cursor: pointer;
}

View File

@@ -25,6 +25,8 @@ export default class ProgressBar extends React.Component<IProps, IState> {
status: '',
total: this.props.downloadManager.getDownloadProgress(this.props.path)?.total || 0,
}
this.stopDownload = this.stopDownload.bind(this)
}
componentDidMount() {
@@ -33,7 +35,7 @@ export default class ProgressBar extends React.Component<IProps, IState> {
const prog = this.props.downloadManager.getDownloadProgress(this.props.path)
this.setState({
progress: prog?.progress || 0,
status: prog?.status || 'error',
status: prog?.status || 'stopped',
total: prog?.total || 0,
})
@@ -71,7 +73,7 @@ export default class ProgressBar extends React.Component<IProps, IState> {
}}></div>
</div>
<div className="DownloadControls">
<div onClick={this.stopDownload}>
<div onClick={this.stopDownload} className="downloadStop">
<img src={Stop}></img>
</div>
</div>
@@ -79,7 +81,7 @@ export default class ProgressBar extends React.Component<IProps, IState> {
<div className="ProgressText">
{capitalize(this.state.status)}
{capitalize(this.state.status) || 'Waiting'}
</div>
</div>
)