diff options
| author | jakka <jakkadoujin@gmail.com> | 2025-06-22 14:42:27 +0300 |
|---|---|---|
| committer | jakka <jakkadoujin@gmail.com> | 2025-06-22 14:42:27 +0300 |
| commit | 2a60a5f68f877cb05dd071703e456dbf050eb8c9 (patch) | |
| tree | 71975120e98690d20d21de588246f7d65bae6187 /src/files.rs | |
| parent | 1769f337c423cbe0248e0fd65f8c50e0bce0d845 (diff) | |
better reencoding logic
Diffstat (limited to 'src/files.rs')
| -rw-r--r-- | src/files.rs | 62 |
1 files changed, 31 insertions, 31 deletions
diff --git a/src/files.rs b/src/files.rs index 764de56..8053da2 100644 --- a/src/files.rs +++ b/src/files.rs @@ -92,35 +92,34 @@ pub async fn index_files_recursively( .with_style(ProgressStyle::with_template(BAR_TEMPLATE)?.progress_chars("#>-")) .with_message("Indexing"); - let _ = WalkDir::new(abspath) - .into_iter() - .map(|file| { - let path = file.unwrap().into_path(); - if path.is_file() { - if let Some(ext) = path.extension() { - if ext == "flac" { - let newconn = conn.clone(); - let newtoken = canceltoken.clone(); - #[cfg(not(test))] - let newbar = bar.clone(); - tasks.spawn(async move { - tokio::select! { - _ = newtoken.cancelled() => Ok(()), - res = async { - handle_file(path, newconn).await?; - #[cfg(not(test))] - newbar.inc(1); - Ok(()) - } => res - } - }); + for entry in WalkDir::new(abspath) { + let path = entry?.into_path(); + if !path.is_file() { + continue; + } + + if path.extension().is_some_and(|x| x == "flac") { + let newconn = conn.clone(); + let newtoken = canceltoken.clone(); + #[cfg(not(test))] + let newbar = bar.clone(); + + tasks.spawn(async move { + tokio::select! { + _ = newtoken.cancelled() => Ok(()), + res = async { + handle_file(path, newconn).await?; #[cfg(not(test))] - bar.inc_length(1); - } + newbar.inc(1); + Ok(()) + } => res } - } - }) - .collect::<Vec<_>>(); + }); + + #[cfg(not(test))] + bar.inc_length(1); + } + } while let Some(task) = tasks.join_next().await { match task { @@ -132,9 +131,9 @@ pub async fn index_files_recursively( #[cfg(not(test))] { if canceltoken.is_cancelled() { - bar.abandon_with_message("Indexing abandoned"); + bar.abandon_with_message("Indexing aborted"); } else { - bar.finish_with_message("Finished encoding"); + bar.finish_with_message("Finished indexing"); } } Ok(()) @@ -171,9 +170,10 @@ pub async fn reencode_files(conn: &Database, canceltoken: CancellationToken) -> } res = async { if let Err(error) = tokio::task::spawn_blocking(move || encode_file(file)).await? { + let _ = std::fs::remove_file(filename.with_extension("tmp.metadata_edit")); + let _ = std::fs::remove_file(filename.with_extension("tmp")); return Err(anyhow!(FileError::new(&filename, error))); }; - if let Err(error) = newconn.update_file(&filename).await { return Err(anyhow!(FileError::new(&filename, error))); }; @@ -197,7 +197,7 @@ pub async fn reencode_files(conn: &Database, canceltoken: CancellationToken) -> #[cfg(not(test))] { if canceltoken.is_cancelled() { - bar.abandon_with_message("Reencoding abandoned"); + bar.abandon_with_message("Reencoding aborted"); } else { bar.finish_with_message("Finished encoding"); } |
