From 8cd8bd1aa157462fba5d89f85067504e288077cc Mon Sep 17 00:00:00 2001 From: zhengkunwang223 Date: Wed, 6 Mar 2024 11:42:02 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A7=A3=E5=86=B3=E7=BD=91=E7=AB=99?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E6=9F=A5=E7=9C=8B=E6=8A=A5=E9=94=99=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/utils/files/utils.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/backend/utils/files/utils.go b/backend/utils/files/utils.go index def018e1e02a..c6c4aec53596 100644 --- a/backend/utils/files/utils.go +++ b/backend/utils/files/utils.go @@ -3,6 +3,7 @@ package files import ( "bufio" "github.com/spf13/afero" + "io" "net/http" "os" "os/user" @@ -87,16 +88,20 @@ 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 { @@ -104,10 +109,6 @@ func ReadFileByLine(filename string, page, pageSize int) ([]string, bool, error) } } - if err := scanner.Err(); err != nil { - return nil, false, err - } - isEndOfFile := currentLine < endLine return lines, isEndOfFile, nil