upload_content.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package operation
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "io/ioutil"
  9. "mime"
  10. "mime/multipart"
  11. "net/http"
  12. "net/textproto"
  13. "path/filepath"
  14. "strings"
  15. "github.com/chrislusf/seaweedfs/weed/glog"
  16. "github.com/chrislusf/seaweedfs/weed/security"
  17. )
  18. type UploadResult struct {
  19. Name string `json:"name,omitempty"`
  20. Size uint32 `json:"size,omitempty"`
  21. Error string `json:"error,omitempty"`
  22. }
  23. var (
  24. client *http.Client
  25. )
  26. func init() {
  27. client = &http.Client{Transport: &http.Transport{
  28. MaxIdleConnsPerHost: 1024,
  29. }}
  30. }
  31. var fileNameEscaper = strings.NewReplacer("\\", "\\\\", "\"", "\\\"")
  32. func Upload(uploadUrl string, filename string, reader io.Reader, isGzipped bool, mtype string, pairMap map[string]string, jwt security.EncodedJwt) (*UploadResult, error) {
  33. return upload_content(uploadUrl, func(w io.Writer) (err error) {
  34. _, err = io.Copy(w, reader)
  35. return
  36. }, filename, isGzipped, mtype, pairMap, jwt)
  37. }
  38. func upload_content(uploadUrl string, fillBufferFunction func(w io.Writer) error, filename string, isGzipped bool, mtype string, pairMap map[string]string, jwt security.EncodedJwt) (*UploadResult, error) {
  39. body_buf := bytes.NewBufferString("")
  40. body_writer := multipart.NewWriter(body_buf)
  41. h := make(textproto.MIMEHeader)
  42. h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="file"; filename="%s"`, fileNameEscaper.Replace(filename)))
  43. if mtype == "" {
  44. mtype = mime.TypeByExtension(strings.ToLower(filepath.Ext(filename)))
  45. }
  46. if mtype != "" {
  47. h.Set("Content-Type", mtype)
  48. }
  49. if isGzipped {
  50. h.Set("Content-Encoding", "gzip")
  51. }
  52. if jwt != "" {
  53. h.Set("Authorization", "BEARER "+string(jwt))
  54. }
  55. file_writer, cp_err := body_writer.CreatePart(h)
  56. if cp_err != nil {
  57. glog.V(0).Infoln("error creating form file", cp_err.Error())
  58. return nil, cp_err
  59. }
  60. if err := fillBufferFunction(file_writer); err != nil {
  61. glog.V(0).Infoln("error copying data", err)
  62. return nil, err
  63. }
  64. content_type := body_writer.FormDataContentType()
  65. if err := body_writer.Close(); err != nil {
  66. glog.V(0).Infoln("error closing body", err)
  67. return nil, err
  68. }
  69. req, postErr := http.NewRequest("POST", uploadUrl, body_buf)
  70. if postErr != nil {
  71. glog.V(0).Infoln("failing to upload to", uploadUrl, postErr.Error())
  72. return nil, postErr
  73. }
  74. req.Header.Set("Content-Type", content_type)
  75. for k, v := range pairMap {
  76. req.Header.Set(k, v)
  77. }
  78. resp, post_err := client.Do(req)
  79. if post_err != nil {
  80. glog.V(0).Infoln("failing to upload to", uploadUrl, post_err.Error())
  81. return nil, post_err
  82. }
  83. defer resp.Body.Close()
  84. resp_body, ra_err := ioutil.ReadAll(resp.Body)
  85. if ra_err != nil {
  86. return nil, ra_err
  87. }
  88. var ret UploadResult
  89. unmarshal_err := json.Unmarshal(resp_body, &ret)
  90. if unmarshal_err != nil {
  91. glog.V(0).Infoln("failing to read upload response", uploadUrl, string(resp_body))
  92. return nil, unmarshal_err
  93. }
  94. if ret.Error != "" {
  95. return nil, errors.New(ret.Error)
  96. }
  97. return &ret, nil
  98. }