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.

135 lines
3.6 KiB

9 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 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. "strconv"
  7. "strings"
  8. "time"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/operation"
  11. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  12. "github.com/chrislusf/seaweedfs/weed/topology"
  13. )
  14. func (vs *VolumeServer) PostHandler(w http.ResponseWriter, r *http.Request) {
  15. if e := r.ParseForm(); e != nil {
  16. glog.V(0).Infoln("form parse error:", e)
  17. writeJsonError(w, r, http.StatusBadRequest, e)
  18. return
  19. }
  20. vid, fid, _, _, _ := parseURLPath(r.URL.Path)
  21. volumeId, ve := needle.NewVolumeId(vid)
  22. if ve != nil {
  23. glog.V(0).Infoln("NewVolumeId error:", ve)
  24. writeJsonError(w, r, http.StatusBadRequest, ve)
  25. return
  26. }
  27. if !vs.maybeCheckJwtAuthorization(r, vid, fid) {
  28. writeJsonError(w, r, http.StatusUnauthorized, errors.New("wrong jwt"))
  29. return
  30. }
  31. needle, originalSize, ne := needle.CreateNeedleFromRequest(r, vs.FixJpgOrientation)
  32. if ne != nil {
  33. writeJsonError(w, r, http.StatusBadRequest, ne)
  34. return
  35. }
  36. ret := operation.UploadResult{}
  37. _, isUnchanged, writeError := topology.ReplicatedWrite(vs.GetMaster(), vs.store, volumeId, needle, r)
  38. httpStatus := http.StatusCreated
  39. if isUnchanged {
  40. httpStatus = http.StatusNotModified
  41. }
  42. if writeError != nil {
  43. httpStatus = http.StatusInternalServerError
  44. ret.Error = writeError.Error()
  45. }
  46. if needle.HasName() {
  47. ret.Name = string(needle.Name)
  48. }
  49. ret.Size = uint32(originalSize)
  50. ret.ETag = needle.Etag()
  51. setEtag(w, ret.ETag)
  52. writeJsonQuiet(w, r, httpStatus, ret)
  53. }
  54. func (vs *VolumeServer) DeleteHandler(w http.ResponseWriter, r *http.Request) {
  55. n := new(needle.Needle)
  56. vid, fid, _, _, _ := parseURLPath(r.URL.Path)
  57. volumeId, _ := needle.NewVolumeId(vid)
  58. n.ParsePath(fid)
  59. if !vs.maybeCheckJwtAuthorization(r, vid, fid) {
  60. writeJsonError(w, r, http.StatusUnauthorized, errors.New("wrong jwt"))
  61. return
  62. }
  63. // glog.V(2).Infof("volume %s deleting %s", vid, n)
  64. cookie := n.Cookie
  65. _, ok := vs.store.ReadVolumeNeedle(volumeId, n)
  66. if ok != nil {
  67. m := make(map[string]uint32)
  68. m["size"] = 0
  69. writeJsonQuiet(w, r, http.StatusNotFound, m)
  70. return
  71. }
  72. if n.Cookie != cookie {
  73. glog.V(0).Infoln("delete", r.URL.Path, "with unmaching cookie from ", r.RemoteAddr, "agent", r.UserAgent())
  74. writeJsonError(w, r, http.StatusBadRequest, errors.New("File Random Cookie does not match."))
  75. return
  76. }
  77. count := int64(n.Size)
  78. if n.IsChunkedManifest() {
  79. chunkManifest, e := operation.LoadChunkManifest(n.Data, n.IsGzipped())
  80. if e != nil {
  81. writeJsonError(w, r, http.StatusInternalServerError, fmt.Errorf("Load chunks manifest error: %v", e))
  82. return
  83. }
  84. // make sure all chunks had deleted before delete manifest
  85. if e := chunkManifest.DeleteChunks(vs.GetMaster(), vs.grpcDialOption); e != nil {
  86. writeJsonError(w, r, http.StatusInternalServerError, fmt.Errorf("Delete chunks error: %v", e))
  87. return
  88. }
  89. count = chunkManifest.Size
  90. }
  91. n.LastModified = uint64(time.Now().Unix())
  92. if len(r.FormValue("ts")) > 0 {
  93. modifiedTime, err := strconv.ParseInt(r.FormValue("ts"), 10, 64)
  94. if err == nil {
  95. n.LastModified = uint64(modifiedTime)
  96. }
  97. }
  98. _, err := topology.ReplicatedDelete(vs.GetMaster(), vs.store, volumeId, n, r)
  99. if err == nil {
  100. m := make(map[string]int64)
  101. m["size"] = count
  102. writeJsonQuiet(w, r, http.StatusAccepted, m)
  103. } else {
  104. writeJsonError(w, r, http.StatusInternalServerError, fmt.Errorf("Deletion Failed: %v", err))
  105. }
  106. }
  107. func setEtag(w http.ResponseWriter, etag string) {
  108. if etag != "" {
  109. if strings.HasPrefix(etag, "\"") {
  110. w.Header().Set("ETag", etag)
  111. } else {
  112. w.Header().Set("ETag", "\""+etag+"\"")
  113. }
  114. }
  115. }