-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
129 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package multipart | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"io" | ||
"mime/multipart" | ||
"strings" | ||
"sync" | ||
) | ||
|
||
type processor struct { | ||
*multipart.Reader | ||
valueMap map[string][]string | ||
fileMap map[string][]*FileHeader | ||
} | ||
|
||
func newProcessor(r *multipart.Reader) *processor { | ||
return &processor{ | ||
Reader: r, | ||
valueMap: make(map[string][]string), | ||
} | ||
} | ||
|
||
var bufPool = sync.Pool{ | ||
New: func() interface{} { | ||
return new(bytes.Buffer) | ||
}, | ||
} | ||
|
||
func (p *processor) readValue(name string) []string { | ||
if values, ok := p.valueMap[name]; ok { | ||
return values | ||
} | ||
|
||
part, err := p.readUntilField(name) | ||
if err != nil { | ||
return nil | ||
} | ||
defer part.Close() | ||
|
||
if part == nil { | ||
return nil | ||
} | ||
|
||
sb := strings.Builder{} | ||
if _, err := io.Copy(&sb, part); err != nil { | ||
return nil | ||
} | ||
|
||
return []string{sb.String()} | ||
} | ||
|
||
func (p *processor) readUntilField(name string) (*multipart.Part, error) { | ||
buf, ok := bufPool.Get().(*bytes.Buffer) | ||
if !ok { | ||
buf = new(bytes.Buffer) | ||
} | ||
buf.Reset() | ||
|
||
for { | ||
part, err := p.NextPart() | ||
if err != nil { | ||
if errors.Is(err, io.EOF) { | ||
return nil, nil | ||
} | ||
return nil, err | ||
} | ||
|
||
if part.FormName() == name { | ||
return part, nil | ||
} | ||
|
||
if _, err := buf.ReadFrom(part); err != nil { | ||
return nil, err | ||
} | ||
} | ||
} |
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,48 @@ | ||
package multipart | ||
|
||
import ( | ||
"io" | ||
"mime/multipart" | ||
"net/textproto" | ||
) | ||
|
||
type Reader struct { | ||
*multipart.Reader | ||
} | ||
|
||
func NewReader(r io.Reader, boundary string) *Reader { | ||
return &Reader{ | ||
Reader: multipart.NewReader(r, boundary), | ||
} | ||
} | ||
|
||
func (r *Reader) ReadForm(maxMemory int64) (*Form, error) { | ||
return &Form{ | ||
processor: newProcessor(r.Reader), | ||
}, nil | ||
} | ||
|
||
type Form struct { | ||
processor *processor | ||
} | ||
|
||
func (f *Form) Value(name string) ([]string, bool) { | ||
return f.processor.readValue(name) | ||
} | ||
|
||
func (f *Form) File(name string) []*FileHeader | ||
|
||
type FileHeader struct { | ||
Filename string | ||
Header textproto.MIMEHeader | ||
Size int64 | ||
} | ||
|
||
func (fh *FileHeader) Open() (File, error) | ||
|
||
type File interface { | ||
io.Reader | ||
io.ReaderAt | ||
io.Seeker | ||
io.Closer | ||
} |