feat: 设置新增服务器日志页(SFTP 读取 logs 目录,支持自动刷新与跟随底部)
This commit is contained in:
1 parent
71e76b1c2b
commit
eb2f9e9ea7
4 files changed
+416
No files matched your search
@@ -395,6 +395,16 @@ class S {
|
||||
'视频': 'Video',
|
||||
'图片': 'Image',
|
||||
'连接凭据已加密保存,仅存于本机': 'Credentials are encrypted and stored on this device only',
|
||||
'服务器日志': 'Server Logs',
|
||||
'通过 SSH 查看服务器 logs 目录日志输出': 'View server logs directory output via SSH',
|
||||
'请先在「服务器」页配置 SSH 连接': 'Configure an SSH connection on the Servers page first',
|
||||
'选择服务器(需开启 SSH)': 'Select server (SSH required)',
|
||||
'日志目录路径': 'Logs directory path',
|
||||
'例如 /root/logs': 'e.g. /root/logs',
|
||||
'自动刷新(每 5 秒)': 'Auto refresh (every 5s)',
|
||||
'目录下没有日志文件': 'No log files in this directory',
|
||||
'读取失败:{0}': 'Failed to read: {0}',
|
||||
'自动刷新并跟随底部(每 5 秒)': 'Auto refresh & follow bottom (every 5s)',
|
||||
};
|
||||
|
||||
static String t(String zh) => en ? (_en[zh] ?? zh) : zh;
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:typed_data';
|
||||
import 'package:dartssh2/dartssh2.dart';
|
||||
import 'dart:io' as io;
|
||||
@@ -83,6 +84,31 @@ class SftpService {
|
||||
}
|
||||
}
|
||||
|
||||
/// 读取远程文件文本内容(保留末尾 maxBytes 字节,适合大日志文件)。
|
||||
Future<String> readString(
|
||||
String remotePath, {
|
||||
int maxBytes = 2 * 1024 * 1024,
|
||||
}) async {
|
||||
final sftp = _requireSftp();
|
||||
final file = await sftp.open(remotePath, mode: SftpFileOpenMode.read);
|
||||
try {
|
||||
final buf = BytesBuilder();
|
||||
await for (final chunk in file.read()) {
|
||||
buf.add(chunk);
|
||||
}
|
||||
final bytes = buf.takeBytes();
|
||||
if (bytes.length <= maxBytes) return utf8.decode(bytes, allowMalformed: true);
|
||||
// 只保留末尾 maxBytes,并跳过半个字符
|
||||
var start = bytes.length - maxBytes;
|
||||
while (start < bytes.length && (bytes[start] & 0xC0) == 0x80) {
|
||||
start++;
|
||||
}
|
||||
return utf8.decode(bytes.sublist(start), allowMalformed: true);
|
||||
} finally {
|
||||
await file.close();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> delete(String remotePath) async {
|
||||
await _requireSftp().remove(remotePath);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user