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.

197 lines
5.0 KiB

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