From 8dfdf3e131070a9129f9dc5cd7246e48e67b7237 Mon Sep 17 00:00:00 2001 From: Northseadl Date: Sat, 7 Jan 2023 23:31:59 +0800 Subject: [PATCH] add multipart filememe --- http/contract/dataflowInterface.go | 1 + http/dataflow/mutlipart.go | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/http/contract/dataflowInterface.go b/http/contract/dataflowInterface.go index 49bff3b..ea9dcc7 100644 --- a/http/contract/dataflowInterface.go +++ b/http/contract/dataflowInterface.go @@ -52,6 +52,7 @@ type ResponseHelper interface { type MultipartDfInterface interface { Boundary(b string) MultipartDfInterface FileByPath(fieldName string, filePath string) MultipartDfInterface + FileMem(fieldName string, fileName string, reader io.Reader) MultipartDfInterface Part(header textproto.MIMEHeader, reader io.Reader) MultipartDfInterface FieldValue(fieldName string, value string) MultipartDfInterface Field(fieldName string, reader io.Reader) MultipartDfInterface diff --git a/http/dataflow/mutlipart.go b/http/dataflow/mutlipart.go index 4b83fed..d98dee4 100644 --- a/http/dataflow/mutlipart.go +++ b/http/dataflow/mutlipart.go @@ -55,6 +55,23 @@ func (m *MultipartDf) FileByPath(fieldName string, filePath string) contract.Mul return m } +func (m *MultipartDf) FileMem(fieldName string, fileName string, reader io.Reader) contract.MultipartDfInterface { + writer, err := m.mWriter.CreateFormFile(fieldName, fileName) + if err != nil { + m.errs = append(m.errs, errors.Wrap(err, "create file part failed")) + } + var buf bytes.Buffer + _, err = buf.ReadFrom(reader) + if err != nil { + m.errs = append(m.errs, errors.Wrap(err, "create file part failed")) + } + _, err = buf.WriteTo(writer) + if err != nil { + m.errs = append(m.errs, errors.Wrap(err, "create file part failed")) + } + return m +} + func (m *MultipartDf) Part(header textproto.MIMEHeader, reader io.Reader) contract.MultipartDfInterface { writer, err := m.mWriter.CreatePart(header) if err != nil {