mirror of
https://github.com/knadh/listmonk.git
synced 2025-10-01 02:47:40 +08:00
Fix incorrect line counts due to empty lines in CSV importer (#2542)
This commit is contained in:
parent
aa864fd5bf
commit
419f88a9c3
1 changed files with 18 additions and 9 deletions
|
@ -707,23 +707,32 @@ func (s *Session) mapCSVHeaders(csvHdrs []string, knownHdrs map[string]bool) map
|
|||
// Credit: https://stackoverflow.com/a/24563853
|
||||
func countLines(r io.Reader) (int, error) {
|
||||
var (
|
||||
buf = make([]byte, 32*1024)
|
||||
count = 0
|
||||
lineSep = []byte{'\n'}
|
||||
buf = make([]byte, 32*1024)
|
||||
count = 0
|
||||
lineSep = byte('\n')
|
||||
lastByte byte
|
||||
)
|
||||
|
||||
for {
|
||||
c, err := r.Read(buf)
|
||||
count += bytes.Count(buf[:c], lineSep)
|
||||
if c > 0 {
|
||||
count += bytes.Count(buf[:c], []byte{lineSep})
|
||||
lastByte = buf[c-1]
|
||||
}
|
||||
|
||||
switch {
|
||||
case err == io.EOF:
|
||||
return count, nil
|
||||
|
||||
case err != nil:
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
return count, err
|
||||
}
|
||||
}
|
||||
|
||||
if lastByte != 0 && lastByte != lineSep {
|
||||
count++
|
||||
}
|
||||
|
||||
return count, nil
|
||||
}
|
||||
|
||||
func makeDomainMap(domains []string) (map[string]struct{}, bool) {
|
||||
|
|
Loading…
Add table
Reference in a new issue