docs: 为核心协议层、服务层与主要页面补充代码注释,修复下载进度计算逻辑
This commit is contained in:
1 parent
7e618adf6c
commit
854f00038c
14 files changed
+244
-28
No files matched your search
@@ -11,11 +11,15 @@ import '../../core/l10n/strings.dart';
|
||||
class SftpService {
|
||||
SSHClient? _client;
|
||||
SftpClient? _sftp;
|
||||
|
||||
/// 当前连接绑定的服务器,用于判断能否复用已有连接
|
||||
ServerConfig? _bound;
|
||||
|
||||
bool get isConnected => _client != null && !_client!.isClosed;
|
||||
|
||||
/// 连接到指定服务器的 SSH。失败抛异常。
|
||||
///
|
||||
/// 如果已经连到同一台服务器则直接复用,避免重复握手(SSH 握手较慢)。
|
||||
Future<void> connect(ServerConfig server) async {
|
||||
if (isConnected && _bound?.id == server.id) return;
|
||||
await disconnect();
|
||||
@@ -27,6 +31,7 @@ class SftpService {
|
||||
_client = SSHClient(
|
||||
socket,
|
||||
username: server.sshUser,
|
||||
// 目前仅支持密码认证;密钥认证需在设置里扩展
|
||||
onPasswordRequest: () => server.sshPassword,
|
||||
);
|
||||
_sftp = await _client!.sftp();
|
||||
@@ -39,6 +44,9 @@ class SftpService {
|
||||
}
|
||||
|
||||
/// 上传本地文件到远程路径。
|
||||
///
|
||||
/// 采用 create + truncate 模式覆盖写入;流式读取本地文件,
|
||||
/// 大文件不会一次性读进内存。
|
||||
Future<void> upload(String localPath, String remotePath) async {
|
||||
final sftp = _requireSftp();
|
||||
final file = await sftp.open(
|
||||
@@ -60,6 +68,8 @@ class SftpService {
|
||||
}
|
||||
|
||||
/// 下载远程文件到本地路径。
|
||||
///
|
||||
/// [onProgress] 参数为 (已下载字节, 总字节);总字节未知时为 0。
|
||||
Future<void> download(
|
||||
String remotePath,
|
||||
String localPath, {
|
||||
@@ -85,6 +95,10 @@ class SftpService {
|
||||
}
|
||||
|
||||
/// 读取远程文件文本内容(保留末尾 maxBytes 字节,适合大日志文件)。
|
||||
///
|
||||
/// 服务器日志动辄上百 MB,全量读取会撑爆内存且价值不大,
|
||||
/// 因此超限时只保留尾部——排查崩溃时真正关心的是最后那段堆栈。
|
||||
/// 截断位置会向前对齐到 UTF-8 字符边界,避免出现乱码。
|
||||
Future<String> readString(
|
||||
String remotePath, {
|
||||
int maxBytes = 2 * 1024 * 1024,
|
||||
|
||||
Reference in new issue
Block a user