Remove SFTP download

This commit is contained in:
Junyuan Feng
2022-04-06 13:23:54 +08:00
parent f8201f9542
commit 00cfd0f88d
8 changed files with 144 additions and 136 deletions

View File

@@ -0,0 +1,15 @@
extension NumX on num {
String get convertBytes {
const suffix = ['B', 'KB', 'MB', 'GB', 'TB'];
double value = toDouble();
int squareTimes = 0;
for (; value / 1024 > 1 && squareTimes < suffix.length - 1; squareTimes++) {
value /= 1024;
}
var finalValue = value.toStringAsFixed(1);
if (finalValue.endsWith('.0')) {
finalValue = finalValue.replaceFirst('.0', '');
}
return '$finalValue ${suffix[squareTimes]}';
}
}

12
lib/core/path.dart Normal file
View File

@@ -0,0 +1,12 @@
import 'dart:io';
import 'package:path_provider/path_provider.dart';
Future<Directory> get sftpDownloadDir async {
final docDir = await getApplicationDocumentsDirectory();
final dir = Directory('${docDir.path}/sftp');
if (!dir.existsSync()) {
dir.createSync();
}
return dir;
}

View File

@@ -1,4 +1,4 @@
import 'dart:math';
import 'package:toolbox/core/extension/numx.dart';
class NetSpeedPart {
String device;
@@ -27,45 +27,31 @@ class NetSpeed {
String speedIn({String? device}) {
if (old[0].device == '' || now[0].device == '') return '0kb/s';
int idx = 0;
if (device != null) {
for (var item in now) {
if (item.device == device) {
idx = now.indexOf(item);
break;
}
}
}
final idx = deviceIdx(device);
final speedInBytesPerSecond =
(now[idx].bytesIn - old[idx].bytesIn) / timeDiff;
int squareTimes = 0;
for (; speedInBytesPerSecond / pow(1024, squareTimes) > 1024;) {
if (squareTimes >= suffixs.length - 1) break;
squareTimes++;
}
return '${(speedInBytesPerSecond / pow(1024, squareTimes)).toStringAsFixed(1)} ${suffixs[squareTimes]}';
return buildStandardOutput(speedInBytesPerSecond);
}
String speedOut({String? device}) {
if (old[0].device == '' || now[0].device == '') return '0kb/s';
int idx = 0;
final idx = deviceIdx(device);
final speedInBytesPerSecond =
(now[idx].bytesOut - old[idx].bytesOut) / timeDiff;
return buildStandardOutput(speedInBytesPerSecond);
}
int deviceIdx(String? device) {
if (device != null) {
for (var item in now) {
if (item.device == device) {
idx = now.indexOf(item);
break;
return now.indexOf(item);
}
}
}
final speedInBytesPerSecond =
(now[idx].bytesOut - old[idx].bytesOut) / timeDiff;
int squareTimes = 0;
for (; speedInBytesPerSecond / pow(1024, squareTimes) > 1024;) {
if (squareTimes >= suffixs.length - 1) break;
squareTimes++;
}
return '${(speedInBytesPerSecond / pow(1024, squareTimes)).toStringAsFixed(1)} ${suffixs[squareTimes]}';
return 0;
}
}
const suffixs = ['b/s', 'kb/s', 'mb/s', 'gb/s'];
String buildStandardOutput(double speed) =>
'${speed.convertBytes.toLowerCase()}/s';
}

View File

@@ -2,9 +2,9 @@
class BuildData {
static const String name = "ServerBox";
static const int build = 108;
static const int build = 109;
static const String engine =
"Flutter 2.10.3 • channel stable • https://github.com/flutter/flutter.git\nFramework • revision 7e9793dee1 (8 days ago) • 2022-03-02 11:23:12 -0600\nEngine • revision bd539267b4\nTools • Dart 2.16.1 • DevTools 2.9.2\n";
static const String buildAt = "2022-03-10 15:25:32.032568";
static const int modifications = 0;
"Flutter 2.10.4 • channel stable • https://github.com/flutter/flutter.git\nFramework • revision c860cba910 (12 days ago) • 2022-03-25 00:23:12 -0500\nEngine • revision 57d3bac3dd\nTools • Dart 2.16.2 • DevTools 2.9.2\n";
static const String buildAt = "2022-04-06 13:00:26.954649";
static const int modifications = 7;
}

View File

