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.

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