feat: hrp support uploading file

This commit is contained in:
buyuxiang
2022-06-21 16:18:38 +08:00
parent 3006b0b0bb
commit da41def86b
8 changed files with 139 additions and 66 deletions

View File

@@ -1,25 +1,34 @@
package builtin
import (
"bytes"
"crypto/md5"
"encoding/hex"
"fmt"
"math"
"math/rand"
"mime/multipart"
"os"
"path/filepath"
"time"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
)
var Functions = map[string]interface{}{
"get_timestamp": getTimestamp, // call without arguments
"sleep": sleep, // call with one argument
"gen_random_string": genRandomString, // call with one argument
"max": math.Max, // call with two arguments
"md5": MD5, // call with one argument
"parameterize": loadFromCSV,
"P": loadFromCSV,
"environ": os.Getenv,
"ENV": os.Getenv,
"load_ws_message": loadMessage,
"get_timestamp": getTimestamp, // call without arguments
"sleep": sleep, // call with one argument
"gen_random_string": genRandomString, // call with one argument
"max": math.Max, // call with two arguments
"md5": MD5, // call with one argument
"parameterize": loadFromCSV,
"P": loadFromCSV,
"environ": os.Getenv,
"ENV": os.Getenv,
"load_ws_message": loadMessage,
"multipart_encoder": multipartEncoder,
"multipart_content_type": multipartContentType,
}
func init() {
@@ -50,3 +59,60 @@ func MD5(str string) string {
hasher.Write([]byte(str))
return hex.EncodeToString(hasher.Sum(nil))
}
type TFormWriter struct {
Writer *multipart.Writer
Payload *bytes.Buffer
}
func multipartEncoder(formMap map[string]interface{}) *TFormWriter {
payload := &bytes.Buffer{}
writer := multipart.NewWriter(payload)
for formKey, formValue := range formMap {
formValueString := fmt.Sprintf("%v", formValue)
if err := writeFormDataFile(writer, formKey, formValueString); err == nil {
// form value is a file path
continue
}
// form value is not a file path, write as raw string
if err := writer.WriteField(formKey, formValueString); err != nil {
log.Info().Err(err).Msgf("failed to write field: %v=%v, ignore", formKey, formValue)
}
}
if err := writer.Close(); err != nil {
}
return &TFormWriter{
Writer: writer,
Payload: payload,
}
}
func writeFormDataFile(writer *multipart.Writer, fName, fPath string) error {
var err error
fPath, err = filepath.Abs(fPath)
if err != nil {
log.Error().Err(err).Str("path", fPath).Msg("convert absolute path failed")
return err
}
if !IsFilePathExists(fPath) {
return errors.Errorf("file %v not existed", fPath)
}
file, err := os.ReadFile(fPath)
if err != nil {
log.Error().Err(err).Str("path", fPath).Msg("read file failed")
return err
}
formFile, err := writer.CreateFormFile(fName, filepath.Base(fPath))
if err != nil {
return err
}
_, err = formFile.Write(file)
return err
}
func multipartContentType(w *TFormWriter) string {
if w.Writer == nil {
return ""
}
return w.Writer.FormDataContentType()
}