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.

166 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/go/glog"
  7. "github.com/chrislusf/seaweedfs/go/operation"
  8. "github.com/chrislusf/seaweedfs/go/storage"
  9. "github.com/chrislusf/seaweedfs/go/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. defer n.ReleaseMemory()
  52. if ok != nil {
  53. m := make(map[string]uint32)
  54. m["size"] = 0
  55. writeJsonQuiet(w, r, http.StatusNotFound, m)
  56. return
  57. }
  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. ret := topology.ReplicatedDelete(vs.GetMasterNode(), vs.store, volumeId, n, r)
  78. if ret != 0 {
  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, errors.New("Deletion Failed."))
  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. n.ReleaseMemory()
  111. continue
  112. }
  113. if n.IsChunkedManifest() {
  114. ret = append(ret, operation.DeleteResult{
  115. Fid: fid,
  116. Status: http.StatusNotAcceptable,
  117. Error: "ChunkManifest: not allowed in batch delete mode.",
  118. })
  119. n.ReleaseMemory()
  120. continue
  121. }
  122. if n.Cookie != cookie {
  123. ret = append(ret, operation.DeleteResult{
  124. Fid: fid,
  125. Status: http.StatusBadRequest,
  126. Error: "File Random Cookie does not match.",
  127. })
  128. glog.V(0).Infoln("deleting", fid, "with unmaching cookie from ", r.RemoteAddr, "agent", r.UserAgent())
  129. n.ReleaseMemory()
  130. return
  131. }
  132. if size, err := vs.store.Delete(volumeId, n); err != nil {
  133. ret = append(ret, operation.DeleteResult{
  134. Fid: fid,
  135. Status: http.StatusInternalServerError,
  136. Error: err.Error()},
  137. )
  138. } else {
  139. ret = append(ret, operation.DeleteResult{
  140. Fid: fid,
  141. Status: http.StatusAccepted,
  142. Size: int(size)},
  143. )
  144. }
  145. n.ReleaseMemory()
  146. }
  147. writeJsonQuiet(w, r, http.StatusAccepted, ret)
  148. }