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.

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