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.

257 lines
6.3 KiB

12 years ago
12 years ago
12 years ago
12 years ago
9 years ago
9 years ago
9 years ago
12 years ago
  1. package storage
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "math"
  6. "mime"
  7. "net/http"
  8. "path"
  9. "strconv"
  10. "strings"
  11. "time"
  12. "encoding/json"
  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. rawBlock *Block // underlying supporing []byte, fetched and released into a pool
  47. }
  48. func (n *Needle) String() (str string) {
  49. 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)
  50. return
  51. }
  52. func ParseUpload(r *http.Request) (
  53. fileName string, data []byte, mimeType string, pairs []byte, isGzipped bool,
  54. modifiedTime uint64, ttl *TTL, isChunkedFile bool, e error) {
  55. pairMap := make(map[string]string)
  56. for k, v := range r.Header {
  57. if len(v) > 0 && strings.HasPrefix(k, PairNamePrefix) {
  58. pairMap[k] = v[0]
  59. }
  60. }
  61. if len(pairMap) != 0 {
  62. pairs, _ = json.Marshal(pairMap)
  63. }
  64. form, fe := r.MultipartReader()
  65. if fe != nil {
  66. glog.V(0).Infoln("MultipartReader [ERROR]", fe)
  67. e = fe
  68. return
  69. }
  70. //first multi-part item
  71. part, fe := form.NextPart()
  72. if fe != nil {
  73. glog.V(0).Infoln("Reading Multi part [ERROR]", fe)
  74. e = fe
  75. return
  76. }
  77. fileName = part.FileName()
  78. if fileName != "" {
  79. fileName = path.Base(fileName)
  80. }
  81. data, e = ioutil.ReadAll(part)
  82. if e != nil {
  83. glog.V(0).Infoln("Reading Content [ERROR]", e)
  84. return
  85. }
  86. //if the filename is empty string, do a search on the other multi-part items
  87. for fileName == "" {
  88. part2, fe := form.NextPart()
  89. if fe != nil {
  90. break // no more or on error, just safely break
  91. }
  92. fName := part2.FileName()
  93. //found the first <file type> multi-part has filename
  94. if fName != "" {
  95. data2, fe2 := ioutil.ReadAll(part2)
  96. if fe2 != nil {
  97. glog.V(0).Infoln("Reading Content [ERROR]", fe2)
  98. e = fe2
  99. return
  100. }
  101. //update
  102. data = data2
  103. fileName = path.Base(fName)
  104. break
  105. }
  106. }
  107. isChunkedFile, _ = strconv.ParseBool(r.FormValue("cm"))
  108. dotIndex := strings.LastIndex(fileName, ".")
  109. ext, mtype := "", ""
  110. if dotIndex > 0 {
  111. ext = strings.ToLower(fileName[dotIndex:])
  112. mtype = mime.TypeByExtension(ext)
  113. }
  114. contentType := part.Header.Get("Content-Type")
  115. if contentType != "" && mtype != contentType {
  116. mimeType = contentType //only return mime type if not deductable
  117. mtype = contentType
  118. }
  119. if !isChunkedFile {
  120. if part.Header.Get("Content-Encoding") == "gzip" {
  121. isGzipped = true
  122. } else if operation.IsGzippable(ext, mtype) {
  123. if data, e = operation.GzipData(data); e != nil {
  124. return
  125. }
  126. isGzipped = true
  127. }
  128. if ext == ".gz" {
  129. isGzipped = true
  130. }
  131. if strings.HasSuffix(fileName, ".gz") &&
  132. !strings.HasSuffix(fileName, ".tar.gz") {
  133. fileName = fileName[:len(fileName)-3]
  134. }
  135. }
  136. modifiedTime, _ = strconv.ParseUint(r.FormValue("ts"), 10, 64)
  137. ttl, _ = ReadTTL(r.FormValue("ttl"))
  138. return
  139. }
  140. func NewNeedle(r *http.Request, fixJpgOrientation bool) (n *Needle, e error) {
  141. var pair []byte
  142. fname, mimeType, isGzipped, isChunkedFile := "", "", false, false
  143. n = new(Needle)
  144. fname, n.Data, mimeType, pair, isGzipped, n.LastModified, n.Ttl, isChunkedFile, e = ParseUpload(r)
  145. if e != nil {
  146. return
  147. }
  148. if len(fname) < 256 {
  149. n.Name = []byte(fname)
  150. n.SetHasName()
  151. }
  152. if len(mimeType) < 256 {
  153. n.Mime = []byte(mimeType)
  154. n.SetHasMime()
  155. }
  156. if len(pair) < 65536 {
  157. n.Pairs = pair
  158. n.PairsSize = uint16(len(pair))
  159. n.SetHasPairs()
  160. }
  161. if isGzipped {
  162. n.SetGzipped()
  163. }
  164. if n.LastModified == 0 {
  165. n.LastModified = uint64(time.Now().Unix())
  166. }
  167. n.SetHasLastModifiedDate()
  168. if n.Ttl != EMPTY_TTL {
  169. n.SetHasTtl()
  170. }
  171. if isChunkedFile {
  172. n.SetIsChunkManifest()
  173. }
  174. if fixJpgOrientation {
  175. loweredName := strings.ToLower(fname)
  176. if mimeType == "image/jpeg" || strings.HasSuffix(loweredName, ".jpg") || strings.HasSuffix(loweredName, ".jpeg") {
  177. n.Data = images.FixJpgOrientation(n.Data)
  178. }
  179. }
  180. n.Checksum = NewCRC(n.Data)
  181. commaSep := strings.LastIndex(r.URL.Path, ",")
  182. dotSep := strings.LastIndex(r.URL.Path, ".")
  183. fid := r.URL.Path[commaSep+1:]
  184. if dotSep > 0 {
  185. fid = r.URL.Path[commaSep+1 : dotSep]
  186. }
  187. e = n.ParsePath(fid)
  188. return
  189. }
  190. func (n *Needle) ParsePath(fid string) (err error) {
  191. length := len(fid)
  192. if length <= 8 {
  193. return fmt.Errorf("Invalid fid: %s", fid)
  194. }
  195. delta := ""
  196. deltaIndex := strings.LastIndex(fid, "_")
  197. if deltaIndex > 0 {
  198. fid, delta = fid[0:deltaIndex], fid[deltaIndex+1:]
  199. }
  200. n.Id, n.Cookie, err = ParseKeyHash(fid)
  201. if err != nil {
  202. return err
  203. }
  204. if delta != "" {
  205. if d, e := strconv.ParseUint(delta, 10, 64); e == nil {
  206. n.Id += d
  207. } else {
  208. return e
  209. }
  210. }
  211. return err
  212. }
  213. func ParseKeyHash(key_hash_string string) (uint64, uint32, error) {
  214. if len(key_hash_string) <= 8 {
  215. return 0, 0, fmt.Errorf("KeyHash is too short.")
  216. }
  217. if len(key_hash_string) > 24 {
  218. return 0, 0, fmt.Errorf("KeyHash is too long.")
  219. }
  220. split := len(key_hash_string) - 8
  221. key, err := strconv.ParseUint(key_hash_string[:split], 16, 64)
  222. if err != nil {
  223. return 0, 0, fmt.Errorf("Parse key error: %v", err)
  224. }
  225. hash, err := strconv.ParseUint(key_hash_string[split:], 16, 32)
  226. if err != nil {
  227. return 0, 0, fmt.Errorf("Parse hash error: %v", err)
  228. }
  229. return key, uint32(hash), nil
  230. }