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.

132 lines
3.5 KiB

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