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.9 KiB

9 years ago
6 years ago
7 years ago
9 years ago
9 years ago
9 years ago
6 years ago
9 years ago
  1. package weed_server
  2. import (
  3. "errors"
  4. "fmt"
  5. "net/http"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/operation"
  11. "github.com/chrislusf/seaweedfs/weed/storage"
  12. "github.com/chrislusf/seaweedfs/weed/topology"
  13. )
  14. func (vs *VolumeServer) PostHandler(w http.ResponseWriter, r *http.Request) {
  15. if e := r.ParseForm(); e != nil {
  16. glog.V(0).Infoln("form parse error:", e)
  17. writeJsonError(w, r, http.StatusBadRequest, e)
  18. return
  19. }
  20. vid, _, _, _, _ := parseURLPath(r.URL.Path)
  21. volumeId, ve := storage.NewVolumeId(vid)
  22. if ve != nil {
  23. glog.V(0).Infoln("NewVolumeId error:", ve)
  24. writeJsonError(w, r, http.StatusBadRequest, ve)
  25. return
  26. }
  27. needle, ne := storage.NewNeedle(r, vs.FixJpgOrientation)
  28. if ne != nil {
  29. writeJsonError(w, r, http.StatusBadRequest, ne)
  30. return
  31. }
  32. ret := operation.UploadResult{}
  33. size, errorStatus := topology.ReplicatedWrite(vs.GetMaster(),
  34. vs.store, volumeId, needle, r)
  35. httpStatus := http.StatusCreated
  36. if errorStatus != "" {
  37. httpStatus = http.StatusInternalServerError
  38. ret.Error = errorStatus
  39. }
  40. if needle.HasName() {
  41. ret.Name = string(needle.Name)
  42. }
  43. ret.Size = size
  44. setEtag(w, needle.Etag())
  45. writeJsonQuiet(w, r, httpStatus, ret)
  46. }
  47. func (vs *VolumeServer) DeleteHandler(w http.ResponseWriter, r *http.Request) {
  48. n := new(storage.Needle)
  49. vid, fid, _, _, _ := parseURLPath(r.URL.Path)
  50. volumeId, _ := storage.NewVolumeId(vid)
  51. n.ParsePath(fid)
  52. glog.V(2).Infof("volume %s deleting %s", vid, n)
  53. cookie := n.Cookie
  54. _, ok := vs.store.ReadVolumeNeedle(volumeId, n)
  55. if ok != nil {
  56. m := make(map[string]uint32)
  57. m["size"] = 0
  58. writeJsonQuiet(w, r, http.StatusNotFound, m)
  59. return
  60. }
  61. if n.Cookie != cookie {
  62. glog.V(0).Infoln("delete", r.URL.Path, "with unmaching cookie from ", r.RemoteAddr, "agent", r.UserAgent())
  63. writeJsonError(w, r, http.StatusBadRequest, errors.New("File Random Cookie does not match."))
  64. return
  65. }
  66. count := int64(n.Size)
  67. if n.IsChunkedManifest() {
  68. chunkManifest, e := operation.LoadChunkManifest(n.Data, n.IsGzipped())
  69. if e != nil {
  70. writeJsonError(w, r, http.StatusInternalServerError, fmt.Errorf("Load chunks manifest error: %v", e))
  71. return
  72. }
  73. // make sure all chunks had deleted before delete manifest
  74. if e := chunkManifest.DeleteChunks(vs.GetMaster()); e != nil {
  75. writeJsonError(w, r, http.StatusInternalServerError, fmt.Errorf("Delete chunks error: %v", e))
  76. return
  77. }
  78. count = chunkManifest.Size
  79. }
  80. n.LastModified = uint64(time.Now().Unix())
  81. if len(r.FormValue("ts")) > 0 {
  82. modifiedTime, err := strconv.ParseInt(r.FormValue("ts"), 10, 64)
  83. if err == nil {
  84. n.LastModified = uint64(modifiedTime)
  85. }
  86. }
  87. _, err := topology.ReplicatedDelete(vs.GetMaster(), vs.store, volumeId, n, r)
  88. if err == nil {
  89. m := make(map[string]int64)
  90. m["size"] = count
  91. writeJsonQuiet(w, r, http.StatusAccepted, m)
  92. } else {
  93. writeJsonError(w, r, http.StatusInternalServerError, fmt.Errorf("Deletion Failed: %v", err))
  94. }
  95. }
  96. //Experts only: takes multiple fid parameters. This function does not propagate deletes to replicas.
  97. func (vs *VolumeServer) batchDeleteHandler(w http.ResponseWriter, r *http.Request) {
  98. r.ParseForm()
  99. var ret []operation.DeleteResult
  100. now := uint64(time.Now().Unix())
  101. for _, fid := range r.Form["fid"] {
  102. vid, id_cookie, err := operation.ParseFileId(fid)
  103. if err != nil {
  104. ret = append(ret, operation.DeleteResult{
  105. Fid: fid,
  106. Status: http.StatusBadRequest,
  107. Error: err.Error()})
  108. continue
  109. }
  110. n := new(storage.Needle)
  111. volumeId, _ := storage.NewVolumeId(vid)
  112. n.ParsePath(id_cookie)
  113. // glog.V(4).Infoln("batch deleting", n)
  114. cookie := n.Cookie
  115. if _, err := vs.store.ReadVolumeNeedle(volumeId, n); err != nil {
  116. ret = append(ret, operation.DeleteResult{
  117. Fid: fid,
  118. Status: http.StatusNotFound,
  119. Error: err.Error(),
  120. })
  121. continue
  122. }
  123. if n.IsChunkedManifest() {
  124. ret = append(ret, operation.DeleteResult{
  125. Fid: fid,
  126. Status: http.StatusNotAcceptable,
  127. Error: "ChunkManifest: not allowed in batch delete mode.",
  128. })
  129. continue
  130. }
  131. if n.Cookie != cookie {
  132. ret = append(ret, operation.DeleteResult{
  133. Fid: fid,
  134. Status: http.StatusBadRequest,
  135. Error: "File Random Cookie does not match.",
  136. })
  137. glog.V(0).Infoln("deleting", fid, "with unmaching cookie from ", r.RemoteAddr, "agent", r.UserAgent())
  138. return
  139. }
  140. n.LastModified = now
  141. if size, err := vs.store.Delete(volumeId, n); err != nil {
  142. ret = append(ret, operation.DeleteResult{
  143. Fid: fid,
  144. Status: http.StatusInternalServerError,
  145. Error: err.Error()},
  146. )
  147. } else {
  148. ret = append(ret, operation.DeleteResult{
  149. Fid: fid,
  150. Status: http.StatusAccepted,
  151. Size: int(size)},
  152. )
  153. }
  154. }
  155. writeJsonQuiet(w, r, http.StatusAccepted, ret)
  156. }
  157. func setEtag(w http.ResponseWriter, etag string) {
  158. if etag != "" {
  159. if strings.HasPrefix(etag, "\"") {
  160. w.Header().Set("ETag", etag)
  161. } else {
  162. w.Header().Set("ETag", "\""+etag+"\"")
  163. }
  164. }
  165. }