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.

114 lines
3.3 KiB

  1. package weed_server
  2. import (
  3. "errors"
  4. "net/http"
  5. "github.com/chrislusf/seaweedfs/go/glog"
  6. "github.com/chrislusf/seaweedfs/go/operation"
  7. "github.com/chrislusf/seaweedfs/go/storage"
  8. "github.com/chrislusf/seaweedfs/go/topology"
  9. )
  10. func (vs *VolumeServer) PostHandler(w http.ResponseWriter, r *http.Request) {
  11. if e := r.ParseForm(); e != nil {
  12. glog.V(0).Infoln("form parse error:", e)
  13. writeJsonError(w, r, http.StatusBadRequest, e)
  14. return
  15. }
  16. vid, _, _, _, _ := parseURLPath(r.URL.Path)
  17. volumeId, ve := storage.NewVolumeId(vid)
  18. if ve != nil {
  19. glog.V(0).Infoln("NewVolumeId error:", ve)
  20. writeJsonError(w, r, http.StatusBadRequest, ve)
  21. return
  22. }
  23. needle, ne := storage.NewNeedle(r, vs.FixJpgOrientation)
  24. if ne != nil {
  25. writeJsonError(w, r, http.StatusBadRequest, ne)
  26. return
  27. }
  28. ret := operation.UploadResult{}
  29. size, errorStatus := topology.ReplicatedWrite(vs.GetMasterNode(),
  30. vs.store, volumeId, needle, r)
  31. httpStatus := http.StatusCreated
  32. if errorStatus != "" {
  33. httpStatus = http.StatusInternalServerError
  34. ret.Error = errorStatus
  35. }
  36. if needle.HasName() {
  37. ret.Name = string(needle.Name)
  38. }
  39. ret.Size = size
  40. writeJsonQuiet(w, r, httpStatus, ret)
  41. }
  42. func (vs *VolumeServer) DeleteHandler(w http.ResponseWriter, r *http.Request) {
  43. n := new(storage.Needle)
  44. vid, fid, _, _, _ := parseURLPath(r.URL.Path)
  45. volumeId, _ := storage.NewVolumeId(vid)
  46. n.ParsePath(fid)
  47. glog.V(2).Infoln("deleting", n)
  48. cookie := n.Cookie
  49. count, ok := vs.store.ReadVolumeNeedle(volumeId, n)
  50. if 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. return
  59. }
  60. ret := topology.ReplicatedDelete(vs.GetMasterNode(), vs.store, volumeId, n, r)
  61. if ret != 0 {
  62. m := make(map[string]uint32)
  63. m["size"] = uint32(count)
  64. writeJsonQuiet(w, r, http.StatusAccepted, m)
  65. } else {
  66. writeJsonError(w, r, http.StatusInternalServerError, errors.New("Deletion Failed."))
  67. }
  68. }
  69. //Experts only: takes multiple fid parameters. This function does not propagate deletes to replicas.
  70. func (vs *VolumeServer) batchDeleteHandler(w http.ResponseWriter, r *http.Request) {
  71. r.ParseForm()
  72. var ret []operation.DeleteResult
  73. for _, fid := range r.Form["fid"] {
  74. vid, id_cookie, err := operation.ParseFileId(fid)
  75. if err != nil {
  76. ret = append(ret, operation.DeleteResult{Fid: fid, Error: err.Error()})
  77. continue
  78. }
  79. n := new(storage.Needle)
  80. volumeId, _ := storage.NewVolumeId(vid)
  81. n.ParsePath(id_cookie)
  82. glog.V(4).Infoln("batch deleting", n)
  83. cookie := n.Cookie
  84. if _, err := vs.store.ReadVolumeNeedle(volumeId, n); err != nil {
  85. ret = append(ret, operation.DeleteResult{Fid: fid, Error: err.Error()})
  86. continue
  87. }
  88. if n.Cookie != cookie {
  89. ret = append(ret, operation.DeleteResult{Fid: fid, Error: "File Random Cookie does not match."})
  90. glog.V(0).Infoln("deleting", fid, "with unmaching cookie from ", r.RemoteAddr, "agent", r.UserAgent())
  91. return
  92. }
  93. if size, err := vs.store.Delete(volumeId, n); err != nil {
  94. ret = append(ret, operation.DeleteResult{Fid: fid, Error: err.Error()})
  95. } else {
  96. ret = append(ret, operation.DeleteResult{Fid: fid, Size: int(size)})
  97. }
  98. }
  99. writeJsonQuiet(w, r, http.StatusAccepted, ret)
  100. }