volume_server_handlers_write.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package weed_server
  2. import (
  3. "errors"
  4. "fmt"
  5. "net/http"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "github.com/seaweedfs/seaweedfs/weed/glog"
  10. "github.com/seaweedfs/seaweedfs/weed/operation"
  11. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  12. "github.com/seaweedfs/seaweedfs/weed/topology"
  13. "github.com/seaweedfs/seaweedfs/weed/util/buffer_pool"
  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 := buffer_pool.SyncPoolGetBuffer()
  33. defer buffer_pool.SyncPoolPutBuffer(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, contentMd5)
  41. if writeError != nil {
  42. writeJsonError(w, r, http.StatusInternalServerError, writeError)
  43. return
  44. }
  45. // http 204 status code does not allow body
  46. if writeError == nil && isUnchanged {
  47. SetEtag(w, reqNeedle.Etag())
  48. w.WriteHeader(http.StatusNoContent)
  49. return
  50. }
  51. httpStatus := http.StatusCreated
  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. }