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.

219 lines
5.4 KiB

5 years ago
  1. package needle
  2. import (
  3. "compress/gzip"
  4. "crypto/md5"
  5. "fmt"
  6. "hash"
  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. MimeType string
  22. PairMap map[string]string
  23. IsGzipped 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.MimeType == "" {
  52. pu.MimeType = http.DetectContentType(pu.Data)
  53. // println("detected mimetype to", pu.MimeType)
  54. if pu.MimeType == "application/octet-stream" {
  55. pu.MimeType = ""
  56. }
  57. }
  58. if pu.IsGzipped {
  59. if unzipped, e := util.DecompressData(pu.Data); e == nil {
  60. pu.OriginalDataSize = len(unzipped)
  61. pu.UncompressedData = unzipped
  62. // println("ungzipped data size", len(unzipped))
  63. }
  64. } else {
  65. ext := filepath.Base(pu.FileName)
  66. if shouldGzip, iAmSure := util.IsGzippableFileType(ext, pu.MimeType); pu.MimeType == "" && !iAmSure || shouldGzip && 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. return
  78. }
  79. func parsePut(r *http.Request, sizeLimit int64, pu *ParsedUpload) (e error) {
  80. pu.IsGzipped = r.Header.Get("Content-Encoding") == "gzip"
  81. pu.MimeType = r.Header.Get("Content-Type")
  82. pu.FileName = ""
  83. pu.Data, e = ioutil.ReadAll(io.LimitReader(r.Body, sizeLimit+1))
  84. if e == io.EOF || int64(pu.OriginalDataSize) == sizeLimit+1 {
  85. io.Copy(ioutil.Discard, r.Body)
  86. }
  87. r.Body.Close()
  88. return nil
  89. }
  90. type ChecksumReader struct {
  91. h hash.Hash
  92. r io.Reader
  93. }
  94. func (cr *ChecksumReader) Read(p []byte) (int, error) {
  95. n, err := cr.r.Read(p)
  96. cr.h.Write(p[:n])
  97. return n, err
  98. }
  99. func (cr *ChecksumReader) Checksum() string {
  100. return fmt.Sprintf("%x", cr.h.Sum(nil))
  101. }
  102. func parseMultipart(r *http.Request, sizeLimit int64, pu *ParsedUpload) (e error) {
  103. defer func() {
  104. if e != nil && r.Body != nil {
  105. io.Copy(ioutil.Discard, r.Body)
  106. r.Body.Close()
  107. }
  108. }()
  109. form, fe := r.MultipartReader()
  110. if fe != nil {
  111. glog.V(0).Infoln("MultipartReader [ERROR]", fe)
  112. e = fe
  113. return
  114. }
  115. // first multi-part item
  116. part, fe := form.NextPart()
  117. if fe != nil {
  118. glog.V(0).Infoln("Reading Multi part [ERROR]", fe)
  119. e = fe
  120. return
  121. }
  122. pu.FileName = part.FileName()
  123. if pu.FileName != "" {
  124. pu.FileName = path.Base(pu.FileName)
  125. }
  126. reader := io.LimitReader(part, sizeLimit+1)
  127. if expectedChecksum := r.Header.Get("Content-MD5"); expectedChecksum != "" {
  128. if r.Header.Get("Content-Encoding") == "gzip" {
  129. gr, err := gzip.NewReader(reader)
  130. if err != nil {
  131. e = fmt.Errorf("Content-Encoding == gzip but content was not gzipped: %s", err)
  132. return
  133. }
  134. reader = gr
  135. }
  136. cr := &ChecksumReader{md5.New(), reader}
  137. pu.Data, e = ioutil.ReadAll(cr)
  138. if expectedChecksum != cr.Checksum() {
  139. e = fmt.Errorf("Content-MD5 did not match md5 of file data [%s] != [%s]", expectedChecksum, cr.Checksum())
  140. return
  141. }
  142. } else {
  143. pu.Data, e = ioutil.ReadAll(reader)
  144. }
  145. if e != nil {
  146. glog.V(0).Infoln("Reading Content [ERROR]", e)
  147. return
  148. }
  149. if len(pu.Data) == int(sizeLimit)+1 {
  150. e = fmt.Errorf("file over the limited %d bytes", sizeLimit)
  151. return
  152. }
  153. // if the filename is empty string, do a search on the other multi-part items
  154. for pu.FileName == "" {
  155. part2, fe := form.NextPart()
  156. if fe != nil {
  157. break // no more or on error, just safely break
  158. }
  159. fName := part2.FileName()
  160. // found the first <file type> multi-part has filename
  161. if fName != "" {
  162. data2, fe2 := ioutil.ReadAll(io.LimitReader(part2, sizeLimit+1))
  163. if fe2 != nil {
  164. glog.V(0).Infoln("Reading Content [ERROR]", fe2)
  165. e = fe2
  166. return
  167. }
  168. if len(data2) == int(sizeLimit)+1 {
  169. e = fmt.Errorf("file over the limited %d bytes", sizeLimit)
  170. return
  171. }
  172. // update
  173. pu.Data = data2
  174. pu.FileName = path.Base(fName)
  175. break
  176. }
  177. }
  178. pu.IsChunkedFile, _ = strconv.ParseBool(r.FormValue("cm"))
  179. if !pu.IsChunkedFile {
  180. dotIndex := strings.LastIndex(pu.FileName, ".")
  181. ext, mtype := "", ""
  182. if dotIndex > 0 {
  183. ext = strings.ToLower(pu.FileName[dotIndex:])
  184. mtype = mime.TypeByExtension(ext)
  185. }
  186. contentType := part.Header.Get("Content-Type")
  187. if contentType != "" && contentType != "application/octet-stream" && mtype != contentType {
  188. pu.MimeType = contentType // only return mime type if not deductable
  189. mtype = contentType
  190. }
  191. pu.IsGzipped = part.Header.Get("Content-Encoding") == "gzip"
  192. }
  193. return
  194. }