needle_parse_upload.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 false {
  72. if shouldBeCompressed, iAmSure := util.IsCompressableFileType(ext, mimeType); mimeType == "" && !iAmSure || shouldBeCompressed && iAmSure {
  73. // println("ext", ext, "iAmSure", iAmSure, "shouldBeCompressed", shouldBeCompressed, "mimeType", pu.MimeType)
  74. if compressedData, err := util.GzipData(pu.Data); err == nil {
  75. if len(compressedData)*10 < len(pu.Data)*9 {
  76. pu.Data = compressedData
  77. pu.IsGzipped = true
  78. }
  79. // println("gzipped data size", len(compressedData))
  80. }
  81. }
  82. }
  83. }
  84. // md5
  85. h := md5.New()
  86. h.Write(pu.UncompressedData)
  87. pu.ContentMd5 = base64.StdEncoding.EncodeToString(h.Sum(nil))
  88. if expectedChecksum := r.Header.Get("Content-MD5"); expectedChecksum != "" {
  89. if expectedChecksum != pu.ContentMd5 {
  90. e = fmt.Errorf("Content-MD5 did not match md5 of file data expected [%s] received [%s] size %d", expectedChecksum, pu.ContentMd5, len(pu.UncompressedData))
  91. return
  92. }
  93. }
  94. return
  95. }
  96. func parsePut(r *http.Request, sizeLimit int64, pu *ParsedUpload) error {
  97. pu.IsGzipped = r.Header.Get("Content-Encoding") == "gzip"
  98. // pu.IsZstd = r.Header.Get("Content-Encoding") == "zstd"
  99. pu.MimeType = r.Header.Get("Content-Type")
  100. pu.FileName = ""
  101. dataSize, err := pu.bytesBuffer.ReadFrom(io.LimitReader(r.Body, sizeLimit+1))
  102. if err == io.EOF || dataSize == sizeLimit+1 {
  103. io.Copy(ioutil.Discard, r.Body)
  104. }
  105. pu.Data = pu.bytesBuffer.Bytes()
  106. r.Body.Close()
  107. return nil
  108. }
  109. func parseMultipart(r *http.Request, sizeLimit int64, pu *ParsedUpload) (e error) {
  110. defer func() {
  111. if e != nil && r.Body != nil {
  112. io.Copy(ioutil.Discard, r.Body)
  113. r.Body.Close()
  114. }
  115. }()
  116. form, fe := r.MultipartReader()
  117. if fe != nil {
  118. glog.V(0).Infoln("MultipartReader [ERROR]", fe)
  119. e = fe
  120. return
  121. }
  122. // first multi-part item
  123. part, fe := form.NextPart()
  124. if fe != nil {
  125. glog.V(0).Infoln("Reading Multi part [ERROR]", fe)
  126. e = fe
  127. return
  128. }
  129. pu.FileName = part.FileName()
  130. if pu.FileName != "" {
  131. pu.FileName = path.Base(pu.FileName)
  132. }
  133. var dataSize int64
  134. dataSize, e = pu.bytesBuffer.ReadFrom(io.LimitReader(part, sizeLimit+1))
  135. if e != nil {
  136. glog.V(0).Infoln("Reading Content [ERROR]", e)
  137. return
  138. }
  139. if dataSize == sizeLimit+1 {
  140. e = fmt.Errorf("file over the limited %d bytes", sizeLimit)
  141. return
  142. }
  143. pu.Data = pu.bytesBuffer.Bytes()
  144. // if the filename is empty string, do a search on the other multi-part items
  145. for pu.FileName == "" {
  146. part2, fe := form.NextPart()
  147. if fe != nil {
  148. break // no more or on error, just safely break
  149. }
  150. fName := part2.FileName()
  151. // found the first <file type> multi-part has filename
  152. if fName != "" {
  153. pu.bytesBuffer.Reset()
  154. dataSize2, fe2 := pu.bytesBuffer.ReadFrom(io.LimitReader(part2, sizeLimit+1))
  155. if fe2 != nil {
  156. glog.V(0).Infoln("Reading Content [ERROR]", fe2)
  157. e = fe2
  158. return
  159. }
  160. if dataSize2 == sizeLimit+1 {
  161. e = fmt.Errorf("file over the limited %d bytes", sizeLimit)
  162. return
  163. }
  164. // update
  165. pu.Data = pu.bytesBuffer.Bytes()
  166. pu.FileName = path.Base(fName)
  167. break
  168. }
  169. }
  170. pu.IsChunkedFile, _ = strconv.ParseBool(r.FormValue("cm"))
  171. if !pu.IsChunkedFile {
  172. dotIndex := strings.LastIndex(pu.FileName, ".")
  173. ext, mtype := "", ""
  174. if dotIndex > 0 {
  175. ext = strings.ToLower(pu.FileName[dotIndex:])
  176. mtype = mime.TypeByExtension(ext)
  177. }
  178. contentType := part.Header.Get("Content-Type")
  179. if contentType != "" && contentType != "application/octet-stream" && mtype != contentType {
  180. pu.MimeType = contentType // only return mime type if not deductable
  181. mtype = contentType
  182. }
  183. }
  184. pu.IsGzipped = part.Header.Get("Content-Encoding") == "gzip"
  185. // pu.IsZstd = part.Header.Get("Content-Encoding") == "zstd"
  186. return
  187. }