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.

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