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.

208 lines
5.3 KiB

5 years ago
  1. package needle
  2. import (
  3. "bytes"
  4. "crypto/md5"
  5. "encoding/base64"
  6. "fmt"
  7. "io"
  8. "io/ioutil"
  9. "mime"
  10. "net/http"
  11. "path"
  12. "path/filepath"
  13. "strconv"
  14. "strings"
  15. "github.com/chrislusf/seaweedfs/weed/glog"
  16. "github.com/chrislusf/seaweedfs/weed/util"
  17. )
  18. type ParsedUpload struct {
  19. FileName string
  20. Data []byte
  21. bytesBuffer *bytes.Buffer
  22. MimeType string
  23. PairMap map[string]string
  24. IsGzipped bool
  25. // IsZstd bool
  26. OriginalDataSize int
  27. ModifiedTime uint64
  28. Ttl *TTL
  29. IsChunkedFile bool
  30. UncompressedData []byte
  31. ContentMd5 string
  32. }
  33. func ParseUpload(r *http.Request, sizeLimit int64, bytesBuffer *bytes.Buffer) (pu *ParsedUpload, e error) {
  34. bytesBuffer.Reset()
  35. pu = &ParsedUpload{bytesBuffer: bytesBuffer}
  36. pu.PairMap = make(map[string]string)
  37. for k, v := range r.Header {
  38. if len(v) > 0 && strings.HasPrefix(k, PairNamePrefix) {
  39. pu.PairMap[k] = v[0]
  40. }
  41. }
  42. if r.Method == "POST" {
  43. e = parseMultipart(r, sizeLimit, pu)
  44. } else {
  45. e = parsePut(r, sizeLimit, pu)
  46. }
  47. if e != nil {
  48. return
  49. }
  50. pu.ModifiedTime, _ = strconv.ParseUint(r.FormValue("ts"), 10, 64)
  51. pu.Ttl, _ = ReadTTL(r.FormValue("ttl"))
  52. pu.OriginalDataSize = len(pu.Data)
  53. pu.UncompressedData = pu.Data
  54. // println("received data", len(pu.Data), "isGzipped", pu.IsGzipped, "mime", pu.MimeType, "name", pu.FileName)
  55. if pu.IsGzipped {
  56. if unzipped, e := util.DecompressData(pu.Data); e == nil {
  57. pu.OriginalDataSize = len(unzipped)
  58. pu.UncompressedData = unzipped
  59. // println("ungzipped data size", len(unzipped))
  60. }
  61. } else {
  62. ext := filepath.Base(pu.FileName)
  63. mimeType := pu.MimeType
  64. if mimeType == "" {
  65. mimeType = http.DetectContentType(pu.Data)
  66. }
  67. // println("detected mimetype to", pu.MimeType)
  68. if mimeType == "application/octet-stream" {
  69. mimeType = ""
  70. }
  71. if shouldBeCompressed, iAmSure := util.IsCompressableFileType(ext, mimeType); shouldBeCompressed && iAmSure {
  72. // println("ext", ext, "iAmSure", iAmSure, "shouldBeCompressed", shouldBeCompressed, "mimeType", pu.MimeType)
  73. if compressedData, err := util.GzipData(pu.Data); err == nil {
  74. if len(compressedData)*10 < len(pu.Data)*9 {
  75. pu.Data = compressedData
  76. pu.IsGzipped = true
  77. }
  78. // println("gzipped data size", len(compressedData))
  79. }
  80. }
  81. }
  82. // md5
  83. h := md5.New()
  84. h.Write(pu.UncompressedData)
  85. pu.ContentMd5 = base64.StdEncoding.EncodeToString(h.Sum(nil))
  86. if expectedChecksum := r.Header.Get("Content-MD5"); expectedChecksum != "" {
  87. if expectedChecksum != pu.ContentMd5 {
  88. e = fmt.Errorf("Content-MD5 did not match md5 of file data expected [%s] received [%s] size %d", expectedChecksum, pu.ContentMd5, len(pu.UncompressedData))
  89. return
  90. }
  91. }
  92. return
  93. }
  94. func parsePut(r *http.Request, sizeLimit int64, pu *ParsedUpload) error {
  95. pu.IsGzipped = r.Header.Get("Content-Encoding") == "gzip"
  96. // pu.IsZstd = r.Header.Get("Content-Encoding") == "zstd"
  97. pu.MimeType = r.Header.Get("Content-Type")
  98. pu.FileName = ""
  99. dataSize, err := pu.bytesBuffer.ReadFrom(io.LimitReader(r.Body, sizeLimit+1))
  100. if err == io.EOF || dataSize == sizeLimit+1 {
  101. io.Copy(ioutil.Discard, r.Body)
  102. }
  103. pu.Data = pu.bytesBuffer.Bytes()
  104. r.Body.Close()
  105. return nil
  106. }
  107. func parseMultipart(r *http.Request, sizeLimit int64, pu *ParsedUpload) (e error) {
  108. defer func() {
  109. if e != nil && r.Body != nil {
  110. io.Copy(ioutil.Discard, r.Body)
  111. r.Body.Close()
  112. }
  113. }()
  114. form, fe := r.MultipartReader()
  115. if fe != nil {
  116. glog.V(0).Infoln("MultipartReader [ERROR]", fe)
  117. e = fe
  118. return
  119. }
  120. // first multi-part item
  121. part, fe := form.NextPart()
  122. if fe != nil {
  123. glog.V(0).Infoln("Reading Multi part [ERROR]", fe)
  124. e = fe
  125. return
  126. }
  127. pu.FileName = part.FileName()
  128. if pu.FileName != "" {
  129. pu.FileName = path.Base(pu.FileName)
  130. }
  131. var dataSize int64
  132. dataSize, e = pu.bytesBuffer.ReadFrom(io.LimitReader(part, sizeLimit+1))
  133. if e != nil {
  134. glog.V(0).Infoln("Reading Content [ERROR]", e)
  135. return
  136. }
  137. if dataSize == sizeLimit+1 {
  138. e = fmt.Errorf("file over the limited %d bytes", sizeLimit)
  139. return
  140. }
  141. pu.Data = pu.bytesBuffer.Bytes()
  142. // if the filename is empty string, do a search on the other multi-part items
  143. for pu.FileName == "" {
  144. part2, fe := form.NextPart()
  145. if fe != nil {
  146. break // no more or on error, just safely break
  147. }
  148. fName := part2.FileName()
  149. // found the first <file type> multi-part has filename
  150. if fName != "" {
  151. pu.bytesBuffer.Reset()
  152. dataSize2, fe2 := pu.bytesBuffer.ReadFrom(io.LimitReader(part2, sizeLimit+1))
  153. if fe2 != nil {
  154. glog.V(0).Infoln("Reading Content [ERROR]", fe2)
  155. e = fe2
  156. return
  157. }
  158. if dataSize2 == sizeLimit+1 {
  159. e = fmt.Errorf("file over the limited %d bytes", sizeLimit)
  160. return
  161. }
  162. // update
  163. pu.Data = pu.bytesBuffer.Bytes()
  164. pu.FileName = path.Base(fName)
  165. break
  166. }
  167. }
  168. pu.IsChunkedFile, _ = strconv.ParseBool(r.FormValue("cm"))
  169. if !pu.IsChunkedFile {
  170. dotIndex := strings.LastIndex(pu.FileName, ".")
  171. ext, mtype := "", ""
  172. if dotIndex > 0 {
  173. ext = strings.ToLower(pu.FileName[dotIndex:])
  174. mtype = mime.TypeByExtension(ext)
  175. }
  176. contentType := part.Header.Get("Content-Type")
  177. if contentType != "" && contentType != "application/octet-stream" && mtype != contentType {
  178. pu.MimeType = contentType // only return mime type if not deductable
  179. mtype = contentType
  180. }
  181. }
  182. pu.IsGzipped = part.Header.Get("Content-Encoding") == "gzip"
  183. // pu.IsZstd = part.Header.Get("Content-Encoding") == "zstd"
  184. return
  185. }