Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mimeType detection for CSV file #353

Merged
merged 3 commits into from Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 8 additions & 7 deletions compose/service/attachment.go
Expand Up @@ -6,7 +6,6 @@ import (
"image"
"image/gif"
"io"
"net/http"
"path"
"regexp"
"strings"
Expand All @@ -20,6 +19,7 @@ import (
systemService "github.com/cortezaproject/corteza-server/system/service"
"github.com/disintegration/imaging"
"github.com/edwvee/exiffix"
"github.com/gabriel-vasile/mimetype"
)

const (
Expand Down Expand Up @@ -510,21 +510,22 @@ func (svc attachment) create(ctx context.Context, s store.ComposeAttachments, na
return nil
}

func (svc attachment) extractMimetype(file io.ReadSeeker) (mimetype string, err error) {
func (svc attachment) extractMimetype(file io.ReadSeeker) (mType string, err error) {
if _, err = file.Seek(0, 0); err != nil {
return
}

// Make sure we rewind when we're done
defer file.Seek(0, 0)
defer func(file io.ReadSeeker, offset int64, whence int) {
_, _ = file.Seek(offset, whence)
}(file, 0, 0)

// See http.DetectContentType about 512 bytes
var buf = make([]byte, 512)
if _, err = file.Read(buf); err != nil {
var mime *mimetype.MIME
if mime, err = mimetype.DetectReader(file); err != nil {
return
}

return http.DetectContentType(buf), nil
return mime.String(), nil
}

func (svc attachment) processImage(original io.ReadSeeker, att *types.Attachment) (err error) {
Expand Down
6 changes: 2 additions & 4 deletions tests/compose/page_test.go
Expand Up @@ -369,9 +369,7 @@ func TestPageAttachment(t *testing.T) {

// one megabyte limit
systemService.CurrentSettings.Compose.Page.Attachments.MaxSize = 1
systemService.CurrentSettings.Compose.Page.Attachments.Mimetypes = []string{
"application/octet-stream",
}
systemService.CurrentSettings.Compose.Page.Attachments.Mimetypes = []string{}

cc := []struct {
name string
Expand Down Expand Up @@ -419,7 +417,7 @@ func TestPageAttachment(t *testing.T) {
"numbers.gif",
"image/gif",
map[string]string{},
helpers.AssertError("attachment.errors.notAllowedToUploadThisType"),
helpers.AssertError("attachment.errors.failedToProcessImage"),
},
}

Expand Down
24 changes: 20 additions & 4 deletions tests/compose/record_test.go
Expand Up @@ -796,23 +796,31 @@ func TestRecordAttachment(t *testing.T) {
},
},
&types.ModuleField{Name: "str", Kind: "String"},
&types.ModuleField{
Name: "csv_only",
Kind: "File",
Options: types.ModuleFieldOptions{
"mimetypes": "text/csv",
},
},
)

xxlBlob := bytes.Repeat([]byte("0"), maxSizeLimit*1_000_000+1)

testImgFh, err := os.ReadFile("./testdata/test.png")
h.noError(err)

testCsvFh, err := os.ReadFile("./testdata/test.csv")
h.noError(err)

defer func() {
// reset settings after we're done
systemService.CurrentSettings.Compose.Record.Attachments.MaxSize = 0
systemService.CurrentSettings.Compose.Record.Attachments.Mimetypes = nil
}()

systemService.CurrentSettings.Compose.Record.Attachments.MaxSize = maxSizeLimit
systemService.CurrentSettings.Compose.Record.Attachments.Mimetypes = []string{
"application/octet-stream",
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing this; No need to add fallback allowed mime-type after updating mime-type detection func, It extracts file mime-type more accurately now.

systemService.CurrentSettings.Compose.Record.Attachments.Mimetypes = []string{}

cc := []struct {
name string
Expand Down Expand Up @@ -892,7 +900,7 @@ func TestRecordAttachment(t *testing.T) {
"numbers.gif",
"image/gif",
map[string]string{"fieldName": "no_constraints"},
helpers.AssertError("attachment.errors.notAllowedToUploadThisType"),
helpers.AssertError("attachment.errors.failedToProcessImage"),
},
{
"field mimetype - ok",
Expand All @@ -910,6 +918,14 @@ func TestRecordAttachment(t *testing.T) {
map[string]string{"fieldName": "img_only"},
helpers.AssertNoErrors,
},
{
"csv file - ok",
testCsvFh,
"testCSV",
"text/csv",
map[string]string{"fieldName": "csv_only"},
helpers.AssertNoErrors,
},
}

for _, c := range cc {
Expand Down
6 changes: 6 additions & 0 deletions tests/compose/testdata/test.csv
@@ -0,0 +1,6 @@
John,Doe,120 jefferson st.,Riverside, NJ, 08075
Jack,McGinnis,220 hobo Av.,Phila, PA,09119
"John ""Da Man""",Repici,120 Jefferson St.,Riverside, NJ,08075
Stephen,Tyler,"7452 Terrace ""At the Plaza"" road",SomeTown,SD, 91234
,Blankman,,SomeTown, SD, 00298
"Joan ""the bone"", Anne",Jet,"9th, at Terrace plc",Desert City,CO,00123