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.

159 lines
4.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. if _, ok := vs.store.ReadVolumeNeedle(volumeId, n); ok != nil {
  50. m := make(map[string]uint32)
  51. m["size"] = 0
  52. writeJsonQuiet(w, r, http.StatusNotFound, m)
  53. return
  54. }
  55. if n.Cookie != cookie {
  56. glog.V(0).Infoln("delete", r.URL.Path, "with unmaching cookie from ", r.RemoteAddr, "agent", r.UserAgent())
  57. writeJsonError(w, r, http.StatusBadRequest, errors.New("File Random Cookie does not match."))
  58. return
  59. }
  60. count := int64(n.Size)
  61. if n.IsChunkedManifest() {
  62. chunkManifest, e := operation.LoadChunkManifest(n.Data, n.IsGzipped())
  63. if e != nil {
  64. writeJsonError(w, r, http.StatusInternalServerError, errors.New("Load chunks manifest error: "+e.Error()))
  65. return
  66. }
  67. if e := chunkManifest.DeleteChunks(vs.GetMasterNode()); e != nil {
  68. writeJsonError(w, r, http.StatusInternalServerError, errors.New("Delete chunks error: "+e.Error()))
  69. return
  70. }
  71. count = chunkManifest.Size
  72. }
  73. ret := topology.ReplicatedDelete(vs.GetMasterNode(), vs.store, volumeId, n, r)
  74. if ret != 0 {
  75. m := make(map[string]int64)
  76. m["size"] = count
  77. writeJsonQuiet(w, r, http.StatusAccepted, m)
  78. } else {
  79. writeJsonError(w, r, http.StatusInternalServerError, errors.New("Deletion Failed."))
  80. }
  81. }
  82. //Experts only: takes multiple fid parameters. This function does not propagate deletes to replicas.
  83. func (vs *VolumeServer) batchDeleteHandler(w http.ResponseWriter, r *http.Request) {
  84. r.ParseForm()
  85. var ret []operation.DeleteResult
  86. for _, fid := range r.Form["fid"] {
  87. vid, id_cookie, err := operation.ParseFileId(fid)
  88. if err != nil {
  89. ret = append(ret, operation.DeleteResult{
  90. Fid: fid,
  91. Status: http.StatusBadRequest,
  92. Error: err.Error()})
  93. continue
  94. }
  95. n := new(storage.Needle)
  96. volumeId, _ := storage.NewVolumeId(vid)
  97. n.ParsePath(id_cookie)
  98. glog.V(4).Infoln("batch deleting", n)
  99. cookie := n.Cookie
  100. if _, err := vs.store.ReadVolumeNeedle(volumeId, n); err != nil {
  101. ret = append(ret, operation.DeleteResult{
  102. Fid: fid,
  103. Status: http.StatusNotFound,
  104. Error: err.Error(),
  105. })
  106. continue
  107. }
  108. if n.IsChunkedManifest() {
  109. //Don't allow delete manifest in batch delete mode
  110. ret = append(ret, operation.DeleteResult{
  111. Fid: fid,
  112. Status: http.StatusNotAcceptable,
  113. Error: "ChunkManifest: not allow.",
  114. })
  115. continue
  116. }
  117. if n.Cookie != cookie {
  118. ret = append(ret, operation.DeleteResult{
  119. Fid: fid,
  120. Status: http.StatusBadRequest,
  121. Error: "File Random Cookie does not match.",
  122. })
  123. glog.V(0).Infoln("deleting", fid, "with unmaching cookie from ", r.RemoteAddr, "agent", r.UserAgent())
  124. return
  125. }
  126. if size, err := vs.store.Delete(volumeId, n); err != nil {
  127. ret = append(ret, operation.DeleteResult{
  128. Fid: fid,
  129. Status: http.StatusInternalServerError,
  130. Error: err.Error()},
  131. )
  132. } else {
  133. ret = append(ret, operation.DeleteResult{
  134. Fid: fid,
  135. Status: http.StatusAccepted,
  136. Size: int(size)},
  137. )
  138. }
  139. }
  140. writeJsonQuiet(w, r, http.StatusAccepted, ret)
  141. }