fix: use fastGet/fastPut for more reliable SFTP transfers

This commit is contained in:
Ethanfly 2025-12-29 15:12:34 +08:00
parent a4f4078551
commit 92147b39ec

View File

@ -119,59 +119,39 @@ class SFTPService {
try { try {
({ conn, sftp } = await this.createConnection(hostConfig)); ({ conn, sftp } = await this.createConnection(hostConfig));
const filename = path.basename(remotePath);
const self = this;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// 获取文件大小 // 使用 fastGet 方法进行下载,更加可靠
sftp.stat(remotePath, (err, stats) => { sftp.fastGet(remotePath, localPath, {
if (err) { concurrency: 1, // 单线程下载,更稳定
conn.end(); chunkSize: 32768, // 32KB 块大小
reject(err); step: function(transferred, chunk, total) {
return; const percent = Math.round((transferred / total) * 100);
} if (self.progressCallback) {
self.progressCallback({
const totalSize = stats.size;
let downloadedSize = 0;
const filename = path.basename(remotePath);
// 创建读写流
const readStream = sftp.createReadStream(remotePath);
const writeStream = fs.createWriteStream(localPath);
readStream.on('data', (chunk) => {
downloadedSize += chunk.length;
const percent = Math.round((downloadedSize / totalSize) * 100);
if (this.progressCallback) {
this.progressCallback({
type: 'download', type: 'download',
filename, filename,
percent, percent,
transferred: downloadedSize, transferred,
total: totalSize, total,
}); });
} }
}); }
}, (err) => {
readStream.on('error', (err) => { conn.end();
writeStream.destroy(); if (err) {
conn.end(); // 删除可能不完整的文件
fs.unlink(localPath, () => { }); try {
fs.unlinkSync(localPath);
} catch (e) {
// 忽略删除失败
}
reject(err); reject(err);
}); return;
}
writeStream.on('error', (err) => { resolve({ success: true, localPath });
readStream.destroy();
conn.end();
fs.unlink(localPath, () => { });
reject(err);
});
// 使用 'finish' 事件确保数据完全写入磁盘
writeStream.on('finish', () => {
conn.end();
resolve({ success: true, localPath });
});
readStream.pipe(writeStream);
}); });
}); });
} catch (err) { } catch (err) {
@ -187,50 +167,34 @@ class SFTPService {
try { try {
({ conn, sftp } = await this.createConnection(hostConfig)); ({ conn, sftp } = await this.createConnection(hostConfig));
const filename = path.basename(localPath);
const self = this;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const stats = fs.statSync(localPath); // 使用 fastPut 方法进行上传,更加可靠
const totalSize = stats.size; sftp.fastPut(localPath, remotePath, {
let uploadedSize = 0; concurrency: 1, // 单线程上传,更稳定
const filename = path.basename(localPath); chunkSize: 32768, // 32KB 块大小
step: function(transferred, chunk, total) {
// 创建读写流 const percent = Math.round((transferred / total) * 100);
const readStream = fs.createReadStream(localPath); if (self.progressCallback) {
const writeStream = sftp.createWriteStream(remotePath); self.progressCallback({
type: 'upload',
readStream.on('data', (chunk) => { filename,
uploadedSize += chunk.length; percent,
const percent = Math.round((uploadedSize / totalSize) * 100); transferred,
if (this.progressCallback) { total,
this.progressCallback({ });
type: 'upload', }
filename,
percent,
transferred: uploadedSize,
total: totalSize,
});
} }
}); }, (err) => {
readStream.on('error', (err) => {
writeStream.destroy();
conn.end();
reject(err);
});
writeStream.on('error', (err) => {
readStream.destroy();
conn.end();
reject(err);
});
// 使用 'finish' 事件确保数据完全写入
writeStream.on('finish', () => {
conn.end(); conn.end();
if (err) {
reject(err);
return;
}
resolve({ success: true, remotePath }); resolve({ success: true, remotePath });
}); });
readStream.pipe(writeStream);
}); });
} catch (err) { } catch (err) {
return { success: false, error: err.message }; return { success: false, error: err.message };