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.

312 lines
9.0 KiB

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