@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:toolbox/core/extension/numx.dart';
import 'package:toolbox/data/model/server/net_speed.dart';
import 'package:toolbox/data/model/server/server.dart';
import 'package:toolbox/data/model/server/server_status.dart';
@@ -177,24 +178,11 @@ class _ServerDetailPageState extends State<ServerDetailPage>
));
}
String convertMB(int mb) {
const suffix = ['MB', 'GB', 'TB'];
double value = mb.toDouble();
int squareTimes = 0;
for (; value / 1024 > 1 && squareTimes < 3; squareTimes++) {
value /= 1024;
}
var finalValue = value.toStringAsFixed(1);
if (finalValue.endsWith('.0')) {
finalValue = finalValue.replaceFirst('.0', '');
}
return '$finalValue ${suffix[squareTimes]}';
}
Widget _buildMemView(ServerStatus ss) {
final pColor = primaryColor;
final used = ss.memory.used / ss.memory.total;
final width = _media.size.width - 17 * 2 - 17 * 2;
const mb = 1024 * 1024;
return RoundRectCard(Padding(
padding: roundRectCardPadding,
child: SizedBox(
@@ -206,10 +194,12 @@ class _ServerDetailPageState extends State<ServerDetailPage>
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
_buildMemExplain(convertMB(ss.memory.used), pColor),
_buildMemExplain(
convertMB(ss.memory.cache), pColor.withAlpha(77)),
_buildMemExplain(convertMB(ss.memory.total - ss.memory.used),
(ss.memory.used * mb).convertBytes, pColor),
_buildMemExplain((ss.memory.cache * mb).convertBytes,
pColor.withAlpha(77)),
_buildMemExplain(
((ss.memory.total - ss.memory.used) * mb).convertBytes,
progressColor.resolve(context))
],
),

View File

@@ -1,8 +1,6 @@
import 'dart:io';
import 'package:dartssh2/dartssh2.dart';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'package:toolbox/core/extension/numx.dart';
import 'package:toolbox/core/utils.dart';
import 'package:toolbox/data/model/server/server_connection_state.dart';
import 'package:toolbox/data/model/server/server_private_info.dart';
@@ -10,7 +8,6 @@ import 'package:toolbox/data/model/sftp/absolute_path.dart';
import 'package:toolbox/data/model/sftp/sftp_side_status.dart';
import 'package:toolbox/data/provider/server.dart';
import 'package:toolbox/locator.dart';
import 'package:toolbox/view/widget/center_loading.dart';
import 'package:toolbox/view/widget/fade_in.dart';
import 'package:toolbox/view/widget/two_line_text.dart';
@@ -96,7 +93,7 @@ class _SFTPPageState extends State<SFTPPage> {
return _buildDestSelector();
}
final file = _status.files![index - 1];
final isDir = file.attr.mode?.isDirectory ?? true;
final isDir = file.attr.isDirectory;
return ListTile(
leading: Icon(isDir ? Icons.folder : Icons.insert_drive_file),
title: Text(file.filename),
@@ -108,7 +105,7 @@ class _SFTPPageState extends State<SFTPPage> {
style: const TextStyle(color: Colors.grey),
),
subtitle:
isDir ? null : Text(convertBytes(file.attr.size ?? 0)),
isDir ? null : Text((file.attr.size ?? 0).convertBytes),
onTap: () {
if (isDir) {
_status.path?.update(file.filename);
@@ -148,11 +145,11 @@ class _SFTPPageState extends State<SFTPPage> {
title: const Text('Rename'),
onTap: () => rename(context, file),
),
ListTile(
leading: const Icon(Icons.download),
title: const Text('Download'),
onTap: () => download(context, file),
)
// ListTile(
// leading: const Icon(Icons.download),
// title: const Text('Download'),
// onTap: () => download(context, file),
// )
],
),
[
@@ -162,41 +159,63 @@ class _SFTPPageState extends State<SFTPPage> {
]);
}
void download(BuildContext context, SftpName name) {
showRoundDialog(
context, 'Download', Text('Download ${name.filename} to local?'), [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('Cancel')),
TextButton(
onPressed: () async {
var result = '';
try {
Navigator.of(context).pop();
showRoundDialog(context, name.filename, centerSizedLoading, [],
barrierDismiss: false);
final path = await getApplicationDocumentsDirectory();
final localFile = File('${path.path}/${name.filename}');
final remotePath = _status.path!.path + '/' + name.filename;
final file = await _status.client?.open(remotePath);
localFile.writeAsBytes(await file!.readBytes());
Navigator.of(context).pop();
} catch (e) {
result = e.toString();
} finally {
if (result.isEmpty) {
result = 'Donwloaded successfully.';
}
showRoundDialog(context, 'Result', Text(result), [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('OK'))
]);
}
},
child: const Text('Download'))
]);
}
// void download(BuildContext context, SftpName name) {
// showRoundDialog(
// context, 'Download', Text('Download ${name.filename} to local?'), [
// TextButton(
// onPressed: () => Navigator.of(context).pop(),
// child: const Text('Cancel')),
// TextButton(
// onPressed: () async {
// var result = '';
// try {
// Navigator.of(context).pop();
// showRoundDialog(
// context,
// name.filename,
// const Text('Downloading...\nKepp this app in the foreground.',
// textAlign: TextAlign.center),
// [],
// barrierDismiss: false);
// final path = await sftpDownloadDir;
// final local = File('${path.path}/${name.filename}');
// if (await local.exists()) {
// await local.delete();
// }
// final localFile =
// await local.open(mode: FileMode.writeOnlyAppend);
// final remotePath = _status.path!.path + '/' + name.filename;
// final file = await _status.client!.open(remotePath);
// final size = (await file.stat()).size;
// if (size == null) {
// throw Exception('can not get file size');
// }
// const chunkSize = 1024 * 128;
// for (var i = 0; i < size; i += chunkSize) {
// final data = file.read(length: chunkSize);
// await for (var item in data) {
// localFile.writeFrom(item);
// }
// }
// } catch (e) {
// result = e.toString();
// } finally {
// Navigator.of(context).pop();
// if (result.isEmpty) {
// result = 'Donwloaded successfully.';
// }
// showRoundDialog(context, 'Result', Text(result), [
// TextButton(
// onPressed: () => Navigator.of(context).pop(),
// child: const Text('OK'))
// ]);
// }
// },
// child: const Text('Download'))
// ]);
// }
void delete(BuildContext context, SftpName file) {
Navigator.of(context).pop();
@@ -296,20 +315,6 @@ class _SFTPPageState extends State<SFTPPage> {
]);
}
String convertBytes(int bytes) {
const suffix = ['B', 'KB', 'MB', 'GB', 'TB'];
double value = bytes.toDouble();
int squareTimes = 0;
for (; value / 1024 > 1 && squareTimes < 3; squareTimes++) {
value /= 1024;
}
var finalValue = value.toStringAsFixed(1);
if (finalValue.endsWith('.0')) {
finalValue = finalValue.replaceFirst('.0', '');
}
return '$finalValue ${suffix[squareTimes]}';
}
Future<void> listDir({String? path, SSHClient? client}) async {
if (_status.isBusy) {
return;