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.

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