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.

272 lines
8.4 KiB

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