Add fix for BOM in CSV importers

This commit is contained in:
Kailash Nadh 2020-03-09 19:47:20 +05:30
parent ad9ee7528d
commit faf8a7ae63
1 changed files with 4 additions and 0 deletions

View File

@ -104,6 +104,8 @@ var (
// https://www.alexedwards.net/blog/validation-snippets-for-go#email-validation // https://www.alexedwards.net/blog/validation-snippets-for-go#email-validation
regexEmail = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$") regexEmail = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
regexCleanStr = regexp.MustCompile("[[:^ascii:]]")
) )
// New returns a new instance of Importer. // New returns a new instance of Importer.
@ -555,6 +557,8 @@ func (s *Session) mapCSVHeaders(csvHdrs []string, knownHdrs map[string]bool) map
// This is to allow dynamic ordering of columns in th CSV. // This is to allow dynamic ordering of columns in th CSV.
hdrKeys := make(map[string]int) hdrKeys := make(map[string]int)
for i, h := range csvHdrs { for i, h := range csvHdrs {
// Clean the string of non-ASCII characters (BOM etc.).
h := regexCleanStr.ReplaceAllString(h, "")
if _, ok := knownHdrs[h]; !ok { if _, ok := knownHdrs[h]; !ok {
s.log.Printf("ignoring unknown header '%s'", h) s.log.Printf("ignoring unknown header '%s'", h)
continue continue