upload_content.go 8.7 KB

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