diff --git a/backend/app/dto/request/file.go b/backend/app/dto/request/file.go index 82c2aadb2..0e61d41f5 100644 --- a/backend/app/dto/request/file.go +++ b/backend/app/dto/request/file.go @@ -126,6 +126,7 @@ type FileReadByLineReq struct { Type string `json:"type" validate:"required"` ID uint `json:"ID"` Name string `json:"name"` + Latest bool `json:"latest"` } type FileExistReq struct { diff --git a/backend/app/dto/response/file.go b/backend/app/dto/response/file.go index 43b5017e6..b67447bd5 100644 --- a/backend/app/dto/response/file.go +++ b/backend/app/dto/response/file.go @@ -37,6 +37,7 @@ type FileLineContent struct { Content string `json:"content"` End bool `json:"end"` Path string `json:"path"` + Total int `json:"total"` } type FileExist struct { diff --git a/backend/app/service/file.go b/backend/app/service/file.go index 177b7e839..9252c4566 100644 --- a/backend/app/service/file.go +++ b/backend/app/service/file.go @@ -410,7 +410,7 @@ func (f *FileService) ReadLogByLine(req request.FileReadByLineReq) (*response.Fi logFilePath = path.Join(global.CONF.System.TmpDir, fmt.Sprintf("docker_logs/%s", req.Name)) } - lines, isEndOfFile, err := files.ReadFileByLine(logFilePath, req.Page, req.PageSize) + lines, isEndOfFile, total, err := files.ReadFileByLine(logFilePath, req.Page, req.PageSize, req.Latest) if err != nil { return nil, err } @@ -418,6 +418,7 @@ func (f *FileService) ReadLogByLine(req request.FileReadByLineReq) (*response.Fi Content: strings.Join(lines, "\n"), End: isEndOfFile, Path: logFilePath, + Total: total, } return res, nil } diff --git a/backend/app/service/website.go b/backend/app/service/website.go index 624e2dfe0..35a39ad98 100644 --- a/backend/app/service/website.go +++ b/backend/app/service/website.go @@ -1008,7 +1008,7 @@ func (w WebsiteService) OpWebsiteLog(req request.WebsiteLogReq) (*response.Websi } } filePath := path.Join(sitePath, "log", req.LogType) - lines, end, err := files.ReadFileByLine(filePath, req.Page, req.PageSize) + lines, end, _, err := files.ReadFileByLine(filePath, req.Page, req.PageSize, false) if err != nil { return nil, err } diff --git a/backend/utils/files/utils.go b/backend/utils/files/utils.go index 206910d0a..76ba78156 100644 --- a/backend/utils/files/utils.go +++ b/backend/utils/files/utils.go @@ -66,19 +66,44 @@ func IsHidden(path string) bool { return path[0] == dotCharacter } -func ReadFileByLine(filename string, page, pageSize int) ([]string, bool, error) { - if !NewFileOp().Stat(filename) { - return nil, true, nil - } - file, err := os.Open(filename) +func countLines(path string) (int, error) { + file, err := os.Open(path) if err != nil { - return nil, false, err + return 0, err } defer file.Close() + scanner := bufio.NewScanner(file) + lineCount := 0 + for scanner.Scan() { + lineCount++ + } + if err := scanner.Err(); err != nil { + return 0, err + } + return lineCount, nil +} + +func ReadFileByLine(filename string, page, pageSize int, latest bool) (lines []string, isEndOfFile bool, total int, err error) { + if !NewFileOp().Stat(filename) { + return + } + file, err := os.Open(filename) + if err != nil { + return + } + defer file.Close() + + totalLines, err := countLines(filename) + if err != nil { + return + } + total = (totalLines + pageSize - 1) / pageSize reader := bufio.NewReaderSize(file, 8192) - var lines []string + if latest { + page = total + } currentLine := 0 startLine := (page - 1) * pageSize endLine := startLine + pageSize @@ -97,9 +122,8 @@ func ReadFileByLine(filename string, page, pageSize int) ([]string, bool, error) } } - isEndOfFile := currentLine < endLine - - return lines, isEndOfFile, nil + isEndOfFile = currentLine < endLine + return } func GetParentMode(path string) (os.FileMode, error) { diff --git a/frontend/package.json b/frontend/package.json index 70807860f..fcd8be5f4 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -1,7 +1,7 @@ { "name": "1Panel-Frontend", "private": true, - "version": "1.7", + "version": "1.10", "description": "1Panel 前端", "scripts": { "dev": "vite", @@ -25,6 +25,7 @@ "@codemirror/legacy-modes": "^6.4.0", "@codemirror/theme-one-dark": "^6.1.2", "@element-plus/icons-vue": "^1.1.4", + "@highlightjs/vue-plugin": "^2.1.0", "@vueuse/core": "^8.9.4", "@xterm/addon-fit": "^0.10.0", "@xterm/xterm": "^5.5.0", @@ -32,6 +33,7 @@ "echarts": "^5.5.0", "element-plus": "^2.7.5", "fit2cloud-ui-plus": "^1.1.4", + "highlight.js": "^11.9.0", "js-base64": "^3.7.7", "md-editor-v3": "^2.11.3", "monaco-editor": "^0.34.1", diff --git a/frontend/src/components/log-file/index.vue b/frontend/src/components/log-file/index.vue index 9d49881b6..c95d7c3f2 100644 --- a/frontend/src/components/log-file/index.vue +++ b/frontend/src/components/log-file/index.vue @@ -12,35 +12,23 @@