new & fix

- new: switch of sftp `rm -rf` dir
- fix: sftp upload
- fix: sftp uploading progress
- opt.: editor will save content if is editing file
This commit is contained in:
lollipopkit
2023-08-27 00:10:01 +08:00
parent 65de4a8ca5
commit 7abadfb5a6
18 changed files with 129 additions and 74 deletions

View File

@@ -5,9 +5,8 @@ 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 '../../../core/utils/server.dart';
import 'req.dart';
class SftpWorker {
@@ -78,16 +77,14 @@ Future<void> _download(
final client = await genClient(req.spi, privateKey: req.privateKey);
mainSendPort.send(SftpWorkerStatus.sshConnectted);
final remotePath = req.remotePath;
final localPath = req.localPath;
await Directory(localPath.substring(0, req.localPath.lastIndexOf('/')))
await Directory(req.localPath.substring(0, req.localPath.lastIndexOf('/')))
.create(recursive: true);
final local = File(localPath);
final local = File(req.localPath);
if (await local.exists()) {
await local.delete();
}
final localFile = local.openWrite(mode: FileMode.append);
final file = await (await client.sftp()).open(remotePath);
final file = await (await client.sftp()).open(req.remotePath);
final size = (await file.stat()).size;
if (size == null) {
mainSendPort.send(Exception('can not get file size'));
@@ -97,7 +94,7 @@ Future<void> _download(
const defaultChunkSize = 1024 * 1024;
final chunkSize = size > defaultChunkSize ? defaultChunkSize : size;
mainSendPort.send(size);
mainSendPort.send(SftpWorkerStatus.downloading);
mainSendPort.send(SftpWorkerStatus.loading);
for (var i = 0; i < size; i += chunkSize) {
final fileData = file.read(length: chunkSize);
await for (var form in fileData) {
@@ -125,19 +122,23 @@ Future<void> _upload(
final client = await genClient(req.spi, privateKey: req.privateKey);
mainSendPort.send(SftpWorkerStatus.sshConnectted);
final localPath = req.localPath;
final fileName = getFileName(localPath) ?? 'srvbox_sftp_upload';
final remotePath = '${req.remotePath}/$fileName';
final local = File(localPath);
final local = File(req.localPath);
if (!await local.exists()) {
mainSendPort.send(Exception('local file not exists'));
return;
}
final localLen = await local.length();
mainSendPort.send(localLen);
mainSendPort.send(SftpWorkerStatus.loading);
final localFile = local.openRead().cast<Uint8List>();
final sftp = await client.sftp();
final file = await sftp.open(remotePath,
mode: SftpFileOpenMode.write | SftpFileOpenMode.create);
final writer = file.write(localFile);
final file = await sftp.open(
req.remotePath,
mode: SftpFileOpenMode.write | SftpFileOpenMode.create,
);
final writer = file.write(localFile, onProgress: (total) {
mainSendPort.send(total / localLen * 100);
},);
await writer.done;
await file.close();
mainSendPort.send(watch.elapsed);