Skip to content

Commit

Permalink
Updates import to read with a scanner (#4788) (#4789)
Browse files Browse the repository at this point in the history
* Updates import to read with a scanner

* Fix linter

(cherry picked from commit 625526c)

Co-authored-by: Miguel de la Cruz <[email protected]>
  • Loading branch information
mattermost-build and mgdelacroix committed Jun 22, 2023
1 parent 88cfee1 commit be9f99b
Showing 1 changed file with 16 additions and 19 deletions.
35 changes: 16 additions & 19 deletions server/app/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ import (
)

const (
archiveVersion = 2
legacyFileBegin = "{\"version\":1"
archiveVersion = 2
legacyFileBegin = "{\"version\":1"
importMaxFileSize = 1024 * 1024 * 70
)

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
Expand Down Expand Up @@ -153,7 +155,8 @@ func (a *App) ImportBoardJSONL(r io.Reader, opt model.ImportArchiveOptions) (*mo
Blocks: make([]*model.Block, 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
if userID == model.SingleUser {
Expand All @@ -165,8 +168,12 @@ func (a *App) ImportBoardJSONL(r io.Reader, opt model.ImportArchiveOptions) (*mo

lineNum := 1
firstLine := true
for {
line, errRead := readLine(lineReader)
for scanner.Scan() {
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 {
var skip bool
if firstLine {
Expand Down Expand Up @@ -233,14 +240,10 @@ func (a *App) ImportBoardJSONL(r io.Reader, opt model.ImportArchiveOptions) (*mo
firstLine = false
}
}
}

if errRead != nil {
if errors.Is(errRead, io.EOF) {
break
}
return nil, fmt.Errorf("error reading archive line %d: %w", lineNum, errRead)
}
lineNum++
if errRead := scanner.Err(); errRead != nil {
return nil, fmt.Errorf("error reading archive line %d: %w", lineNum, errRead)
}

// loop to remove the people how are not part of the team and system
Expand Down Expand Up @@ -462,9 +465,3 @@ func parseVersionFile(r io.Reader) (int, error) {
}
return header.Version, nil
}

func readLine(r *bufio.Reader) ([]byte, error) {
line, err := r.ReadBytes('\n')
line = bytes.TrimSpace(line)
return line, err
}

0 comments on commit be9f99b

Please sign in to comment.