Skip to content

High performance, self-hosted, newsletter and mailing list manager with a modern dashboard. Single binary app.

Fix incorrect line counts due to empty lines in CSV importer (#2542)

Adam Howard419f88a9c3a7
diff --git a/internal/subimporter/importer.go b/internal/subimporter/importer.go
index 761dae27..79206576 100644
--- a/internal/subimporter/importer.go
+++ b/internal/subimporter/importer.go
@@ -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)
-
- switch {
- case err == io.EOF:
- return count, nil
+ if c > 0 {
+ count += bytes.Count(buf[:c], []byte{lineSep})
+ lastByte = buf[c-1]
+ }
- 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) {