mirror of
https://github.com/knadh/listmonk.git
synced 2025-10-06 13:26:17 +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
|
// Credit: https://stackoverflow.com/a/24563853
|
||||||
func countLines(r io.Reader) (int, error) {
|
func countLines(r io.Reader) (int, error) {
|
||||||
var (
|
var (
|
||||||
buf = make([]byte, 32*1024)
|
buf = make([]byte, 32*1024)
|
||||||
count = 0
|
count = 0
|
||||||
lineSep = []byte{'\n'}
|
lineSep = byte('\n')
|
||||||
|
lastByte byte
|
||||||
)
|
)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
c, err := r.Read(buf)
|
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 {
|
if err == io.EOF {
|
||||||
case err == io.EOF:
|
break
|
||||||
return count, nil
|
}
|
||||||
|
if err != nil {
|
||||||
case err != nil:
|
|
||||||
return count, err
|
return count, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if lastByte != 0 && lastByte != lineSep {
|
||||||
|
count++
|
||||||
|
}
|
||||||
|
|
||||||
|
return count, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeDomainMap(domains []string) (map[string]struct{}, bool) {
|
func makeDomainMap(domains []string) (map[string]struct{}, bool) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue