From b9aec6ef869142de2da329ac1838c0713a4853ff Mon Sep 17 00:00:00 2001 From: caprain Date: Sun, 19 Jul 2026 15:50:50 +0800 Subject: [PATCH] =?UTF-8?q?fix=EF=BC=9A=20=E5=AE=89=E5=8D=93=E7=AB=AF?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E6=B6=88=E6=81=AF=E7=A6=BB=E7=BA=BF=E4=B8=8D?= =?UTF-8?q?=E8=A1=A5=E5=8F=91=E3=80=81=E7=A6=BB=E7=BA=BF=E7=94=A8=E6=88=B7?= =?UTF-8?q?=20Share=20Intent=20=E5=9B=BE=E7=89=87=E9=A2=84=E8=A7=88?= =?UTF-8?q?=E4=B8=8D=E6=B8=B2=E6=9F=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-tauri/src/commands.rs | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs index b717d77..0c375e4 100644 --- a/src-tauri/src/commands.rs +++ b/src-tauri/src/commands.rs @@ -1215,18 +1215,44 @@ pub async fn send_file_from_fd( "[Command] 用户 {} 处于离线记录中,直接保存文件消息为挂起状态", peerId ); - let _ = crate::db::save_file_message( + // 接管 FD 所有权,克隆一份用于缓存 + let android_file = AndroidFile::from_fd(fd)?; + let std_file = android_file.into_file(); + let dup = std_file.try_clone() + .map_err(|e| format!("FD 克隆失败: {}", e))?; + let cached_fd = dup.into_raw_fd(); + // 原始 std_file 在此 drop 关闭原始 FD,cached_fd 是独立的新 FD + drop(std_file); + + // 先保存消息获取 msg_id + let msg_id = crate::db::save_file_message( &state.pool, peerId.clone(), fileName.clone(), fileSize, - format!("fd:{}", fd), + "fd:temp".to_string(), "pending".to_string(), "pending".to_string(), - ).await; + ).await.map_err(|e| format!("创建记录失败: {}", e))?; + + // 缓存克隆的 FD(用 msg_id 作为 key) + crate::android_fd::cache_fd_for_msg( + msg_id, + cached_fd, + fileName.clone(), + fileSize as u64, + ); + + // 更新 DB 路径为 fd:{msg_id} + let fd_path = format!("fd:{}", msg_id); + if let Err(e) = crate::db::update_file_path_by_id(&state.pool, msg_id, &fd_path).await { + eprintln!("[Command] 更新 FD 文件路径失败: {}", e); + } + return Ok(serde_json::json!({ "success": true, "status": "pending", + "msg_id": msg_id, "file_name": fileName, })); }