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.

198 lines
5.0 KiB

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