From 2915b2c6896e42a7ac063860d3f5090f51427a19 Mon Sep 17 00:00:00 2001 From: Ethanfly Date: Mon, 29 Dec 2025 15:06:18 +0800 Subject: [PATCH] fix: SFTP download/upload use finish event to ensure data written --- src/services/sftp.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/services/sftp.js b/src/services/sftp.js index 1662121..062ef86 100644 --- a/src/services/sftp.js +++ b/src/services/sftp.js @@ -152,17 +152,21 @@ class SFTPService { }); readStream.on('error', (err) => { + writeStream.destroy(); conn.end(); fs.unlink(localPath, () => {}); reject(err); }); writeStream.on('error', (err) => { + readStream.destroy(); conn.end(); + fs.unlink(localPath, () => {}); reject(err); }); - writeStream.on('close', () => { + // 使用 'finish' 事件确保数据完全写入磁盘 + writeStream.on('finish', () => { conn.end(); resolve({ success: true, localPath }); }); @@ -209,16 +213,19 @@ class SFTPService { }); readStream.on('error', (err) => { + writeStream.destroy(); conn.end(); reject(err); }); writeStream.on('error', (err) => { + readStream.destroy(); conn.end(); reject(err); }); - writeStream.on('close', () => { + // 使用 'finish' 事件确保数据完全写入 + writeStream.on('finish', () => { conn.end(); resolve({ success: true, remotePath }); });