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.

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