make upload/download work sequentialy so animation stays cool

This commit is contained in:
RafayAhmad7548 2025-08-14 07:34:45 +05:00
parent 08bbeb2619
commit 1ccadcf200

View file

@ -109,23 +109,46 @@ class SftpWorker {
static void _sftpCmdHandler(SendPort sendPort, ReceivePort receivePort, SftpClient sftpClient) { static void _sftpCmdHandler(SendPort sendPort, ReceivePort receivePort, SftpClient sftpClient) {
receivePort.listen((message) async {
final (int id, SftpCommand command) = message; final StreamController<(int, DownloadFile)> downloadController = StreamController();
switch (command) { downloadController.stream.asyncMap((cmd) async {
case ListDir(:final path): final (int id, DownloadFile downloadCmd) = cmd;
try { try {
final files = await sftpClient.listdir(path); final localFile = File('${downloadCmd.downloadPath}/${downloadCmd.file.filename}');
sendPort.send((id, files)); if (await localFile.exists()) {
sendPort.send((id, RemoteError('File Already Exists', '')));
return;
}
final localFileWriter = await localFile.open(mode: FileMode.write);
final remoteFile = await sftpClient.open('${downloadCmd.path}${downloadCmd.file.filename}');
final fileSize = downloadCmd.file.attr.size!;
bool timeout = true;
await for (final bytes in remoteFile.read(
onProgress: (progress) {
if (timeout) {
timeout = false;
sendPort.send((id, progress/fileSize));
Future.delayed(Duration(seconds: 2), () => timeout = true);
}
}
)) {
await localFileWriter.writeFrom(bytes);
}
} }
on SftpStatusError catch (e) { on SftpStatusError catch (e) {
sendPort.send((id, RemoteError(e.message, ''))); sendPort.send((id, RemoteError(e.message, '')));
} }
case UploadFile(:final path, :final filePath): sendPort.send((id, 1.0));
}).listen((_) {});
final StreamController<(int, UploadFile)> uploadController = StreamController();
uploadController.stream.asyncMap((cmd) async {
final (int id, UploadFile uploadCmd) = cmd;
try { try {
final file = File(filePath); final file = File(uploadCmd.filePath);
final fileSize = await file.length(); final fileSize = await file.length();
final remoteFile = await sftpClient.open( final remoteFile = await sftpClient.open(
'$path${basename(filePath)}', '${uploadCmd.path}${basename(uploadCmd.filePath)}',
mode: SftpFileOpenMode.create | SftpFileOpenMode.write | SftpFileOpenMode.exclusive mode: SftpFileOpenMode.create | SftpFileOpenMode.write | SftpFileOpenMode.exclusive
); );
bool timeout = true; bool timeout = true;
@ -144,6 +167,21 @@ class SftpWorker {
sendPort.send((id, RemoteError(e.message, ''))); sendPort.send((id, RemoteError(e.message, '')));
} }
sendPort.send((id, 1.0)); sendPort.send((id, 1.0));
}).listen((_) {});
receivePort.listen((message) async {
final (int id, SftpCommand command) = message;
switch (command) {
case ListDir(:final path):
try {
final files = await sftpClient.listdir(path);
sendPort.send((id, files));
}
on SftpStatusError catch (e) {
sendPort.send((id, RemoteError(e.message, '')));
}
case UploadFile():
uploadController.add((id, command));
case MkDir(:final path): case MkDir(:final path):
try { try {
await sftpClient.mkdir(path); await sftpClient.mkdir(path);
@ -187,33 +225,8 @@ class SftpWorker {
on SftpStatusError catch (e) { on SftpStatusError catch (e) {
sendPort.send((id, RemoteError(e.message, ''))); sendPort.send((id, RemoteError(e.message, '')));
} }
case DownloadFile(:final file, :final path, :final downloadPath): case DownloadFile():
try { downloadController.add((id, command));
final localFile = File('$downloadPath/${file.filename}');
if (await localFile.exists()) {
sendPort.send((id, RemoteError('File Already Exists', '')));
break;
}
final localFileWriter = await localFile.open(mode: FileMode.write);
final remoteFile = await sftpClient.open('$path${file.filename}');
final fileSize = file.attr.size!;
bool timeout = true;
await for (final bytes in remoteFile.read(
onProgress: (progress) {
if (timeout) {
timeout = false;
sendPort.send((id, progress/fileSize));
Future.delayed(Duration(seconds: 2), () => timeout = true);
}
}
)) {
await localFileWriter.writeFrom(bytes);
}
}
on SftpStatusError catch (e) {
sendPort.send((id, RemoteError(e.message, '')));
}
sendPort.send((id, 1.0));
} }
}); });
} }