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.

133 lines
3.5 KiB

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