needle.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. package storage
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "math"
  7. "mime"
  8. "net/http"
  9. "path"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "github.com/chrislusf/seaweedfs/weed/glog"
  14. "github.com/chrislusf/seaweedfs/weed/images"
  15. "github.com/chrislusf/seaweedfs/weed/operation"
  16. )
  17. const (
  18. NeedleHeaderSize = 16 //should never change this
  19. NeedlePaddingSize = 8
  20. NeedleChecksumSize = 4
  21. MaxPossibleVolumeSize = 4 * 1024 * 1024 * 1024 * 8
  22. TombstoneFileSize = math.MaxUint32
  23. PairNamePrefix = "Seaweed-"
  24. )
  25. /*
  26. * A Needle means a uploaded and stored file.
  27. * Needle file size is limited to 4GB for now.
  28. */
  29. type Needle struct {
  30. Cookie uint32 `comment:"random number to mitigate brute force lookups"`
  31. Id uint64 `comment:"needle id"`
  32. Size uint32 `comment:"sum of DataSize,Data,NameSize,Name,MimeSize,Mime"`
  33. DataSize uint32 `comment:"Data size"` //version2
  34. Data []byte `comment:"The actual file data"`
  35. Flags byte `comment:"boolean flags"` //version2
  36. NameSize uint8 //version2
  37. Name []byte `comment:"maximum 256 characters"` //version2
  38. MimeSize uint8 //version2
  39. Mime []byte `comment:"maximum 256 characters"` //version2
  40. PairsSize uint16 //version2
  41. Pairs []byte `comment:"additional name value pairs, json format, maximum 64kB"`
  42. LastModified uint64 //only store LastModifiedBytesLength bytes, which is 5 bytes to disk
  43. Ttl *TTL
  44. Checksum CRC `comment:"CRC32 to check integrity"`
  45. Padding []byte `comment:"Aligned to 8 bytes"`
  46. }
  47. func (n *Needle) String() (str string) {
  48. str = fmt.Sprintf("Cookie:%d, Id:%d, Size:%d, DataSize:%d, Name: %s, Mime: %s", n.Cookie, n.Id, n.Size, n.DataSize, n.Name, n.Mime)
  49. return
  50. }
  51. func ParseUpload(r *http.Request) (
  52. fileName string, data []byte, mimeType string, pairMap map[string]string, isGzipped bool,
  53. modifiedTime uint64, ttl *TTL, isChunkedFile bool, e error) {
  54. pairMap = make(map[string]string)
  55. for k, v := range r.Header {
  56. if len(v) > 0 && strings.HasPrefix(k, PairNamePrefix) {
  57. pairMap[k] = v[0]
  58. }
  59. }
  60. form, fe := r.MultipartReader()
  61. if fe != nil {
  62. glog.V(0).Infoln("MultipartReader [ERROR]", fe)
  63. e = fe
  64. return
  65. }
  66. //first multi-part item
  67. part, fe := form.NextPart()
  68. if fe != nil {
  69. glog.V(0).Infoln("Reading Multi part [ERROR]", fe)
  70. e = fe
  71. return
  72. }
  73. fileName = part.FileName()
  74. if fileName != "" {
  75. fileName = path.Base(fileName)
  76. }
  77. data, e = ioutil.ReadAll(part)
  78. if e != nil {
  79. glog.V(0).Infoln("Reading Content [ERROR]", e)
  80. return
  81. }
  82. //if the filename is empty string, do a search on the other multi-part items
  83. for fileName == "" {
  84. part2, fe := form.NextPart()
  85. if fe != nil {
  86. break // no more or on error, just safely break
  87. }
  88. fName := part2.FileName()
  89. //found the first <file type> multi-part has filename
  90. if fName != "" {
  91. data2, fe2 := ioutil.ReadAll(part2)
  92. if fe2 != nil {
  93. glog.V(0).Infoln("Reading Content [ERROR]", fe2)
  94. e = fe2
  95. return
  96. }
  97. //update
  98. data = data2
  99. fileName = path.Base(fName)
  100. break
  101. }
  102. }
  103. isChunkedFile, _ = strconv.ParseBool(r.FormValue("cm"))
  104. if !isChunkedFile {
  105. dotIndex := strings.LastIndex(fileName, ".")
  106. ext, mtype := "", ""
  107. if dotIndex > 0 {
  108. ext = strings.ToLower(fileName[dotIndex:])
  109. mtype = mime.TypeByExtension(ext)
  110. }
  111. contentType := part.Header.Get("Content-Type")
  112. if contentType != "" && mtype != contentType {
  113. mimeType = contentType //only return mime type if not deductable
  114. mtype = contentType
  115. }
  116. if part.Header.Get("Content-Encoding") == "gzip" {
  117. isGzipped = true
  118. } else if operation.IsGzippable(ext, mtype) {
  119. if data, e = operation.GzipData(data); e != nil {
  120. return
  121. }
  122. isGzipped = true
  123. }
  124. if ext == ".gz" {
  125. if strings.HasSuffix(fileName, ".css.gz") ||
  126. strings.HasSuffix(fileName, ".html.gz") ||
  127. strings.HasSuffix(fileName, ".txt.gz") ||
  128. strings.HasSuffix(fileName, ".js.gz") {
  129. fileName = fileName[:len(fileName)-3]
  130. isGzipped = true
  131. }
  132. }
  133. }
  134. modifiedTime, _ = strconv.ParseUint(r.FormValue("ts"), 10, 64)
  135. ttl, _ = ReadTTL(r.FormValue("ttl"))
  136. return
  137. }
  138. func NewNeedle(r *http.Request, fixJpgOrientation bool) (n *Needle, e error) {
  139. var pairMap map[string]string
  140. fname, mimeType, isGzipped, isChunkedFile := "", "", false, false
  141. n = new(Needle)
  142. fname, n.Data, mimeType, pairMap, isGzipped, n.LastModified, n.Ttl, isChunkedFile, e = ParseUpload(r)
  143. if e != nil {
  144. return
  145. }
  146. if len(fname) < 256 {
  147. n.Name = []byte(fname)
  148. n.SetHasName()
  149. }
  150. if len(mimeType) < 256 {
  151. n.Mime = []byte(mimeType)
  152. n.SetHasMime()
  153. }
  154. if len(pairMap) != 0 {
  155. trimmedPairMap := make(map[string]string)
  156. for k, v := range pairMap {
  157. trimmedPairMap[k[len(PairNamePrefix):]] = v
  158. }
  159. pairs, _ := json.Marshal(trimmedPairMap)
  160. if len(pairs) < 65536 {
  161. n.Pairs = pairs
  162. n.PairsSize = uint16(len(pairs))
  163. n.SetHasPairs()
  164. }
  165. }
  166. if isGzipped {
  167. n.SetGzipped()
  168. }
  169. if n.LastModified == 0 {
  170. n.LastModified = uint64(time.Now().Unix())
  171. }
  172. n.SetHasLastModifiedDate()
  173. if n.Ttl != EMPTY_TTL {
  174. n.SetHasTtl()
  175. }
  176. if isChunkedFile {
  177. n.SetIsChunkManifest()
  178. }
  179. if fixJpgOrientation {
  180. loweredName := strings.ToLower(fname)
  181. if mimeType == "image/jpeg" || strings.HasSuffix(loweredName, ".jpg") || strings.HasSuffix(loweredName, ".jpeg") {
  182. n.Data = images.FixJpgOrientation(n.Data)
  183. }
  184. }
  185. n.Checksum = NewCRC(n.Data)
  186. commaSep := strings.LastIndex(r.URL.Path, ",")
  187. dotSep := strings.LastIndex(r.URL.Path, ".")
  188. fid := r.URL.Path[commaSep+1:]
  189. if dotSep > 0 {
  190. fid = r.URL.Path[commaSep+1 : dotSep]
  191. }
  192. e = n.ParsePath(fid)
  193. return
  194. }
  195. func (n *Needle) ParsePath(fid string) (err error) {
  196. length := len(fid)
  197. if length <= 8 {
  198. return fmt.Errorf("Invalid fid: %s", fid)
  199. }
  200. delta := ""
  201. deltaIndex := strings.LastIndex(fid, "_")
  202. if deltaIndex > 0 {
  203. fid, delta = fid[0:deltaIndex], fid[deltaIndex+1:]
  204. }
  205. n.Id, n.Cookie, err = ParseKeyHash(fid)
  206. if err != nil {
  207. return err
  208. }
  209. if delta != "" {
  210. if d, e := strconv.ParseUint(delta, 10, 64); e == nil {
  211. n.Id += d
  212. } else {
  213. return e
  214. }
  215. }
  216. return err
  217. }
  218. func ParseKeyHash(key_hash_string string) (uint64, uint32, error) {
  219. if len(key_hash_string) <= 8 {
  220. return 0, 0, fmt.Errorf("KeyHash is too short.")
  221. }
  222. if len(key_hash_string) > 24 {
  223. return 0, 0, fmt.Errorf("KeyHash is too long.")
  224. }
  225. split := len(key_hash_string) - 8
  226. key, err := strconv.ParseUint(key_hash_string[:split], 16, 64)
  227. if err != nil {
  228. return 0, 0, fmt.Errorf("Parse key error: %v", err)
  229. }
  230. hash, err := strconv.ParseUint(key_hash_string[split:], 16, 32)
  231. if err != nil {
  232. return 0, 0, fmt.Errorf("Parse hash error: %v", err)
  233. }
  234. return key, uint32(hash), nil
  235. }