Compare commits
2 commits
ce6374d302
...
23cd60701c
Author | SHA1 | Date | |
---|---|---|---|
![]() |
23cd60701c | ||
![]() |
ae32866d46 |
2 changed files with 195 additions and 145 deletions
|
@ -7,10 +7,9 @@ import 'package:flutter/material.dart';
|
||||||
import 'package:fluxcloud/sftp_worker.dart';
|
import 'package:fluxcloud/sftp_worker.dart';
|
||||||
|
|
||||||
class SftpExplorer extends StatefulWidget {
|
class SftpExplorer extends StatefulWidget {
|
||||||
const SftpExplorer({super.key, required this.sftpWorker, this.path = '/'});
|
const SftpExplorer({super.key, required this.sftpWorker});
|
||||||
|
|
||||||
final SftpWorker sftpWorker;
|
final SftpWorker sftpWorker;
|
||||||
final String path;
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<SftpExplorer> createState() => _SftpExplorerState();
|
State<SftpExplorer> createState() => _SftpExplorerState();
|
||||||
|
@ -18,6 +17,8 @@ class SftpExplorer extends StatefulWidget {
|
||||||
|
|
||||||
class _SftpExplorerState extends State<SftpExplorer> {
|
class _SftpExplorerState extends State<SftpExplorer> {
|
||||||
|
|
||||||
|
String path = '/';
|
||||||
|
|
||||||
bool _isLoading = true;
|
bool _isLoading = true;
|
||||||
late List<SftpName> _dirContents;
|
late List<SftpName> _dirContents;
|
||||||
|
|
||||||
|
@ -32,7 +33,7 @@ class _SftpExplorerState extends State<SftpExplorer> {
|
||||||
Future<void> _listDir() async {
|
Future<void> _listDir() async {
|
||||||
setState(() => _isLoading = true);
|
setState(() => _isLoading = true);
|
||||||
try {
|
try {
|
||||||
_dirContents = await widget.sftpWorker.listdir(widget.path);
|
_dirContents = await widget.sftpWorker.listdir(path);
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
if (mounted) {
|
if (mounted) {
|
||||||
|
@ -51,6 +52,20 @@ class _SftpExplorerState extends State<SftpExplorer> {
|
||||||
title: Text('Explorer'),
|
title: Text('Explorer'),
|
||||||
elevation: 2,
|
elevation: 2,
|
||||||
actionsPadding: EdgeInsets.only(right: 20),
|
actionsPadding: EdgeInsets.only(right: 20),
|
||||||
|
leading: IconButton(
|
||||||
|
onPressed: () {
|
||||||
|
if (path == '/') {
|
||||||
|
// TODO: figure this out
|
||||||
|
// Navigator.pop(context);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
path = path.substring(0, path.length - 1);
|
||||||
|
path = path.substring(0, path.lastIndexOf('/')+1);
|
||||||
|
_listDir();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
icon: Icon(Icons.arrow_back)
|
||||||
|
),
|
||||||
actions: [
|
actions: [
|
||||||
if (_progress != null)
|
if (_progress != null)
|
||||||
Stack(
|
Stack(
|
||||||
|
@ -63,7 +78,7 @@ class _SftpExplorerState extends State<SftpExplorer> {
|
||||||
),
|
),
|
||||||
IconButton(
|
IconButton(
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
|
|
||||||
},
|
},
|
||||||
icon: Icon(Icons.upload)
|
icon: Icon(Icons.upload)
|
||||||
),
|
),
|
||||||
|
@ -72,127 +87,143 @@ class _SftpExplorerState extends State<SftpExplorer> {
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
floatingActionButton: _buildFABs(context),
|
floatingActionButton: _buildFABs(context),
|
||||||
body: _isLoading ? Center(child: CircularProgressIndicator()) : ListView.builder(
|
body: AnimatedSwitcher(
|
||||||
itemCount: _dirContents.length,
|
duration: Duration(milliseconds: 300),
|
||||||
itemBuilder: (context, index) {
|
transitionBuilder: (child, animation) {
|
||||||
final dirEntry = _dirContents[index];
|
final curved = CurvedAnimation(
|
||||||
return ListTile(
|
parent: animation,
|
||||||
leading: Icon(dirEntry.attr.isDirectory ? Icons.folder : Icons.description),
|
curve: Curves.fastOutSlowIn
|
||||||
title: Text(dirEntry.filename),
|
|
||||||
trailing: Row(
|
|
||||||
mainAxisSize: MainAxisSize.min,
|
|
||||||
children: [
|
|
||||||
IconButton(
|
|
||||||
onPressed: () {
|
|
||||||
|
|
||||||
},
|
|
||||||
icon: Icon(Icons.drive_file_move)
|
|
||||||
),
|
|
||||||
IconButton(
|
|
||||||
onPressed: () {
|
|
||||||
|
|
||||||
},
|
|
||||||
icon: Icon(Icons.copy)
|
|
||||||
),
|
|
||||||
IconButton(
|
|
||||||
onPressed: () {
|
|
||||||
final newNameController = TextEditingController(text: dirEntry.filename);
|
|
||||||
showDialog(
|
|
||||||
context: context,
|
|
||||||
builder: (context) => AlertDialog(
|
|
||||||
title: Text('Rename'),
|
|
||||||
content: TextField(
|
|
||||||
controller: newNameController,
|
|
||||||
autofocus: true,
|
|
||||||
decoration: InputDecoration(
|
|
||||||
labelText: 'Enter new name'
|
|
||||||
),
|
|
||||||
),
|
|
||||||
actions: [
|
|
||||||
TextButton(onPressed: () => Navigator.pop(context), child: Text('Cancel')),
|
|
||||||
TextButton(
|
|
||||||
onPressed: () async {
|
|
||||||
// try {
|
|
||||||
// await widget.sftpWorker.rename('${widget.path}${dirEntry.filename}', '${widget.path}${newNameController.text}');
|
|
||||||
// _listDir();
|
|
||||||
// }
|
|
||||||
// on SftpStatusError catch (e) {
|
|
||||||
// if (context.mounted) {
|
|
||||||
// ScaffoldMessenger.of(context).showSnackBar(_buildErrorSnackBar(context, e.message));
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// if (context.mounted) {
|
|
||||||
// Navigator.pop(context);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
},
|
|
||||||
child: Text('Rename')
|
|
||||||
),
|
|
||||||
|
|
||||||
],
|
|
||||||
)
|
|
||||||
);
|
|
||||||
},
|
|
||||||
icon: Icon(Icons.drive_file_rename_outline)
|
|
||||||
),
|
|
||||||
IconButton(
|
|
||||||
onPressed: () {
|
|
||||||
showDialog(
|
|
||||||
context: context,
|
|
||||||
builder: (context) => AlertDialog(
|
|
||||||
title: Text('Delete Permanently?'),
|
|
||||||
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'),
|
|
||||||
actions: [
|
|
||||||
TextButton(onPressed: () => Navigator.pop(context), child: Text('Cancel')),
|
|
||||||
TextButton(
|
|
||||||
onPressed: () async {
|
|
||||||
// if (dirEntry.attr.isDirectory) {
|
|
||||||
// Future<void> removeRecursively (String path) async {
|
|
||||||
// final dirContents = await widget.sftpWorker.listdir(path);
|
|
||||||
// for (SftpName entry in dirContents) {
|
|
||||||
// final fullPath = '$path${entry.filename}';
|
|
||||||
// if (entry.attr.isDirectory) {
|
|
||||||
// await removeRecursively('$fullPath/');
|
|
||||||
// await widget.sftpWorker.rmdir('$fullPath/');
|
|
||||||
// }
|
|
||||||
// else {
|
|
||||||
// await widget.sftpWorker.remove(fullPath);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// await widget.sftpWorker.rmdir(path);
|
|
||||||
// }
|
|
||||||
// await removeRecursively('${widget.path}${dirEntry.filename}/');
|
|
||||||
// }
|
|
||||||
// else {
|
|
||||||
// await widget.sftpWorker.remove('${widget.path}${dirEntry.filename}');
|
|
||||||
// }
|
|
||||||
// _listDir();
|
|
||||||
// if (context.mounted) {
|
|
||||||
// Navigator.pop(context);
|
|
||||||
// }
|
|
||||||
},
|
|
||||||
child: Text('Yes')
|
|
||||||
),
|
|
||||||
],
|
|
||||||
)
|
|
||||||
);
|
|
||||||
},
|
|
||||||
icon: Icon(Icons.delete)
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
onTap: () {
|
|
||||||
if (dirEntry.attr.isDirectory) {
|
|
||||||
Navigator.push(context, MaterialPageRoute(
|
|
||||||
builder: (context) => SftpExplorer(
|
|
||||||
sftpWorker: widget.sftpWorker,
|
|
||||||
path: '${widget.path}${dirEntry.filename}/',
|
|
||||||
)
|
|
||||||
));
|
|
||||||
}
|
|
||||||
},
|
|
||||||
);
|
);
|
||||||
},
|
return FadeTransition(
|
||||||
|
opacity: curved,
|
||||||
|
child: ScaleTransition(
|
||||||
|
scale: Tween<double>(
|
||||||
|
begin: 0.92,
|
||||||
|
end: 1
|
||||||
|
).animate(curved),
|
||||||
|
child: child,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
child: _isLoading ? Center(child: CircularProgressIndicator()) : ListView.builder(
|
||||||
|
key: ValueKey(path),
|
||||||
|
itemCount: _dirContents.length,
|
||||||
|
itemBuilder: (context, index) {
|
||||||
|
final dirEntry = _dirContents[index];
|
||||||
|
return ListTile(
|
||||||
|
leading: Icon(dirEntry.attr.isDirectory ? Icons.folder : Icons.description),
|
||||||
|
title: Text(dirEntry.filename),
|
||||||
|
trailing: Row(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
children: [
|
||||||
|
IconButton(
|
||||||
|
onPressed: () {
|
||||||
|
|
||||||
|
},
|
||||||
|
icon: Icon(Icons.drive_file_move)
|
||||||
|
),
|
||||||
|
IconButton(
|
||||||
|
onPressed: () {
|
||||||
|
|
||||||
|
},
|
||||||
|
icon: Icon(Icons.copy)
|
||||||
|
),
|
||||||
|
IconButton(
|
||||||
|
onPressed: () {
|
||||||
|
final newNameController = TextEditingController(text: dirEntry.filename);
|
||||||
|
showDialog(
|
||||||
|
context: context,
|
||||||
|
builder: (context) => AlertDialog(
|
||||||
|
title: Text('Rename'),
|
||||||
|
content: TextField(
|
||||||
|
controller: newNameController,
|
||||||
|
autofocus: true,
|
||||||
|
decoration: InputDecoration(
|
||||||
|
labelText: 'Enter new name'
|
||||||
|
),
|
||||||
|
),
|
||||||
|
actions: [
|
||||||
|
TextButton(onPressed: () => Navigator.pop(context), child: Text('Cancel')),
|
||||||
|
TextButton(
|
||||||
|
onPressed: () async {
|
||||||
|
// try {
|
||||||
|
// await widget.sftpWorker.rename('${path}${dirEntry.filename}', '${widget.path}${newNameController.text}');
|
||||||
|
// _listDir();
|
||||||
|
// }
|
||||||
|
// on SftpStatusError catch (e) {
|
||||||
|
// if (context.mounted) {
|
||||||
|
// ScaffoldMessenger.of(context).showSnackBar(_buildErrorSnackBar(context, e.message));
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// if (context.mounted) {
|
||||||
|
// Navigator.pop(context);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
},
|
||||||
|
child: Text('Rename')
|
||||||
|
),
|
||||||
|
|
||||||
|
],
|
||||||
|
)
|
||||||
|
);
|
||||||
|
},
|
||||||
|
icon: Icon(Icons.drive_file_rename_outline)
|
||||||
|
),
|
||||||
|
IconButton(
|
||||||
|
onPressed: () {
|
||||||
|
showDialog(
|
||||||
|
context: context,
|
||||||
|
builder: (context) => AlertDialog(
|
||||||
|
title: Text('Delete Permanently?'),
|
||||||
|
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'),
|
||||||
|
actions: [
|
||||||
|
TextButton(onPressed: () => Navigator.pop(context), child: Text('Cancel')),
|
||||||
|
TextButton(
|
||||||
|
onPressed: () async {
|
||||||
|
// if (dirEntry.attr.isDirectory) {
|
||||||
|
// Future<void> removeRecursively (String path) async {
|
||||||
|
// final dirContents = await widget.sftpWorker.listdir(path);
|
||||||
|
// for (SftpName entry in dirContents) {
|
||||||
|
// final fullPath = '$path${entry.filename}';
|
||||||
|
// if (entry.attr.isDirectory) {
|
||||||
|
// await removeRecursively('$fullPath/');
|
||||||
|
// await widget.sftpWorker.rmdir('$fullPath/');
|
||||||
|
// }
|
||||||
|
// else {
|
||||||
|
// await widget.sftpWorker.remove(fullPath);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// await widget.sftpWorker.rmdir(path);
|
||||||
|
// }
|
||||||
|
// await removeRecursively('${path}${dirEntry.filename}/');
|
||||||
|
// }
|
||||||
|
// else {
|
||||||
|
// await widget.sftpWorker.remove('${path}${dirEntry.filename}');
|
||||||
|
// }
|
||||||
|
// _listDir();
|
||||||
|
// if (context.mounted) {
|
||||||
|
// Navigator.pop(context);
|
||||||
|
// }
|
||||||
|
},
|
||||||
|
child: Text('Yes')
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
);
|
||||||
|
},
|
||||||
|
icon: Icon(Icons.delete)
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
onTap: () {
|
||||||
|
if (dirEntry.attr.isDirectory) {
|
||||||
|
path = '$path${dirEntry.filename}/';
|
||||||
|
_listDir();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
},
|
||||||
|
)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -220,23 +251,18 @@ class _SftpExplorerState extends State<SftpExplorer> {
|
||||||
TextButton(onPressed: () => Navigator.pop(context), child: Text('Cancel')),
|
TextButton(onPressed: () => Navigator.pop(context), child: Text('Cancel')),
|
||||||
TextButton(
|
TextButton(
|
||||||
onPressed: () async {
|
onPressed: () async {
|
||||||
// try {
|
try {
|
||||||
// await widget.sftpWorker.mkdir('${widget.path}${nameController.text}');
|
await widget.sftpWorker.mkdir('${path}${nameController.text}');
|
||||||
// _listDir();
|
_listDir();
|
||||||
// }
|
}
|
||||||
// on SftpStatusError catch (e) {
|
catch (e) {
|
||||||
// if (context.mounted) {
|
if (context.mounted) {
|
||||||
// if (e.code == 4) {
|
ScaffoldMessenger.of(context).showSnackBar(_buildErrorSnackBar(context, e.toString()));
|
||||||
// ScaffoldMessenger.of(context).showSnackBar(_buildErrorSnackBar(context, 'Folder Already Exists'));
|
}
|
||||||
// }
|
}
|
||||||
// else {
|
if (context.mounted) {
|
||||||
// ScaffoldMessenger.of(context).showSnackBar(_buildErrorSnackBar(context, 'Error: ${e.message}'));
|
Navigator.pop(context);
|
||||||
// }
|
}
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// if (context.mounted) {
|
|
||||||
// Navigator.pop(context);
|
|
||||||
// }
|
|
||||||
},
|
},
|
||||||
child: Text('Ok')
|
child: Text('Ok')
|
||||||
),
|
),
|
||||||
|
@ -259,8 +285,7 @@ class _SftpExplorerState extends State<SftpExplorer> {
|
||||||
filePaths = files.map((file) => file.path).toList();
|
filePaths = files.map((file) => file.path).toList();
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
await for (final progress in widget.sftpWorker.uploadFiles(widget.path, filePaths)) {
|
await for (final progress in widget.sftpWorker.uploadFiles(path, filePaths)) {
|
||||||
print(progress);
|
|
||||||
setState(() => _progress = progress);
|
setState(() => _progress = progress);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,11 +8,13 @@ import 'package:path/path.dart';
|
||||||
import 'connection.dart';
|
import 'connection.dart';
|
||||||
|
|
||||||
sealed class SftpCommand {}
|
sealed class SftpCommand {}
|
||||||
|
|
||||||
class ListDir extends SftpCommand {
|
class ListDir extends SftpCommand {
|
||||||
final String path;
|
final String path;
|
||||||
|
|
||||||
ListDir(this.path);
|
ListDir(this.path);
|
||||||
}
|
}
|
||||||
|
|
||||||
class UploadFiles extends SftpCommand {
|
class UploadFiles extends SftpCommand {
|
||||||
final String path;
|
final String path;
|
||||||
final List<String> fileNames;
|
final List<String> fileNames;
|
||||||
|
@ -20,6 +22,13 @@ class UploadFiles extends SftpCommand {
|
||||||
UploadFiles(this.path, this.fileNames);
|
UploadFiles(this.path, this.fileNames);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class MkDir extends SftpCommand {
|
||||||
|
final String path;
|
||||||
|
|
||||||
|
MkDir(this.path);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class SftpWorker {
|
class SftpWorker {
|
||||||
|
|
||||||
final ReceivePort _responses;
|
final ReceivePort _responses;
|
||||||
|
@ -79,7 +88,7 @@ class SftpWorker {
|
||||||
|
|
||||||
static void _sftpCmdHandler(SendPort sendPort, ReceivePort receivePort, SftpClient sftpClient) {
|
static void _sftpCmdHandler(SendPort sendPort, ReceivePort receivePort, SftpClient sftpClient) {
|
||||||
receivePort.listen((message) async {
|
receivePort.listen((message) async {
|
||||||
final (int id, dynamic command) = message;
|
final (int id, SftpCommand command) = message;
|
||||||
switch (command) {
|
switch (command) {
|
||||||
case ListDir(:final path):
|
case ListDir(:final path):
|
||||||
try {
|
try {
|
||||||
|
@ -115,6 +124,14 @@ class SftpWorker {
|
||||||
}
|
}
|
||||||
sendPort.send((id, 1.0));
|
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 {
|
Future<List<SftpName>> listdir(String path) async {
|
||||||
final completer = Completer<Object>.sync();
|
final completer = Completer.sync();
|
||||||
final id = _idCounter++;
|
final id = _idCounter++;
|
||||||
_activeRequests[id] = completer;
|
_activeRequests[id] = completer;
|
||||||
_commands.send((id, ListDir(path)));
|
_commands.send((id, ListDir(path)));
|
||||||
|
@ -165,5 +182,13 @@ class SftpWorker {
|
||||||
return controller.stream;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue