fix(backend): replace blocking std::fs with async tokio::fs
This commit is contained in:
@@ -189,16 +189,21 @@ async fn delete_torrent_with_data(
|
|||||||
})?;
|
})?;
|
||||||
|
|
||||||
// Resolve Paths (Canonicalize) to prevent .. traversal and symlink attacks
|
// 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
|
||||||
StatusCode::INTERNAL_SERVER_ERROR,
|
.map_err(|e| {
|
||||||
format!("Invalid download root configuration (on server): {}", e),
|
(
|
||||||
)
|
StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
})?;
|
format!("Invalid download root configuration (on server): {}", e),
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
|
||||||
// Check if target path exists before trying to resolve it
|
// Check if target path exists before trying to resolve it
|
||||||
let target_path_raw = std::path::Path::new(&path);
|
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!(
|
tracing::warn!(
|
||||||
"Data path not found: {:?}. Removing torrent only.",
|
"Data path not found: {:?}. Removing torrent only.",
|
||||||
target_path_raw
|
target_path_raw
|
||||||
@@ -214,12 +219,14 @@ async fn delete_torrent_with_data(
|
|||||||
return Ok("Torrent removed (Data not found)");
|
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
|
||||||
StatusCode::INTERNAL_SERVER_ERROR,
|
.map_err(|e| {
|
||||||
format!("Invalid data path: {}", e),
|
(
|
||||||
)
|
StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
})?;
|
format!("Invalid data path: {}", e),
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
|
||||||
tracing::info!(
|
tracing::info!(
|
||||||
"Delete request: Target='{:?}', Root='{:?}'",
|
"Delete request: Target='{:?}', Root='{:?}'",
|
||||||
@@ -258,9 +265,9 @@ async fn delete_torrent_with_data(
|
|||||||
|
|
||||||
// 3. Delete Files via Native FS
|
// 3. Delete Files via Native FS
|
||||||
let delete_result = if target_path.is_dir() {
|
let delete_result = if target_path.is_dir() {
|
||||||
std::fs::remove_dir_all(&target_path)
|
tokio::fs::remove_dir_all(&target_path).await
|
||||||
} else {
|
} else {
|
||||||
std::fs::remove_file(&target_path)
|
tokio::fs::remove_file(&target_path).await
|
||||||
};
|
};
|
||||||
|
|
||||||
match delete_result {
|
match delete_result {
|
||||||
@@ -601,14 +608,18 @@ pub async fn set_global_limit_handler(
|
|||||||
if let Some(down) = payload.max_download_rate {
|
if let Some(down) = payload.max_download_rate {
|
||||||
// Convert bytes/s to KB/s (divide by 1024)
|
// Convert bytes/s to KB/s (divide by 1024)
|
||||||
let down_kb = down / 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
|
// Use set_kb with empty string as first param (throttle name), then value
|
||||||
if let Err(e) = client
|
if let Err(e) = client
|
||||||
.call("throttle.global_down.max_rate.set_kb", &[
|
.call(
|
||||||
RpcParam::from(""),
|
"throttle.global_down.max_rate.set_kb",
|
||||||
RpcParam::Int(down_kb),
|
&[RpcParam::from(""), RpcParam::Int(down_kb)],
|
||||||
])
|
)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
tracing::error!("Failed to set download limit: {}", e);
|
tracing::error!("Failed to set download limit: {}", e);
|
||||||
@@ -624,12 +635,12 @@ pub async fn set_global_limit_handler(
|
|||||||
// Convert bytes/s to KB/s
|
// Convert bytes/s to KB/s
|
||||||
let up_kb = up / 1024;
|
let up_kb = up / 1024;
|
||||||
tracing::info!("Setting upload limit: {} bytes/s = {} KB/s", up, up_kb);
|
tracing::info!("Setting upload limit: {} bytes/s = {} KB/s", up, up_kb);
|
||||||
|
|
||||||
if let Err(e) = client
|
if let Err(e) = client
|
||||||
.call("throttle.global_up.max_rate.set_kb", &[
|
.call(
|
||||||
RpcParam::from(""),
|
"throttle.global_up.max_rate.set_kb",
|
||||||
RpcParam::Int(up_kb),
|
&[RpcParam::from(""), RpcParam::Int(up_kb)],
|
||||||
])
|
)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
tracing::error!("Failed to set upload limit: {}", e);
|
tracing::error!("Failed to set upload limit: {}", e);
|
||||||
|
|||||||
Reference in New Issue
Block a user