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.

125 lines
3.7 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. if n.IsChunkedManifest(){
  61. chunkManifest, e := operation.LoadChunkManifest(n.Data, n.IsGzipped())
  62. if e != nil {
  63. writeJsonError(w, r, http.StatusInternalServerError, errors.New("Load chunks manifest error: " + e.Error()))
  64. return
  65. }
  66. if e := chunkManifest.DeleteChunks(vs.GetMasterNode()); e != nil {
  67. writeJsonError(w, r, http.StatusInternalServerError, errors.New("Delete chunks error: " + e.Error()))
  68. return
  69. }
  70. }
  71. ret := topology.ReplicatedDelete(vs.GetMasterNode(), vs.store, volumeId, n, r)
  72. if ret != 0 {
  73. m := make(map[string]uint32)
  74. m["size"] = uint32(count)
  75. writeJsonQuiet(w, r, http.StatusAccepted, m)
  76. } else {
  77. writeJsonError(w, r, http.StatusInternalServerError, errors.New("Deletion Failed."))
  78. }
  79. }
  80. //Experts only: takes multiple fid parameters. This function does not propagate deletes to replicas.
  81. func (vs *VolumeServer) batchDeleteHandler(w http.ResponseWriter, r *http.Request) {
  82. r.ParseForm()
  83. var ret []operation.DeleteResult
  84. for _, fid := range r.Form["fid"] {
  85. vid, id_cookie, err := operation.ParseFileId(fid)
  86. if err != nil {
  87. ret = append(ret, operation.DeleteResult{Fid: fid, Error: err.Error()})
  88. continue
  89. }
  90. n := new(storage.Needle)
  91. volumeId, _ := storage.NewVolumeId(vid)
  92. n.ParsePath(id_cookie)
  93. glog.V(4).Infoln("batch deleting", n)
  94. cookie := n.Cookie
  95. if _, err := vs.store.ReadVolumeNeedle(volumeId, n); err != nil {
  96. ret = append(ret, operation.DeleteResult{Fid: fid, Error: err.Error()})
  97. continue
  98. }
  99. if n.Cookie != cookie {
  100. ret = append(ret, operation.DeleteResult{Fid: fid, Error: "File Random Cookie does not match."})
  101. glog.V(0).Infoln("deleting", fid, "with unmaching cookie from ", r.RemoteAddr, "agent", r.UserAgent())
  102. return
  103. }
  104. if size, err := vs.store.Delete(volumeId, n); err != nil {
  105. ret = append(ret, operation.DeleteResult{Fid: fid, Error: err.Error()})
  106. } else {
  107. ret = append(ret, operation.DeleteResult{Fid: fid, Size: int(size)})
  108. }
  109. }
  110. writeJsonQuiet(w, r, http.StatusAccepted, ret)
  111. }