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.

277 lines
8.6 KiB

6 years ago
4 years ago
4 years ago
5 years ago
5 years ago
4 years ago
4 years ago
5 years ago
5 years ago
12 years ago
  1. package operation
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "io/ioutil"
  8. "mime"
  9. "mime/multipart"
  10. "net/http"
  11. "net/textproto"
  12. "path/filepath"
  13. "runtime/debug"
  14. "strings"
  15. "time"
  16. "github.com/chrislusf/seaweedfs/weed/glog"
  17. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  18. "github.com/chrislusf/seaweedfs/weed/security"
  19. "github.com/chrislusf/seaweedfs/weed/util"
  20. "github.com/valyala/bytebufferpool"
  21. )
  22. type UploadResult struct {
  23. Name string `json:"name,omitempty"`
  24. Size uint32 `json:"size,omitempty"`
  25. Error string `json:"error,omitempty"`
  26. ETag string `json:"eTag,omitempty"`
  27. CipherKey []byte `json:"cipherKey,omitempty"`
  28. Mime string `json:"mime,omitempty"`
  29. Gzip uint32 `json:"gzip,omitempty"`
  30. ContentMd5 string `json:"contentMd5,omitempty"`
  31. RetryCount int `json:"-"`
  32. }
  33. func (uploadResult *UploadResult) ToPbFileChunk(fileId string, offset int64) *filer_pb.FileChunk {
  34. fid, _ := filer_pb.ToFileIdObject(fileId)
  35. return &filer_pb.FileChunk{
  36. FileId: fileId,
  37. Offset: offset,
  38. Size: uint64(uploadResult.Size),
  39. Mtime: time.Now().UnixNano(),
  40. ETag: uploadResult.ETag,
  41. CipherKey: uploadResult.CipherKey,
  42. IsCompressed: uploadResult.Gzip > 0,
  43. Fid: fid,
  44. }
  45. }
  46. // HTTPClient interface for testing
  47. type HTTPClient interface {
  48. Do(req *http.Request) (*http.Response, error)
  49. }
  50. var (
  51. HttpClient HTTPClient
  52. )
  53. func init() {
  54. HttpClient = &http.Client{Transport: &http.Transport{
  55. MaxIdleConns: 1024,
  56. MaxIdleConnsPerHost: 1024,
  57. }}
  58. }
  59. var fileNameEscaper = strings.NewReplacer(`\`, `\\`, `"`, `\"`)
  60. // Upload sends a POST request to a volume server to upload the content with adjustable compression level
  61. func UploadData(uploadUrl string, filename string, cipher bool, data []byte, isInputCompressed bool, mtype string, pairMap map[string]string, jwt security.EncodedJwt) (uploadResult *UploadResult, err error) {
  62. uploadResult, err = retriedUploadData(uploadUrl, filename, cipher, data, isInputCompressed, mtype, pairMap, jwt)
  63. return
  64. }
  65. // Upload sends a POST request to a volume server to upload the content with fast compression
  66. func Upload(uploadUrl string, filename string, cipher bool, reader io.Reader, isInputCompressed bool, mtype string, pairMap map[string]string, jwt security.EncodedJwt) (uploadResult *UploadResult, err error, data []byte) {
  67. uploadResult, err, data = doUpload(uploadUrl, filename, cipher, reader, isInputCompressed, mtype, pairMap, jwt)
  68. return
  69. }
  70. func doUpload(uploadUrl string, filename string, cipher bool, reader io.Reader, isInputCompressed bool, mtype string, pairMap map[string]string, jwt security.EncodedJwt) (uploadResult *UploadResult, err error, data []byte) {
  71. bytesReader, ok := reader.(*util.BytesReader)
  72. if ok {
  73. data = bytesReader.Bytes
  74. } else {
  75. data, err = ioutil.ReadAll(reader)
  76. if err != nil {
  77. err = fmt.Errorf("read input: %v", err)
  78. return
  79. }
  80. }
  81. uploadResult, uploadErr := retriedUploadData(uploadUrl, filename, cipher, data, isInputCompressed, mtype, pairMap, jwt)
  82. return uploadResult, uploadErr, data
  83. }
  84. func retriedUploadData(uploadUrl string, filename string, cipher bool, data []byte, isInputCompressed bool, mtype string, pairMap map[string]string, jwt security.EncodedJwt) (uploadResult *UploadResult, err error) {
  85. for i := 0; i < 3; i++ {
  86. uploadResult, err = doUploadData(uploadUrl, filename, cipher, data, isInputCompressed, mtype, pairMap, jwt)
  87. if err == nil {
  88. uploadResult.RetryCount = i
  89. return
  90. } else {
  91. glog.Warningf("uploading to %s: %v", uploadUrl, err)
  92. }
  93. time.Sleep(time.Millisecond * time.Duration(237*(i+1)))
  94. }
  95. return
  96. }
  97. func doUploadData(uploadUrl string, filename string, cipher bool, data []byte, isInputCompressed bool, mtype string, pairMap map[string]string, jwt security.EncodedJwt) (uploadResult *UploadResult, err error) {
  98. contentIsGzipped := isInputCompressed
  99. shouldGzipNow := false
  100. if !isInputCompressed {
  101. if mtype == "" {
  102. mtype = http.DetectContentType(data)
  103. // println("detect1 mimetype to", mtype)
  104. if mtype == "application/octet-stream" {
  105. mtype = ""
  106. }
  107. }
  108. if shouldBeCompressed, iAmSure := util.IsCompressableFileType(filepath.Base(filename), mtype); iAmSure && shouldBeCompressed {
  109. shouldGzipNow = true
  110. } else if !iAmSure && mtype == "" && len(data) > 16*1024 {
  111. var compressed []byte
  112. compressed, err = util.GzipData(data[0:128])
  113. shouldGzipNow = len(compressed)*10 < 128*9 // can not compress to less than 90%
  114. }
  115. }
  116. var clearDataLen int
  117. // gzip if possible
  118. // this could be double copying
  119. clearDataLen = len(data)
  120. clearData := data
  121. if shouldGzipNow && !cipher {
  122. compressed, compressErr := util.GzipData(data)
  123. // fmt.Printf("data is compressed from %d ==> %d\n", len(data), len(compressed))
  124. if compressErr == nil {
  125. data = compressed
  126. contentIsGzipped = true
  127. }
  128. } else if isInputCompressed {
  129. // just to get the clear data length
  130. clearData, err = util.DecompressData(data)
  131. if err == nil {
  132. clearDataLen = len(clearData)
  133. }
  134. }
  135. if cipher {
  136. // encrypt(gzip(data))
  137. // encrypt
  138. cipherKey := util.GenCipherKey()
  139. encryptedData, encryptionErr := util.Encrypt(clearData, cipherKey)
  140. if encryptionErr != nil {
  141. err = fmt.Errorf("encrypt input: %v", encryptionErr)
  142. return
  143. }
  144. // upload data
  145. uploadResult, err = upload_content(uploadUrl, func(w io.Writer) (err error) {
  146. _, err = w.Write(encryptedData)
  147. return
  148. }, "", false, len(encryptedData), "", nil, jwt)
  149. if uploadResult == nil {
  150. return
  151. }
  152. uploadResult.Name = filename
  153. uploadResult.Mime = mtype
  154. uploadResult.CipherKey = cipherKey
  155. uploadResult.Size = uint32(clearDataLen)
  156. } else {
  157. // upload data
  158. uploadResult, err = upload_content(uploadUrl, func(w io.Writer) (err error) {
  159. _, err = w.Write(data)
  160. return
  161. }, filename, contentIsGzipped, len(data), mtype, pairMap, jwt)
  162. if uploadResult == nil {
  163. return
  164. }
  165. uploadResult.Size = uint32(clearDataLen)
  166. if contentIsGzipped {
  167. uploadResult.Gzip = 1
  168. }
  169. }
  170. return uploadResult, err
  171. }
  172. func upload_content(uploadUrl string, fillBufferFunction func(w io.Writer) error, filename string, isGzipped bool, originalDataSize int, mtype string, pairMap map[string]string, jwt security.EncodedJwt) (*UploadResult, error) {
  173. buf := bytebufferpool.Get()
  174. defer bytebufferpool.Put(buf)
  175. body_writer := multipart.NewWriter(buf)
  176. h := make(textproto.MIMEHeader)
  177. h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="file"; filename="%s"`, fileNameEscaper.Replace(filename)))
  178. h.Set("Idempotency-Key", uploadUrl)
  179. if mtype == "" {
  180. mtype = mime.TypeByExtension(strings.ToLower(filepath.Ext(filename)))
  181. }
  182. if mtype != "" {
  183. h.Set("Content-Type", mtype)
  184. }
  185. if isGzipped {
  186. h.Set("Content-Encoding", "gzip")
  187. }
  188. file_writer, cp_err := body_writer.CreatePart(h)
  189. if cp_err != nil {
  190. glog.V(0).Infoln("error creating form file", cp_err.Error())
  191. return nil, cp_err
  192. }
  193. if err := fillBufferFunction(file_writer); err != nil {
  194. glog.V(0).Infoln("error copying data", err)
  195. return nil, err
  196. }
  197. content_type := body_writer.FormDataContentType()
  198. if err := body_writer.Close(); err != nil {
  199. glog.V(0).Infoln("error closing body", err)
  200. return nil, err
  201. }
  202. req, postErr := http.NewRequest("POST", uploadUrl, bytes.NewReader(buf.Bytes()))
  203. if postErr != nil {
  204. glog.V(1).Infof("create upload request %s: %v", uploadUrl, postErr)
  205. return nil, fmt.Errorf("create upload request %s: %v", uploadUrl, postErr)
  206. }
  207. req.Header.Set("Content-Type", content_type)
  208. for k, v := range pairMap {
  209. req.Header.Set(k, v)
  210. }
  211. if jwt != "" {
  212. req.Header.Set("Authorization", "BEARER "+string(jwt))
  213. }
  214. // print("+")
  215. resp, post_err := HttpClient.Do(req)
  216. if post_err != nil {
  217. glog.Errorf("upload %s %d bytes to %v: %v", filename, originalDataSize, uploadUrl, post_err)
  218. debug.PrintStack()
  219. return nil, fmt.Errorf("upload %s %d bytes to %v: %v", filename, originalDataSize, uploadUrl, post_err)
  220. }
  221. // print("-")
  222. defer util.CloseResponse(resp)
  223. var ret UploadResult
  224. etag := getEtag(resp)
  225. if resp.StatusCode == http.StatusNoContent {
  226. ret.ETag = etag
  227. return &ret, nil
  228. }
  229. resp_body, ra_err := ioutil.ReadAll(resp.Body)
  230. if ra_err != nil {
  231. return nil, fmt.Errorf("read response body %v: %v", uploadUrl, ra_err)
  232. }
  233. unmarshal_err := json.Unmarshal(resp_body, &ret)
  234. if unmarshal_err != nil {
  235. glog.Errorf("unmarshal %s: %v", uploadUrl, string(resp_body))
  236. return nil, fmt.Errorf("unmarshal %v: %v", uploadUrl, unmarshal_err)
  237. }
  238. if ret.Error != "" {
  239. return nil, fmt.Errorf("unmarshalled error %v: %v", uploadUrl, ret.Error)
  240. }
  241. ret.ETag = etag
  242. ret.ContentMd5 = resp.Header.Get("Content-MD5")
  243. return &ret, nil
  244. }
  245. func getEtag(r *http.Response) (etag string) {
  246. etag = r.Header.Get("ETag")
  247. if strings.HasPrefix(etag, "\"") && strings.HasSuffix(etag, "\"") {
  248. etag = etag[1 : len(etag)-1]
  249. }
  250. return
  251. }