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.

253 lines
7.5 KiB

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