add upload/download function
This commit is contained in:
@@ -236,6 +236,82 @@ function startServer() {
|
||||
ws.send(JSON.stringify({ type: 'error', message: `삭제 실패: ${err.message}` }));
|
||||
}
|
||||
break;
|
||||
|
||||
case 'DOWNLOAD':
|
||||
if (client.closed) {
|
||||
ws.send(JSON.stringify({ type: 'error', message: 'FTP 연결이 끊어져 있습니다.' }));
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const { remotePath, localPath, transferId } = data;
|
||||
|
||||
// Progress Handler
|
||||
client.trackProgress(info => {
|
||||
ws.send(JSON.stringify({
|
||||
type: 'transfer_progress',
|
||||
id: transferId,
|
||||
bytes: info.bytes,
|
||||
bytesOverall: info.bytesOverall,
|
||||
name: info.name
|
||||
}));
|
||||
});
|
||||
|
||||
await client.downloadTo(localPath, remotePath);
|
||||
|
||||
client.trackProgress(); // Stop tracking
|
||||
ws.send(JSON.stringify({
|
||||
type: 'transfer_success',
|
||||
id: transferId,
|
||||
message: '다운로드 완료',
|
||||
path: localPath
|
||||
}));
|
||||
} catch (err) {
|
||||
client.trackProgress(); // Stop tracking
|
||||
ws.send(JSON.stringify({
|
||||
type: 'transfer_error',
|
||||
id: data.transferId,
|
||||
message: `다운로드 실패: ${err.message}`
|
||||
}));
|
||||
}
|
||||
break;
|
||||
|
||||
case 'UPLOAD':
|
||||
if (client.closed) {
|
||||
ws.send(JSON.stringify({ type: 'error', message: 'FTP 연결이 끊어져 있습니다.' }));
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const { remotePath, localPath, transferId } = data;
|
||||
|
||||
// Progress Handler
|
||||
client.trackProgress(info => {
|
||||
ws.send(JSON.stringify({
|
||||
type: 'transfer_progress',
|
||||
id: transferId,
|
||||
bytes: info.bytes,
|
||||
bytesOverall: info.bytesOverall,
|
||||
name: info.name
|
||||
}));
|
||||
});
|
||||
|
||||
await client.uploadFrom(localPath, remotePath);
|
||||
|
||||
client.trackProgress();
|
||||
ws.send(JSON.stringify({
|
||||
type: 'transfer_success',
|
||||
id: transferId,
|
||||
message: '업로드 완료',
|
||||
path: remotePath
|
||||
}));
|
||||
} catch (err) {
|
||||
client.trackProgress();
|
||||
ws.send(JSON.stringify({
|
||||
type: 'transfer_error',
|
||||
id: data.transferId,
|
||||
message: `업로드 실패: ${err.message}`
|
||||
}));
|
||||
}
|
||||
break;
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("오류 발생:", err);
|
||||
|
||||
Reference in New Issue
Block a user