import 'dart:typed_data'; import 'package:dartssh2/dartssh2.dart'; import 'dart:io' as io; import '../../providers/server_provider.dart'; /// 基于 dartssh2 的 SFTP 文件服务。 /// 一次连接同时支持列目录 / 上传 / 下载 / 删除 / 重命名, /// 供模组管理(M1)与下载完成后自动上传(M2)复用。 class SftpService { SSHClient? _client; SftpClient? _sftp; ServerConfig? _bound; bool get isConnected => _client != null && !_client!.isClosed; /// 连接到指定服务器的 SSH。失败抛异常。 Future connect(ServerConfig server) async { if (isConnected && _bound?.id == server.id) return; await disconnect(); final socket = await SSHSocket.connect( server.host, server.sshPort, timeout: const Duration(seconds: 10), ); _client = SSHClient( socket, username: server.sshUser, onPasswordRequest: () => server.sshPassword, ); _sftp = await _client!.sftp(); _bound = server; } /// 列出目录内容。 Future> listDir(String path) async { return _requireSftp().listdir(path); } /// 上传本地文件到远程路径。 Future upload(String localPath, String remotePath) async { final sftp = _requireSftp(); final file = await sftp.open( remotePath, mode: SftpFileOpenMode.create | SftpFileOpenMode.truncate | SftpFileOpenMode.write, ); try { final local = io.File(localPath); final writer = file.write( local.openRead().map((chunk) => Uint8List.fromList(chunk)), onProgress: (_) {}, ); // 预读 stat.size 仅用于日志/进度;writer 自行完成 await writer.done; } finally { await file.close(); } } /// 下载远程文件到本地路径。 Future download( String remotePath, String localPath, { void Function(int, int)? onProgress, }) async { final sftp = _requireSftp(); final stat = await sftp.stat(remotePath); final total = stat.size ?? 0; final file = await sftp.open(remotePath, mode: SftpFileOpenMode.read); try { final sink = io.File(localPath).openWrite(); var received = 0; await for (final chunk in file.read(onProgress: null)) { sink.add(chunk); received += chunk.length; onProgress?.call(received, total); } await sink.flush(); await sink.close(); } finally { await file.close(); } } Future delete(String remotePath) async { await _requireSftp().remove(remotePath); } Future rename(String remotePath, String newName) async { final sftp = _requireSftp(); final parent = remotePath.substring(0, remotePath.lastIndexOf('/') + 1); await sftp.rename(remotePath, '$parent$newName'); } Future mkdir(String remotePath) async { try { await _requireSftp().mkdir(remotePath); } catch (_) { // 已存在时忽略 } } SftpClient _requireSftp() { final sftp = _sftp; if (sftp == null || !isConnected) { throw StateError('SFTP 未连接,请先调用 connect()'); } return sftp; } Future disconnect() async { _sftp = null; _client?.close(); _client = null; _bound = null; } }