-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
change: ReadUnmarshalFunc func(reader io.ReadCloser, header http.Head…
…er, v interface{}) error; add FileUnmarshaler
- Loading branch information
Showing
5 changed files
with
73 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package unmarshalers | ||
|
||
const ( | ||
ContentDispositionOrFilenameEmpty = "Content-Disposition or filename is empty" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |