You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
3.3 KiB

10 years ago
12 years ago
  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. ETag string `json:"error,omitempty"`
  23. }
  24. var (
  25. client *http.Client
  26. )
  27. func init() {
  28. client = &http.Client{Transport: &http.Transport{
  29. MaxIdleConnsPerHost: 1024,
  30. }}
  31. }
  32. var fileNameEscaper = strings.NewReplacer("\\", "\\\\", "\"", "\\\"")
  33. // Upload sends a POST request to a volume server to upload the content
  34. func Upload(uploadUrl string, filename string, reader io.Reader, isGzipped bool, mtype string, pairMap map[string]string, jwt security.EncodedJwt) (*UploadResult, error) {
  35. return upload_content(uploadUrl, func(w io.Writer) (err error) {
  36. _, err = io.Copy(w, reader)
  37. return
  38. }, filename, isGzipped, mtype, pairMap, jwt)
  39. }
  40. 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) {
  41. body_buf := bytes.NewBufferString("")
  42. body_writer := multipart.NewWriter(body_buf)
  43. h := make(textproto.MIMEHeader)
  44. h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="file"; filename="%s"`, fileNameEscaper.Replace(filename)))
  45. if mtype == "" {
  46. mtype = mime.TypeByExtension(strings.ToLower(filepath.Ext(filename)))
  47. }
  48. if mtype != "" {
  49. h.Set("Content-Type", mtype)
  50. }
  51. if isGzipped {
  52. h.Set("Content-Encoding", "gzip")
  53. }
  54. if jwt != "" {
  55. h.Set("Authorization", "BEARER "+string(jwt))
  56. }
  57. file_writer, cp_err := body_writer.CreatePart(h)
  58. if cp_err != nil {
  59. glog.V(0).Infoln("error creating form file", cp_err.Error())
  60. return nil, cp_err
  61. }
  62. if err := fillBufferFunction(file_writer); err != nil {
  63. glog.V(0).Infoln("error copying data", err)
  64. return nil, err
  65. }
  66. content_type := body_writer.FormDataContentType()
  67. if err := body_writer.Close(); err != nil {
  68. glog.V(0).Infoln("error closing body", err)
  69. return nil, err
  70. }
  71. req, postErr := http.NewRequest("POST", uploadUrl, body_buf)
  72. if postErr != nil {
  73. glog.V(0).Infoln("failing to upload to", uploadUrl, postErr.Error())
  74. return nil, postErr
  75. }
  76. req.Header.Set("Content-Type", content_type)
  77. for k, v := range pairMap {
  78. req.Header.Set(k, v)
  79. }
  80. resp, post_err := client.Do(req)
  81. if post_err != nil {
  82. glog.V(0).Infoln("failing to upload to", uploadUrl, post_err.Error())
  83. return nil, post_err
  84. }
  85. defer resp.Body.Close()
  86. if resp.StatusCode < http.StatusOK ||
  87. resp.StatusCode > http.StatusIMUsed {
  88. return nil, errors.New(http.StatusText(resp.StatusCode))
  89. }
  90. etag := getEtag(resp)
  91. resp_body, ra_err := ioutil.ReadAll(resp.Body)
  92. if ra_err != nil {
  93. return nil, ra_err
  94. }
  95. var ret UploadResult
  96. unmarshal_err := json.Unmarshal(resp_body, &ret)
  97. if unmarshal_err != nil {
  98. glog.V(0).Infoln("failing to read upload response", uploadUrl, string(resp_body))
  99. return nil, unmarshal_err
  100. }
  101. if ret.Error != "" {
  102. return nil, errors.New(ret.Error)
  103. }
  104. ret.ETag = etag
  105. return &ret, nil
  106. }
  107. func getEtag(r *http.Response) (etag string) {
  108. etag = r.Header.Get("ETag")
  109. if strings.HasPrefix(etag, "\"") && strings.HasSuffix(etag, "\"") {
  110. etag = etag[1 : len(etag)-1]
  111. }
  112. return
  113. }