fix: tmp download file should be removed on cancel (#5849)
This commit is contained in:
parent
43b7eb6e18
commit
3afdd0fa1d
@ -185,10 +185,16 @@ pub async fn download_files(
|
|||||||
.cancel_tokens
|
.cancel_tokens
|
||||||
.insert(task_id.to_string(), cancel_token.clone());
|
.insert(task_id.to_string(), cancel_token.clone());
|
||||||
}
|
}
|
||||||
|
// TODO: Support resuming downloads when FE is ready
|
||||||
let result =
|
let result = _download_files_internal(
|
||||||
_download_files_internal(app.clone(), &items, &headers, task_id, cancel_token.clone())
|
app.clone(),
|
||||||
.await;
|
&items,
|
||||||
|
&headers,
|
||||||
|
task_id,
|
||||||
|
false,
|
||||||
|
cancel_token.clone(),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
|
||||||
// cleanup
|
// cleanup
|
||||||
{
|
{
|
||||||
@ -259,6 +265,7 @@ async fn _download_files_internal(
|
|||||||
items: &[DownloadItem],
|
items: &[DownloadItem],
|
||||||
headers: &HashMap<String, String>,
|
headers: &HashMap<String, String>,
|
||||||
task_id: &str,
|
task_id: &str,
|
||||||
|
resume: bool,
|
||||||
cancel_token: CancellationToken,
|
cancel_token: CancellationToken,
|
||||||
) -> Result<(), String> {
|
) -> Result<(), String> {
|
||||||
log::info!("Start download task: {}", task_id);
|
log::info!("Start download task: {}", task_id);
|
||||||
@ -318,7 +325,8 @@ async fn _download_files_internal(
|
|||||||
let tmp_save_path = save_path.with_extension(append_extension("tmp"));
|
let tmp_save_path = save_path.with_extension(append_extension("tmp"));
|
||||||
let url_save_path = save_path.with_extension(append_extension("url"));
|
let url_save_path = save_path.with_extension(append_extension("url"));
|
||||||
|
|
||||||
let mut resume = tmp_save_path.exists()
|
let mut should_resume = resume
|
||||||
|
&& tmp_save_path.exists()
|
||||||
&& tokio::fs::read_to_string(&url_save_path)
|
&& tokio::fs::read_to_string(&url_save_path)
|
||||||
.await
|
.await
|
||||||
.map(|url| url == item.url) // check if we resume the same URL
|
.map(|url| url == item.url) // check if we resume the same URL
|
||||||
@ -331,7 +339,7 @@ async fn _download_files_internal(
|
|||||||
log::info!("Started downloading: {}", item.url);
|
log::info!("Started downloading: {}", item.url);
|
||||||
let client = _get_client_for_item(item, &header_map).map_err(err_to_string)?;
|
let client = _get_client_for_item(item, &header_map).map_err(err_to_string)?;
|
||||||
let mut download_delta = 0u64;
|
let mut download_delta = 0u64;
|
||||||
let resp = if resume {
|
let resp = if should_resume {
|
||||||
let downloaded_size = tmp_save_path.metadata().map_err(err_to_string)?.len();
|
let downloaded_size = tmp_save_path.metadata().map_err(err_to_string)?.len();
|
||||||
match _get_maybe_resume(&client, &item.url, downloaded_size).await {
|
match _get_maybe_resume(&client, &item.url, downloaded_size).await {
|
||||||
Ok(resp) => {
|
Ok(resp) => {
|
||||||
@ -346,7 +354,7 @@ async fn _download_files_internal(
|
|||||||
Err(e) => {
|
Err(e) => {
|
||||||
// fallback to normal download
|
// fallback to normal download
|
||||||
log::warn!("Failed to resume download: {}", e);
|
log::warn!("Failed to resume download: {}", e);
|
||||||
resume = false;
|
should_resume = false;
|
||||||
_get_maybe_resume(&client, &item.url, 0).await?
|
_get_maybe_resume(&client, &item.url, 0).await?
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -355,7 +363,7 @@ async fn _download_files_internal(
|
|||||||
};
|
};
|
||||||
let mut stream = resp.bytes_stream();
|
let mut stream = resp.bytes_stream();
|
||||||
|
|
||||||
let file = if resume {
|
let file = if should_resume {
|
||||||
// resume download, append to existing file
|
// resume download, append to existing file
|
||||||
tokio::fs::OpenOptions::new()
|
tokio::fs::OpenOptions::new()
|
||||||
.write(true)
|
.write(true)
|
||||||
@ -372,6 +380,11 @@ async fn _download_files_internal(
|
|||||||
// write chunk to file
|
// write chunk to file
|
||||||
while let Some(chunk) = stream.next().await {
|
while let Some(chunk) = stream.next().await {
|
||||||
if cancel_token.is_cancelled() {
|
if cancel_token.is_cancelled() {
|
||||||
|
if !should_resume {
|
||||||
|
tokio::fs::remove_dir_all(&save_path.parent().unwrap())
|
||||||
|
.await
|
||||||
|
.ok();
|
||||||
|
}
|
||||||
log::info!("Download cancelled for task: {}", task_id);
|
log::info!("Download cancelled for task: {}", task_id);
|
||||||
app.emit(&evt_name, evt.clone()).unwrap();
|
app.emit(&evt_name, evt.clone()).unwrap();
|
||||||
return Ok(());
|
return Ok(());
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user