Skip to content

Commit

Permalink
Fix record import via some CSV files
Browse files Browse the repository at this point in the history
Some CSV files failed to detect as text/csv so the import failed.
ref: gabriel-vasile/mimetype#138
  • Loading branch information
tjerman committed Jan 15, 2021
1 parent 2eea2ad commit ce21db2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
5 changes: 4 additions & 1 deletion compose/rest/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions compose/service/import_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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()

Expand Down Expand Up @@ -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)
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/envoy/csv/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit ce21db2

Please sign in to comment.