fix(backend): replace blocking std::fs with async tokio::fs

This commit is contained in:
spinline
2026-02-05 18:04:30 +03:00
parent 4c728ad066
commit 661d18979b

View File

@@ -189,7 +189,9 @@ async fn delete_torrent_with_data(
})?;
// Resolve Paths (Canonicalize) to prevent .. traversal and symlink attacks
let root_path = std::fs::canonicalize(std::path::Path::new(&root_path_str)).map_err(|e| {
let root_path = tokio::fs::canonicalize(std::path::Path::new(&root_path_str))
.await
.map_err(|e| {
(
StatusCode::INTERNAL_SERVER_ERROR,
format!("Invalid download root configuration (on server): {}", e),
@@ -198,7 +200,10 @@ async fn delete_torrent_with_data(
// Check if target path exists before trying to resolve it
let target_path_raw = std::path::Path::new(&path);
if !target_path_raw.exists() {
if !tokio::fs::try_exists(target_path_raw)
.await
.unwrap_or(false)
{
tracing::warn!(
"Data path not found: {:?}. Removing torrent only.",
target_path_raw
@@ -214,7 +219,9 @@ async fn delete_torrent_with_data(
return Ok("Torrent removed (Data not found)");
}
let target_path = std::fs::canonicalize(target_path_raw).map_err(|e| {
let target_path = tokio::fs::canonicalize(target_path_raw)
.await
.map_err(|e| {
(
StatusCode::INTERNAL_SERVER_ERROR,
format!("Invalid data path: {}", e),
@@ -258,9 +265,9 @@ async fn delete_torrent_with_data(
// 3. Delete Files via Native FS
let delete_result = if target_path.is_dir() {
std::fs::remove_dir_all(&target_path)
tokio::fs::remove_dir_all(&target_path).await
} else {
std::fs::remove_file(&target_path)
tokio::fs::remove_file(&target_path).await
};
match delete_result {
@@ -601,14 +608,18 @@ pub async fn set_global_limit_handler(
if let Some(down) = payload.max_download_rate {
// Convert bytes/s to KB/s (divide by 1024)
let down_kb = down / 1024;
tracing::info!("Setting download limit: {} bytes/s = {} KB/s", down, down_kb);
tracing::info!(
"Setting download limit: {} bytes/s = {} KB/s",
down,
down_kb
);
// Use set_kb with empty string as first param (throttle name), then value
if let Err(e) = client
.call("throttle.global_down.max_rate.set_kb", &[
RpcParam::from(""),
RpcParam::Int(down_kb),
])
.call(
"throttle.global_down.max_rate.set_kb",
&[RpcParam::from(""), RpcParam::Int(down_kb)],
)
.await
{
tracing::error!("Failed to set download limit: {}", e);
@@ -626,10 +637,10 @@ pub async fn set_global_limit_handler(
tracing::info!("Setting upload limit: {} bytes/s = {} KB/s", up, up_kb);
if let Err(e) = client
.call("throttle.global_up.max_rate.set_kb", &[
RpcParam::from(""),
RpcParam::Int(up_kb),
])
.call(
"throttle.global_up.max_rate.set_kb",
&[RpcParam::from(""), RpcParam::Int(up_kb)],
)
.await
{
tracing::error!("Failed to set upload limit: {}", e);