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.

185 lines
4.6 KiB

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