3
0

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
This commit is contained in:
Tomaž Jerman 2021-01-15 12:12:01 +01:00
parent 2eea2ad908
commit ce21db2197
3 changed files with 11 additions and 4 deletions

View File

@ -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) {

View File

@ -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)
}

View File

@ -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"