1
0
mirror of https://github.com/mattermost/focalboard.git synced 2024-12-21 13:38:56 +02:00

Updates import to read with a scanner (#4788)

* Updates import to read with a scanner

* Fix linter
This commit is contained in:
Miguel de la Cruz 2023-06-22 11:31:08 +02:00 committed by GitHub
parent 343b7bdc4b
commit 625526c3e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -20,12 +20,14 @@ import (
) )
const ( const (
archiveVersion = 2 archiveVersion = 2
legacyFileBegin = "{\"version\":1" legacyFileBegin = "{\"version\":1"
importMaxFileSize = 1024 * 1024 * 70
) )
var ( var (
errBlockIsNotABoard = errors.New("block is not a board") errBlockIsNotABoard = errors.New("block is not a board")
errSizeLimitExceeded = errors.New("size limit exceeded")
) )
// ImportArchive imports an archive containing zero or more boards, plus all // ImportArchive imports an archive containing zero or more boards, plus all
@ -153,7 +155,8 @@ func (a *App) ImportBoardJSONL(r io.Reader, opt model.ImportArchiveOptions) (*mo
Blocks: make([]*model.Block, 0, 10), Blocks: make([]*model.Block, 0, 10),
Boards: make([]*model.Board, 0, 10), Boards: make([]*model.Board, 0, 10),
} }
lineReader := bufio.NewReader(r) lineReader := &io.LimitedReader{R: r, N: importMaxFileSize + 1}
scanner := bufio.NewScanner(lineReader)
userID := opt.ModifiedBy userID := opt.ModifiedBy
if userID == model.SingleUser { if userID == model.SingleUser {
@ -165,8 +168,12 @@ func (a *App) ImportBoardJSONL(r io.Reader, opt model.ImportArchiveOptions) (*mo
lineNum := 1 lineNum := 1
firstLine := true firstLine := true
for { for scanner.Scan() {
line, errRead := readLine(lineReader) if lineReader.N <= 0 {
return nil, fmt.Errorf("error parsing archive line %d: %w", lineNum, errSizeLimitExceeded)
}
line := bytes.TrimSpace(scanner.Bytes())
if len(line) != 0 { if len(line) != 0 {
var skip bool var skip bool
if firstLine { if firstLine {
@ -233,14 +240,10 @@ func (a *App) ImportBoardJSONL(r io.Reader, opt model.ImportArchiveOptions) (*mo
firstLine = false firstLine = false
} }
} }
}
if errRead != nil { if errRead := scanner.Err(); errRead != nil {
if errors.Is(errRead, io.EOF) { return nil, fmt.Errorf("error reading archive line %d: %w", lineNum, errRead)
break
}
return nil, fmt.Errorf("error reading archive line %d: %w", lineNum, errRead)
}
lineNum++
} }
// loop to remove the people how are not part of the team and system // loop to remove the people how are not part of the team and system
@ -462,9 +465,3 @@ func parseVersionFile(r io.Reader) (int, error) {
} }
return header.Version, nil return header.Version, nil
} }
func readLine(r *bufio.Reader) ([]byte, error) {
line, err := r.ReadBytes('\n')
line = bytes.TrimSpace(line)
return line, err
}