Skip to content

Commit

Permalink
change: ReadUnmarshalFunc func(reader io.ReadCloser, header http.Head…
Browse files Browse the repository at this point in the history
…er, v interface{}) error; add FileUnmarshaler
  • Loading branch information
Hexilee committed Oct 7, 2018
1 parent bdd49a6 commit d7524f2
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 6 deletions.
9 changes: 5 additions & 4 deletions Unmarshaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (

type (
ReadUnmarshaler interface {
Unmarshal(reader io.Reader, header http.Header, v interface{}) error
Unmarshal(reader io.ReadCloser, header http.Header, v interface{}) error
}

ReadUnmarshalFunc func(reader io.Reader, header http.Header, v interface{}) error
ReadUnmarshalFunc func(reader io.ReadCloser, header http.Header, v interface{}) error

ReaderAdapter struct {
unmarshaler Unmarshaler
Expand Down Expand Up @@ -40,13 +40,14 @@ func (fn UnmarshalFunc) Unmarshal(data []byte, v interface{}) error {
return fn(data, v)
}

func (fn ReadUnmarshalFunc) Unmarshal(reader io.Reader, header http.Header, v interface{}) error {
func (fn ReadUnmarshalFunc) Unmarshal(reader io.ReadCloser, header http.Header, v interface{}) error {
return fn(reader, header, v)
}

func (adapter *ReaderAdapter) Unmarshal(reader io.Reader, header http.Header, v interface{}) (err error) {
func (adapter *ReaderAdapter) Unmarshal(reader io.ReadCloser, header http.Header, v interface{}) (err error) {
var body []byte
body, err = ioutil.ReadAll(reader)
reader.Close()
if err == nil {
err = adapter.unmarshaler.Unmarshal(body, v)
}
Expand Down
1 change: 0 additions & 1 deletion VarsCtr.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,6 @@ func (parser *VarsParser) setContentType(contentType string) {
parser.contentType = contentType
}

// TODO: test it
// can only be called by parse()
func (parser *VarsParser) checkContentType(contentType string) (err error) {
switch contentType {
Expand Down
3 changes: 2 additions & 1 deletion builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,10 @@ func TestBuilder_AddReaderUnmarshalerFunc(t *testing.T) {

creator, err := gotten.NewBuilder().
SetBaseUrl("https://mock.io").
AddReadUnmarshalFunc(func(body io.Reader, _ http.Header, v interface{}) (err error) {
AddReadUnmarshalFunc(func(body io.ReadCloser, _ http.Header, v interface{}) (err error) {
var data []byte
data, err = ioutil.ReadAll(body)
body.Close()
if err == nil {
var success bool
success, err = strconv.ParseBool(string(data))
Expand Down
5 changes: 5 additions & 0 deletions unmarshalers/ErrorMessages.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package unmarshalers

const (
ContentDispositionOrFilenameEmpty = "Content-Disposition or filename is empty"
)
61 changes: 61 additions & 0 deletions unmarshalers/FileUnmarshaler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package unmarshalers

import (
"errors"
"github.com/Hexilee/gotten/headers"
"io"
"mime"
"net/http"
"path"
)

type (
Filename int
FileUnmarshaler struct {
basePath string // Default: wd
extensionName string // ZeroStr means keep the same extension name with Content-Disposition or Content-Type
discard bool
filename Filename // Default: ContentDisposition
}
)

const (
// Filename
ContentDisposition Filename = iota // as the same as the filename in Content-Disposition
Hash // md5
HashHeader // hash-filename
)

const (
ZeroStr = ""
)

func (unmarshaler FileUnmarshaler) Unmarshal(reader io.Reader, header http.Header, v interface{}) (err error) {
var filename string
var ext string
var hash string
contentDisposition := header.Get(headers.HeaderContentDisposition)
if contentDisposition != ZeroStr {
_, params, err := mime.ParseMediaType(contentDisposition)
if err == nil {
filename = params["filename"]
ext = path.Ext(filename)
}
}

if err == nil {
if (contentDisposition == ZeroStr || filename == ZeroStr) &&
(unmarshaler.filename == ContentDisposition || unmarshaler.filename == HashHeader) {
err = errors.New(ContentDispositionOrFilenameEmpty)
}

if err == nil {
switch unmarshaler.filename {
case ContentDisposition:
_ = ext
_ = hash
}
}
}
return
}

0 comments on commit d7524f2

Please sign in to comment.