From ce21db21970b80218f313ae41eebe450b13a129d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toma=C5=BE=20Jerman?= Date: Fri, 15 Jan 2021 12:12:01 +0100 Subject: [PATCH] Fix record import via some CSV files Some CSV files failed to detect as text/csv so the import failed. ref: https://github.com/gabriel-vasile/mimetype/issues/138 --- compose/rest/record.go | 5 ++++- compose/service/import_session.go | 6 +++--- pkg/envoy/csv/decoder.go | 4 ++++ 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/compose/rest/record.go b/compose/rest/record.go index db1171a815..a105456593 100644 --- a/compose/rest/record.go +++ b/compose/rest/record.go @@ -285,7 +285,10 @@ func (ctrl *Record) ImportInit(ctx context.Context, r *request.RecordImportInit) } defer f.Close() - return ctrl.importSession.Create(ctx, f, r.Upload.Filename, r.NamespaceID, r.ModuleID) + // Mime type detection library fails for some .csv files, so let's help them out a bit. + // The detection can now fallback to the user-provided content-type. + ct := r.Upload.Header.Get("Content-Type") + return ctrl.importSession.Create(ctx, f, r.Upload.Filename, ct, r.NamespaceID, r.ModuleID) } func (ctrl *Record) ImportRun(ctx context.Context, r *request.RecordImportRun) (interface{}, error) { diff --git a/compose/service/import_session.go b/compose/service/import_session.go index af843f6736..10c8238b51 100644 --- a/compose/service/import_session.go +++ b/compose/service/import_session.go @@ -23,7 +23,7 @@ type ( } ImportSessionService interface { - Create(ctx context.Context, f io.ReadSeeker, name string, namespaceID, moduleID uint64) (*recordImportSession, error) + Create(ctx context.Context, f io.ReadSeeker, name, contentType string, namespaceID, moduleID uint64) (*recordImportSession, error) FindByID(ctx context.Context, sessionID uint64) (*recordImportSession, error) DeleteByID(ctx context.Context, sessionID uint64) error } @@ -45,7 +45,7 @@ func (svc *importSession) indexOf(userID, sessionID uint64) int { return -1 } -func (svc *importSession) Create(ctx context.Context, f io.ReadSeeker, name string, namespaceID, moduleID uint64) (*recordImportSession, error) { +func (svc *importSession) Create(ctx context.Context, f io.ReadSeeker, name, contentType string, namespaceID, moduleID uint64) (*recordImportSession, error) { svc.l.Lock() defer svc.l.Unlock() @@ -77,7 +77,7 @@ func (svc *importSession) Create(ctx context.Context, f io.ReadSeeker, name stri } sh.Resources, err = func() ([]resource.Interface, error) { - if cd.CanDecodeFile(f) { + if cd.CanDecodeFile(f) || cd.CanDecodeMime(contentType) { f.Seek(0, 0) return cd.Decode(ctx, f, do) } diff --git a/pkg/envoy/csv/decoder.go b/pkg/envoy/csv/decoder.go index dfa71b2965..afbf3e61f1 100644 --- a/pkg/envoy/csv/decoder.go +++ b/pkg/envoy/csv/decoder.go @@ -39,6 +39,10 @@ func (y *decoder) CanDecodeFile(f io.Reader) bool { return y.CanDecodeExt(ext) } +func (y *decoder) CanDecodeMime(m string) bool { + return m == "text/csv" +} + func (y *decoder) CanDecodeExt(ext string) bool { pt := strings.Split(ext, ".") return strings.TrimSpace(pt[len(pt)-1]) == "csv"