volume_server_handlers_write.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package weed_server
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "net/http"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "github.com/chrislusf/seaweedfs/weed/glog"
  11. "github.com/chrislusf/seaweedfs/weed/operation"
  12. "github.com/chrislusf/seaweedfs/weed/stats"
  13. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  14. "github.com/chrislusf/seaweedfs/weed/topology"
  15. )
  16. func (vs *VolumeServer) PostHandler(w http.ResponseWriter, r *http.Request) {
  17. stats.VolumeServerRequestCounter.WithLabelValues("post").Inc()
  18. start := time.Now()
  19. defer func() {
  20. stats.VolumeServerRequestHistogram.WithLabelValues("post").Observe(time.Since(start).Seconds())
  21. }()
  22. if e := r.ParseForm(); e != nil {
  23. glog.V(0).Infoln("form parse error:", e)
  24. writeJsonError(w, r, http.StatusBadRequest, e)
  25. return
  26. }
  27. vid, fid, _, _, _ := parseURLPath(r.URL.Path)
  28. volumeId, ve := needle.NewVolumeId(vid)
  29. if ve != nil {
  30. glog.V(0).Infoln("NewVolumeId error:", ve)
  31. writeJsonError(w, r, http.StatusBadRequest, ve)
  32. return
  33. }
  34. if !vs.maybeCheckJwtAuthorization(r, vid, fid, true) {
  35. writeJsonError(w, r, http.StatusUnauthorized, errors.New("wrong jwt"))
  36. return
  37. }
  38. needle, originalSize, ne := needle.CreateNeedleFromRequest(r, vs.FixJpgOrientation, vs.fileSizeLimitBytes)
  39. if ne != nil {
  40. writeJsonError(w, r, http.StatusBadRequest, ne)
  41. return
  42. }
  43. ret := operation.UploadResult{}
  44. _, isUnchanged, writeError := topology.ReplicatedWrite(vs.GetMaster(), vs.store, volumeId, needle, r)
  45. // http 204 status code does not allow body
  46. if writeError == nil && isUnchanged {
  47. setEtag(w, needle.Etag())
  48. w.WriteHeader(http.StatusNoContent)
  49. return
  50. }
  51. httpStatus := http.StatusCreated
  52. if writeError != nil {
  53. httpStatus = http.StatusInternalServerError
  54. ret.Error = writeError.Error()
  55. }
  56. if needle.HasName() {
  57. ret.Name = string(needle.Name)
  58. }
  59. ret.Size = uint32(originalSize)
  60. ret.ETag = needle.Etag()
  61. setEtag(w, ret.ETag)
  62. writeJsonQuiet(w, r, httpStatus, ret)
  63. }
  64. func (vs *VolumeServer) DeleteHandler(w http.ResponseWriter, r *http.Request) {
  65. stats.VolumeServerRequestCounter.WithLabelValues("delete").Inc()
  66. start := time.Now()
  67. defer func() {
  68. stats.VolumeServerRequestHistogram.WithLabelValues("delete").Observe(time.Since(start).Seconds())
  69. }()
  70. n := new(needle.Needle)
  71. vid, fid, _, _, _ := parseURLPath(r.URL.Path)
  72. volumeId, _ := needle.NewVolumeId(vid)
  73. n.ParsePath(fid)
  74. if !vs.maybeCheckJwtAuthorization(r, vid, fid, true) {
  75. writeJsonError(w, r, http.StatusUnauthorized, errors.New("wrong jwt"))
  76. return
  77. }
  78. // glog.V(2).Infof("volume %s deleting %s", vid, n)
  79. cookie := n.Cookie
  80. ecVolume, hasEcVolume := vs.store.FindEcVolume(volumeId)
  81. if hasEcVolume {
  82. count, err := vs.store.DeleteEcShardNeedle(context.Background(), ecVolume, n, cookie)
  83. writeDeleteResult(err, count, w, r)
  84. return
  85. }
  86. _, ok := vs.store.ReadVolumeNeedle(volumeId, n)
  87. if ok != nil {
  88. m := make(map[string]uint32)
  89. m["size"] = 0
  90. writeJsonQuiet(w, r, http.StatusNotFound, m)
  91. return
  92. }
  93. if n.Cookie != cookie {
  94. glog.V(0).Infoln("delete", r.URL.Path, "with unmaching cookie from ", r.RemoteAddr, "agent", r.UserAgent())
  95. writeJsonError(w, r, http.StatusBadRequest, errors.New("File Random Cookie does not match."))
  96. return
  97. }
  98. count := int64(n.Size)
  99. if n.IsChunkedManifest() {
  100. chunkManifest, e := operation.LoadChunkManifest(n.Data, n.IsGzipped())
  101. if e != nil {
  102. writeJsonError(w, r, http.StatusInternalServerError, fmt.Errorf("Load chunks manifest error: %v", e))
  103. return
  104. }
  105. // make sure all chunks had deleted before delete manifest
  106. if e := chunkManifest.DeleteChunks(vs.GetMaster(), vs.grpcDialOption); e != nil {
  107. writeJsonError(w, r, http.StatusInternalServerError, fmt.Errorf("Delete chunks error: %v", e))
  108. return
  109. }
  110. count = chunkManifest.Size
  111. }
  112. n.LastModified = uint64(time.Now().Unix())
  113. if len(r.FormValue("ts")) > 0 {
  114. modifiedTime, err := strconv.ParseInt(r.FormValue("ts"), 10, 64)
  115. if err == nil {
  116. n.LastModified = uint64(modifiedTime)
  117. }
  118. }
  119. _, err := topology.ReplicatedDelete(vs.GetMaster(), vs.store, volumeId, n, r)
  120. writeDeleteResult(err, count, w, r)
  121. }
  122. func writeDeleteResult(err error, count int64, w http.ResponseWriter, r *http.Request) {
  123. if err == nil {
  124. m := make(map[string]int64)
  125. m["size"] = count
  126. writeJsonQuiet(w, r, http.StatusAccepted, m)
  127. } else {
  128. writeJsonError(w, r, http.StatusInternalServerError, fmt.Errorf("Deletion Failed: %v", err))
  129. }
  130. }
  131. func setEtag(w http.ResponseWriter, etag string) {
  132. if etag != "" {
  133. if strings.HasPrefix(etag, "\"") {
  134. w.Header().Set("ETag", etag)
  135. } else {
  136. w.Header().Set("ETag", "\""+etag+"\"")
  137. }
  138. }
  139. }