Skip to content

Commit

Permalink
fix: 解决网站日志查看报错的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengkunwang223 committed Mar 6, 2024
1 parent a99f194 commit 8cd8bd1
Showing 1 changed file with 8 additions and 7 deletions.
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

0 comments on commit 8cd8bd1

Please sign in to comment.