volume_server_handlers_write.go 4.4 KB

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