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
  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. // make sure all chunks had deleted before delete manifest
  68. if e := chunkManifest.DeleteChunks(vs.GetMasterNode()); e != nil {
  69. writeJsonError(w, r, http.StatusInternalServerError, errors.New("Delete chunks error: "+e.Error()))
  70. return
  71. }
  72. count = chunkManifest.Size
  73. }
  74. ret := topology.ReplicatedDelete(vs.GetMasterNode(), vs.store, volumeId, n, r)
  75. if ret != 0 {
  76. m := make(map[string]int64)
  77. m["size"] = count
  78. writeJsonQuiet(w, r, http.StatusAccepted, m)
  79. } else {
  80. writeJsonError(w, r, http.StatusInternalServerError, errors.New("Deletion Failed."))
  81. }
  82. }
  83. //Experts only: takes multiple fid parameters. This function does not propagate deletes to replicas.
  84. func (vs *VolumeServer) batchDeleteHandler(w http.ResponseWriter, r *http.Request) {
  85. r.ParseForm()
  86. var ret []operation.DeleteResult
  87. for _, fid := range r.Form["fid"] {
  88. vid, id_cookie, err := operation.ParseFileId(fid)
  89. if err != nil {
  90. ret = append(ret, operation.DeleteResult{
  91. Fid: fid,
  92. Status: http.StatusBadRequest,
  93. Error: err.Error()})
  94. continue
  95. }
  96. n := new(storage.Needle)
  97. volumeId, _ := storage.NewVolumeId(vid)
  98. n.ParsePath(id_cookie)
  99. glog.V(4).Infoln("batch deleting", n)
  100. cookie := n.Cookie
  101. if _, err := vs.store.ReadVolumeNeedle(volumeId, n); err != nil {
  102. ret = append(ret, operation.DeleteResult{
  103. Fid: fid,
  104. Status: http.StatusNotFound,
  105. Error: err.Error(),
  106. })
  107. continue
  108. }
  109. if n.IsChunkedManifest() {
  110. //Don't allow delete manifest in batch delete mode
  111. ret = append(ret, operation.DeleteResult{
  112. Fid: fid,
  113. Status: http.StatusNotAcceptable,
  114. Error: "ChunkManifest: not allow.",
  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. }