mirror of
https://github.com/lollipopkit/flutter_server_box.git
synced 2025-12-17 07:14:28 +01:00
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:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user