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.

166 lines
4.5 KiB

9 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
9 years ago
9 years ago
9 years ago
5 years ago
5 years ago
  1. package weed_server
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "net/http"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "github.com/seaweedfs/seaweedfs/weed/glog"
  11. "github.com/seaweedfs/seaweedfs/weed/operation"
  12. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  13. "github.com/seaweedfs/seaweedfs/weed/topology"
  14. )
  15. func (vs *VolumeServer) PostHandler(w http.ResponseWriter, r *http.Request) {
  16. if e := r.ParseForm(); e != nil {
  17. glog.V(0).Infoln("form parse error:", e)
  18. writeJsonError(w, r, http.StatusBadRequest, e)
  19. return
  20. }
  21. vid, fid, _, _, _ := parseURLPath(r.URL.Path)
  22. volumeId, ve := needle.NewVolumeId(vid)
  23. if ve != nil {
  24. glog.V(0).Infoln("NewVolumeId error:", ve)
  25. writeJsonError(w, r, http.StatusBadRequest, ve)
  26. return
  27. }
  28. if !vs.maybeCheckJwtAuthorization(r, vid, fid, true) {
  29. writeJsonError(w, r, http.StatusUnauthorized, errors.New("wrong jwt"))
  30. return
  31. }
  32. bytesBuffer := bufPool.Get().(*bytes.Buffer)
  33. defer bufPool.Put(bytesBuffer)
  34. reqNeedle, originalSize, contentMd5, ne := needle.CreateNeedleFromRequest(r, vs.FixJpgOrientation, vs.fileSizeLimitBytes, bytesBuffer)
  35. if ne != nil {
  36. writeJsonError(w, r, http.StatusBadRequest, ne)
  37. return
  38. }
  39. ret := operation.UploadResult{}
  40. isUnchanged, writeError := topology.ReplicatedWrite(vs.GetMaster, vs.grpcDialOption, vs.store, volumeId, reqNeedle, r)
  41. // http 204 status code does not allow body
  42. if writeError == nil && isUnchanged {
  43. setEtag(w, reqNeedle.Etag())
  44. w.WriteHeader(http.StatusNoContent)
  45. return
  46. }
  47. httpStatus := http.StatusCreated
  48. if writeError != nil {
  49. httpStatus = http.StatusInternalServerError
  50. ret.Error = writeError.Error()
  51. }
  52. if reqNeedle.HasName() {
  53. ret.Name = string(reqNeedle.Name)
  54. }
  55. ret.Size = uint32(originalSize)
  56. ret.ETag = reqNeedle.Etag()
  57. ret.Mime = string(reqNeedle.Mime)
  58. setEtag(w, ret.ETag)
  59. w.Header().Set("Content-MD5", contentMd5)
  60. writeJsonQuiet(w, r, httpStatus, ret)
  61. }
  62. func (vs *VolumeServer) DeleteHandler(w http.ResponseWriter, r *http.Request) {
  63. n := new(needle.Needle)
  64. vid, fid, _, _, _ := parseURLPath(r.URL.Path)
  65. volumeId, _ := needle.NewVolumeId(vid)
  66. n.ParsePath(fid)
  67. if !vs.maybeCheckJwtAuthorization(r, vid, fid, true) {
  68. writeJsonError(w, r, http.StatusUnauthorized, errors.New("wrong jwt"))
  69. return
  70. }
  71. // glog.V(2).Infof("volume %s deleting %s", vid, n)
  72. cookie := n.Cookie
  73. ecVolume, hasEcVolume := vs.store.FindEcVolume(volumeId)
  74. if hasEcVolume {
  75. count, err := vs.store.DeleteEcShardNeedle(ecVolume, n, cookie)
  76. writeDeleteResult(err, count, w, r)
  77. return
  78. }
  79. _, ok := vs.store.ReadVolumeNeedle(volumeId, n, nil, nil)
  80. if ok != nil {
  81. m := make(map[string]uint32)
  82. m["size"] = 0
  83. writeJsonQuiet(w, r, http.StatusNotFound, m)
  84. return
  85. }
  86. if n.Cookie != cookie {
  87. glog.V(0).Infoln("delete", r.URL.Path, "with unmaching cookie from ", r.RemoteAddr, "agent", r.UserAgent())
  88. writeJsonError(w, r, http.StatusBadRequest, errors.New("File Random Cookie does not match."))
  89. return
  90. }
  91. count := int64(n.Size)
  92. if n.IsChunkedManifest() {
  93. chunkManifest, e := operation.LoadChunkManifest(n.Data, n.IsCompressed())
  94. if e != nil {
  95. writeJsonError(w, r, http.StatusInternalServerError, fmt.Errorf("Load chunks manifest error: %v", e))
  96. return
  97. }
  98. // make sure all chunks had deleted before delete manifest
  99. if e := chunkManifest.DeleteChunks(vs.GetMaster, false, vs.grpcDialOption); e != nil {
  100. writeJsonError(w, r, http.StatusInternalServerError, fmt.Errorf("Delete chunks error: %v", e))
  101. return
  102. }
  103. count = chunkManifest.Size
  104. }
  105. n.LastModified = uint64(time.Now().Unix())
  106. if len(r.FormValue("ts")) > 0 {
  107. modifiedTime, err := strconv.ParseInt(r.FormValue("ts"), 10, 64)
  108. if err == nil {
  109. n.LastModified = uint64(modifiedTime)
  110. }
  111. }
  112. _, err := topology.ReplicatedDelete(vs.GetMaster, vs.grpcDialOption, vs.store, volumeId, n, r)
  113. writeDeleteResult(err, count, w, r)
  114. }
  115. func writeDeleteResult(err error, count int64, w http.ResponseWriter, r *http.Request) {
  116. if err == nil {
  117. m := make(map[string]int64)
  118. m["size"] = count
  119. writeJsonQuiet(w, r, http.StatusAccepted, m)
  120. } else {
  121. writeJsonError(w, r, http.StatusInternalServerError, fmt.Errorf("Deletion Failed: %v", err))
  122. }
  123. }
  124. func setEtag(w http.ResponseWriter, etag string) {
  125. if etag != "" {
  126. if strings.HasPrefix(etag, "\"") {
  127. w.Header().Set("ETag", etag)
  128. } else {
  129. w.Header().Set("ETag", "\""+etag+"\"")
  130. }
  131. }
  132. }
  133. func getEtag(resp *http.Response) (etag string) {
  134. etag = resp.Header.Get("ETag")
  135. if strings.HasPrefix(etag, "\"") && strings.HasSuffix(etag, "\"") {
  136. return etag[1 : len(etag)-1]
  137. }
  138. return
  139. }