mkdir worky

This commit is contained in:
RafayAhmad7548 2025-08-02 08:59:05 +05:00
parent ce6374d302
commit ae32866d46
2 changed files with 40 additions and 20 deletions

View file

@ -46,6 +46,7 @@ class _SftpExplorerState extends State<SftpExplorer> {
@override
Widget build(BuildContext context) {
return Scaffold(
// TODO: make appbar same for all directories
appBar: AppBar(
toolbarHeight: 75,
title: Text('Explorer'),
@ -220,23 +221,18 @@ class _SftpExplorerState extends State<SftpExplorer> {
TextButton(onPressed: () => Navigator.pop(context), child: Text('Cancel')),
TextButton(
onPressed: () async {
// try {
// await widget.sftpWorker.mkdir('${widget.path}${nameController.text}');
// _listDir();
// }
// on SftpStatusError catch (e) {
// if (context.mounted) {
// if (e.code == 4) {
// ScaffoldMessenger.of(context).showSnackBar(_buildErrorSnackBar(context, 'Folder Already Exists'));
// }
// else {
// ScaffoldMessenger.of(context).showSnackBar(_buildErrorSnackBar(context, 'Error: ${e.message}'));
// }
// }
// }
// if (context.mounted) {
// Navigator.pop(context);
// }
try {
await widget.sftpWorker.mkdir('${widget.path}${nameController.text}');
_listDir();
}
catch (e) {
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(_buildErrorSnackBar(context, e.toString()));
}
}
if (context.mounted) {
Navigator.pop(context);
}
},
child: Text('Ok')
),
@ -260,7 +256,6 @@ class _SftpExplorerState extends State<SftpExplorer> {
}
try {
await for (final progress in widget.sftpWorker.uploadFiles(widget.path, filePaths)) {
print(progress);
setState(() => _progress = progress);
}
}

View file

@ -8,11 +8,13 @@ import 'package:path/path.dart';
import 'connection.dart';
sealed class SftpCommand {}
class ListDir extends SftpCommand {
final String path;
ListDir(this.path);
}
class UploadFiles extends SftpCommand {
final String path;
final List<String> fileNames;
@ -20,6 +22,13 @@ class UploadFiles extends SftpCommand {
UploadFiles(this.path, this.fileNames);
}
class MkDir extends SftpCommand {
final String path;
MkDir(this.path);
}
class SftpWorker {
final ReceivePort _responses;
@ -79,7 +88,7 @@ class SftpWorker {
static void _sftpCmdHandler(SendPort sendPort, ReceivePort receivePort, SftpClient sftpClient) {
receivePort.listen((message) async {
final (int id, dynamic command) = message;
final (int id, SftpCommand command) = message;
switch (command) {
case ListDir(:final path):
try {
@ -115,6 +124,14 @@ class SftpWorker {
}
sendPort.send((id, 1.0));
}
case MkDir(:final path):
try {
await sftpClient.mkdir(path);
sendPort.send((id, 0));
}
on SftpStatusError catch (e) {
sendPort.send((id, RemoteError(e.message, '')));
}
}
});
}
@ -149,7 +166,7 @@ class SftpWorker {
Future<List<SftpName>> listdir(String path) async {
final completer = Completer<Object>.sync();
final completer = Completer.sync();
final id = _idCounter++;
_activeRequests[id] = completer;
_commands.send((id, ListDir(path)));
@ -165,5 +182,13 @@ class SftpWorker {
return controller.stream;
}
Future<void> mkdir(String path) async {
final completer = Completer.sync();
final id = _idCounter++;
_activeRequests[id] = completer;
_commands.send((id, MkDir(path)));
await completer.future;
}
}