fix: sftp dl

This commit is contained in:
lollipopkit
2023-07-28 19:08:34 +08:00
parent 6803e9fa40
commit 6045e7e7f0
22 changed files with 137 additions and 116 deletions

View File

@@ -1,34 +1,38 @@
import 'dart:async';
import '../../../core/utils/server.dart';
import '../server/server_private_info.dart';
import 'worker.dart';
class SftpReqItem {
class SftpReq {
final ServerPrivateInfo spi;
final String remotePath;
final String localPath;
final SftpReqType type;
String? privateKey;
SftpReqItem(this.spi, this.remotePath, this.localPath);
SftpReq(
this.spi,
this.remotePath,
this.localPath,
this.type,
) {
if (spi.pubKeyId != null) {
privateKey = getPrivateKey(spi.pubKeyId!);
}
}
}
enum SftpReqType { download, upload }
class SftpReq {
final SftpReqItem item;
final String? privateKey;
final SftpReqType type;
SftpReq({required this.item, this.privateKey, required this.type});
}
class SftpReqStatus {
final int id;
final SftpReqItem item;
final SftpReq req;
final void Function() notifyListeners;
late SftpWorker worker;
final Completer? completer;
String get fileName => item.localPath.split('/').last;
String get fileName => req.localPath.split('/').last;
// status of the download
double? progress;
@@ -38,17 +42,14 @@ class SftpReqStatus {
Duration? spentTime;
SftpReqStatus({
required this.item,
required this.req,
required this.notifyListeners,
required SftpReqType type,
this.completer,
}) : id = DateTime.now().microsecondsSinceEpoch {
worker = SftpWorker(
onNotify: onNotify,
item: item,
type: type,
);
worker.init();
req: req,
)..init();
}
@override
@@ -79,6 +80,7 @@ class SftpReqStatus {
spentTime = event;
break;
default:
error = Exception('unknown event: $event');
}
notifyListeners();
}

View File

@@ -6,20 +6,19 @@ import 'dart:typed_data';
import 'package:dartssh2/dartssh2.dart';
import 'package:easy_isolate/easy_isolate.dart';
import 'package:toolbox/core/utils/misc.dart';
import 'package:toolbox/core/utils/server.dart';
import 'req.dart';
class SftpWorker {
final Function(Object event) onNotify;
final SftpReqItem item;
final SftpReqType type;
final SftpReq req;
final worker = Worker();
SftpWorker({
required this.onNotify,
required this.item,
required this.type,
required this.req,
});
void dispose() {
@@ -35,7 +34,7 @@ class SftpWorker {
isolateMessageHandler,
errorHandler: print,
);
worker.sendMessage(SftpReq(item: item, type: type));
worker.sendMessage(req);
}
/// Handle the messages coming from the isolate
@@ -69,29 +68,19 @@ Future<void> isolateMessageHandler(
}
Future<void> _download(
SftpReq data,
SftpReq req,
SendPort mainSendPort,
SendErrorFunction sendError,
) async {
try {
mainSendPort.send(SftpWorkerStatus.preparing);
final watch = Stopwatch()..start();
final item = data.item;
final spi = item.spi;
final socket = await SSHSocket.connect(spi.ip, spi.port);
SSHClient client;
if (spi.pubKeyId == null) {
client = SSHClient(socket,
username: spi.user, onPasswordRequest: () => spi.pwd);
} else {
client = SSHClient(socket,
username: spi.user, identities: SSHKeyPair.fromPem(data.privateKey!));
}
final client = await genClient(req.spi, privateKey: req.privateKey);
mainSendPort.send(SftpWorkerStatus.sshConnectted);
final remotePath = item.remotePath;
final localPath = item.localPath;
await Directory(localPath.substring(0, item.localPath.lastIndexOf('/')))
final remotePath = req.remotePath;
final localPath = req.localPath;
await Directory(localPath.substring(0, req.localPath.lastIndexOf('/')))
.create(recursive: true);
final local = File(localPath);
if (await local.exists()) {
@@ -104,7 +93,8 @@ Future<void> _download(
mainSendPort.send(Exception('can not get file size'));
return;
}
const defaultChunkSize = 1024 * 1024;
// Read 10m each time
const defaultChunkSize = 1024 * 1024 * 10;
final chunkSize = size > defaultChunkSize ? defaultChunkSize : size;
mainSendPort.send(size);
mainSendPort.send(SftpWorkerStatus.downloading);
@@ -125,29 +115,19 @@ Future<void> _download(
}
Future<void> _upload(
SftpReq data,
SftpReq req,
SendPort mainSendPort,
SendErrorFunction sendError,
) async {
try {
mainSendPort.send(SftpWorkerStatus.preparing);
final watch = Stopwatch()..start();
final item = data.item;
final spi = item.spi;
final socket = await SSHSocket.connect(spi.ip, spi.port);
SSHClient client;
if (spi.pubKeyId == null) {
client = SSHClient(socket,
username: spi.user, onPasswordRequest: () => spi.pwd);
} else {
client = SSHClient(socket,
username: spi.user, identities: SSHKeyPair.fromPem(data.privateKey!));
}
final client = await genClient(req.spi, privateKey: req.privateKey);
mainSendPort.send(SftpWorkerStatus.sshConnectted);
final localPath = item.localPath;
final localPath = req.localPath;
final remotePath =
item.remotePath + (getFileName(localPath) ?? 'srvbox_sftp_upload');
req.remotePath + (getFileName(localPath) ?? 'srvbox_sftp_upload');
final local = File(localPath);
if (!await local.exists()) {
mainSendPort.send(Exception('local file not exists'));