Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 解决网站日志查看报错的问题 #4082

Merged
merged 1 commit into from
Mar 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions backend/utils/files/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package files
import (
"bufio"
"github.com/spf13/afero"
"io"
"net/http"
"os"
"os/user"
Expand Down Expand Up @@ -87,27 +88,27 @@ func ReadFileByLine(filename string, page, pageSize int) ([]string, bool, error)
}
defer file.Close()

scanner := bufio.NewScanner(file)
reader := bufio.NewReaderSize(file, 8192)

var lines []string
currentLine := 0
startLine := (page - 1) * pageSize
endLine := startLine + pageSize

for scanner.Scan() {
for {
line, _, err := reader.ReadLine()
if err == io.EOF {
break
}
if currentLine >= startLine && currentLine < endLine {
lines = append(lines, scanner.Text())
lines = append(lines, string(line))
}
currentLine++
if currentLine >= endLine {
break
}
}

if err := scanner.Err(); err != nil {
return nil, false, err
}

isEndOfFile := currentLine < endLine

return lines, isEndOfFile, nil
Expand Down
Loading