delete worky
This commit is contained in:
parent
78b2b79d3c
commit
045d0694e7
3 changed files with 84 additions and 40 deletions
|
@ -124,7 +124,7 @@ class _SftpExplorerState extends State<SftpExplorer> {
|
||||||
return ListTile(
|
return ListTile(
|
||||||
leading: Icon(dirEntry.attr.isDirectory ? Icons.folder : Icons.description),
|
leading: Icon(dirEntry.attr.isDirectory ? Icons.folder : Icons.description),
|
||||||
title: Text(dirEntry.filename),
|
title: Text(dirEntry.filename),
|
||||||
trailing: OperationButtons(dirEntry: dirEntry),
|
trailing: OperationButtons(sftpWorker: widget.sftpWorker, path: path, dirEntries: [dirEntry], listDir: _listDir,),
|
||||||
onTap: () {
|
onTap: () {
|
||||||
if (dirEntry.attr.isDirectory) {
|
if (dirEntry.attr.isDirectory) {
|
||||||
path = '$path${dirEntry.filename}/';
|
path = '$path${dirEntry.filename}/';
|
||||||
|
|
|
@ -28,6 +28,13 @@ class MkDir extends SftpCommand {
|
||||||
MkDir(this.path);
|
MkDir(this.path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class Remove extends SftpCommand {
|
||||||
|
final SftpName dirEntry;
|
||||||
|
final String path;
|
||||||
|
|
||||||
|
Remove(this.dirEntry, this.path);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class SftpWorker {
|
class SftpWorker {
|
||||||
|
|
||||||
|
@ -132,6 +139,33 @@ class SftpWorker {
|
||||||
on SftpStatusError catch (e) {
|
on SftpStatusError catch (e) {
|
||||||
sendPort.send((id, RemoteError(e.message, '')));
|
sendPort.send((id, RemoteError(e.message, '')));
|
||||||
}
|
}
|
||||||
|
case Remove(:final dirEntry, :final path):
|
||||||
|
try {
|
||||||
|
if (dirEntry.attr.isDirectory) {
|
||||||
|
Future<void> removeRecursively (String path) async {
|
||||||
|
final dirContents = await sftpClient.listdir(path);
|
||||||
|
for (SftpName entry in dirContents) {
|
||||||
|
final fullPath = '$path${entry.filename}';
|
||||||
|
if (entry.attr.isDirectory) {
|
||||||
|
await removeRecursively('$fullPath/');
|
||||||
|
await sftpClient.rmdir('$fullPath/');
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
await sftpClient.remove(fullPath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
await sftpClient.rmdir(path);
|
||||||
|
}
|
||||||
|
await removeRecursively('$path${dirEntry.filename}/');
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
await sftpClient.remove('$path${dirEntry.filename}');
|
||||||
|
}
|
||||||
|
sendPort.send((id, 0));
|
||||||
|
}
|
||||||
|
on SftpStatusError catch (e) {
|
||||||
|
sendPort.send((id, RemoteError(e.message, '')));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -190,5 +224,12 @@ class SftpWorker {
|
||||||
await completer.future;
|
await completer.future;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> remove(SftpName dirEntry, String path) async {
|
||||||
|
final completer = Completer.sync();
|
||||||
|
final id = _idCounter++;
|
||||||
|
_activeRequests[id] = completer;
|
||||||
|
_commands.send((id, Remove(dirEntry, path)));
|
||||||
|
await completer.future;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,17 @@
|
||||||
import 'package:dartssh2/dartssh2.dart';
|
import 'package:dartssh2/dartssh2.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:fluxcloud/sftp_worker.dart';
|
||||||
|
|
||||||
class OperationButtons extends StatelessWidget {
|
class OperationButtons extends StatelessWidget {
|
||||||
const OperationButtons({
|
const OperationButtons({
|
||||||
super.key,
|
super.key,
|
||||||
required this.dirEntry,
|
required this.sftpWorker, required this.path, required this.dirEntries, required this.listDir,
|
||||||
});
|
});
|
||||||
|
|
||||||
final SftpName dirEntry;
|
final SftpWorker sftpWorker;
|
||||||
|
final String path;
|
||||||
|
final List<SftpName> dirEntries;
|
||||||
|
final Function listDir;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
@ -26,8 +30,10 @@ class OperationButtons extends StatelessWidget {
|
||||||
},
|
},
|
||||||
icon: Icon(Icons.copy)
|
icon: Icon(Icons.copy)
|
||||||
),
|
),
|
||||||
|
if (dirEntries.length == 1)
|
||||||
IconButton(
|
IconButton(
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
|
final dirEntry = dirEntries[0];
|
||||||
final newNameController = TextEditingController(text: dirEntry.filename);
|
final newNameController = TextEditingController(text: dirEntry.filename);
|
||||||
showDialog(
|
showDialog(
|
||||||
context: context,
|
context: context,
|
||||||
|
@ -45,7 +51,7 @@ class OperationButtons extends StatelessWidget {
|
||||||
TextButton(
|
TextButton(
|
||||||
onPressed: () async {
|
onPressed: () async {
|
||||||
// try {
|
// try {
|
||||||
// await widget.sftpWorker.rename('${path}${dirEntry.filename}', '${widget.path}${newNameController.text}');
|
// await sftpWorker.rename('${path}${dirEntry.filename}', '${widget.path}${newNameController.text}');
|
||||||
// _listDir();
|
// _listDir();
|
||||||
// }
|
// }
|
||||||
// on SftpStatusError catch (e) {
|
// on SftpStatusError catch (e) {
|
||||||
|
@ -71,42 +77,39 @@ class OperationButtons extends StatelessWidget {
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
showDialog(
|
showDialog(
|
||||||
context: context,
|
context: context,
|
||||||
builder: (context) => AlertDialog(
|
builder: (context) {
|
||||||
title: Text('Delete Permanently?'),
|
String warningText = 'This action cannot be undone';
|
||||||
content: Text(dirEntry.attr.isDirectory ? 'The contents of this folder will be deleted as well\nThis action cannot be undone' : 'This action cannot be undone'),
|
if (dirEntries.length == 1 && dirEntries[0].attr.isDirectory) {
|
||||||
actions: [
|
warningText = 'The contents of this folder will be deleted as well\n$warningText';
|
||||||
TextButton(onPressed: () => Navigator.pop(context), child: Text('Cancel')),
|
}
|
||||||
TextButton(
|
else if (dirEntries.length > 1) {
|
||||||
onPressed: () async {
|
warningText = 'All selected files will be deleted\n$warningText';
|
||||||
// if (dirEntry.attr.isDirectory) {
|
}
|
||||||
// Future<void> removeRecursively (String path) async {
|
return AlertDialog(
|
||||||
// final dirContents = await widget.sftpWorker.listdir(path);
|
title: Text('Delete Permanently?'),
|
||||||
// for (SftpName entry in dirContents) {
|
content: Text(warningText),
|
||||||
// final fullPath = '$path${entry.filename}';
|
actions: [
|
||||||
// if (entry.attr.isDirectory) {
|
TextButton(onPressed: () => Navigator.pop(context), child: Text('Cancel')),
|
||||||
// await removeRecursively('$fullPath/');
|
TextButton(
|
||||||
// await widget.sftpWorker.rmdir('$fullPath/');
|
onPressed: () async {
|
||||||
// }
|
for (final dirEntry in dirEntries) {
|
||||||
// else {
|
try {
|
||||||
// await widget.sftpWorker.remove(fullPath);
|
await sftpWorker.remove(dirEntry, path);
|
||||||
// }
|
}
|
||||||
// }
|
catch (e) {
|
||||||
// await widget.sftpWorker.rmdir(path);
|
print(e.toString());
|
||||||
// }
|
}
|
||||||
// await removeRecursively('${path}${dirEntry.filename}/');
|
listDir();
|
||||||
// }
|
if (context.mounted) {
|
||||||
// else {
|
Navigator.pop(context);
|
||||||
// await widget.sftpWorker.remove('${path}${dirEntry.filename}');
|
}
|
||||||
// }
|
}
|
||||||
// _listDir();
|
},
|
||||||
// if (context.mounted) {
|
child: Text('Yes')
|
||||||
// Navigator.pop(context);
|
),
|
||||||
// }
|
],
|
||||||
},
|
);
|
||||||
child: Text('Yes')
|
}
|
||||||
),
|
|
||||||
],
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
icon: Icon(Icons.delete)
|
icon: Icon(Icons.delete)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue