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.

160 lines
4.4 KiB

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