Fix mimeType detection for CSV file
This commit is contained in:
committed by
Tomaž Jerman
parent
2898e1b8c4
commit
195c2bb13e
@@ -6,7 +6,6 @@ import (
|
||||
"image"
|
||||
"image/gif"
|
||||
"io"
|
||||
"net/http"
|
||||
"path"
|
||||
"regexp"
|
||||
"strings"
|
||||
@@ -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 (
|
||||
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
@@ -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"),
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -796,6 +796,13 @@ 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)
|
||||
@@ -803,6 +810,9 @@ func TestRecordAttachment(t *testing.T) {
|
||||
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
|
||||
@@ -810,9 +820,7 @@ func TestRecordAttachment(t *testing.T) {
|
||||
}()
|
||||
|
||||
systemService.CurrentSettings.Compose.Record.Attachments.MaxSize = maxSizeLimit
|
||||
systemService.CurrentSettings.Compose.Record.Attachments.Mimetypes = []string{
|
||||
"application/octet-stream",
|
||||
}
|
||||
systemService.CurrentSettings.Compose.Record.Attachments.Mimetypes = []string{}
|
||||
|
||||
cc := []struct {
|
||||
name string
|
||||
@@ -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",
|
||||
@@ -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 {
|
||||
|
||||
Vendored
+6
@@ -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
|
||||
|
Reference in New Issue
Block a user