volume_server_handlers_write.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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, contentMd5)
  41. if writeError != nil {
  42. writeJsonError(w, r, http.StatusInternalServerError, writeError)
  43. }
  44. // http 204 status code does not allow body
  45. if writeError == nil && isUnchanged {
  46. setEtag(w, reqNeedle.Etag())
  47. w.WriteHeader(http.StatusNoContent)
  48. return
  49. }
  50. httpStatus := http.StatusCreated
  51. if reqNeedle.HasName() {
  52. ret.Name = string(reqNeedle.Name)
  53. }
  54. ret.Size = uint32(originalSize)
  55. ret.ETag = reqNeedle.Etag()
  56. ret.Mime = string(reqNeedle.Mime)
  57. setEtag(w, ret.ETag)
  58. w.Header().Set("Content-MD5", contentMd5)
  59. writeJsonQuiet(w, r, httpStatus, ret)
  60. }
  61. func (vs *VolumeServer) DeleteHandler(w http.ResponseWriter, r *http.Request) {
  62. n := new(needle.Needle)
  63. vid, fid, _, _, _ := parseURLPath(r.URL.Path)
  64. volumeId, _ := needle.NewVolumeId(vid)
  65. n.ParsePath(fid)
  66. if !vs.maybeCheckJwtAuthorization(r, vid, fid, true) {
  67. writeJsonError(w, r, http.StatusUnauthorized, errors.New("wrong jwt"))
  68. return
  69. }
  70. // glog.V(2).Infof("volume %s deleting %s", vid, n)
  71. cookie := n.Cookie
  72. ecVolume, hasEcVolume := vs.store.FindEcVolume(volumeId)
  73. if hasEcVolume {
  74. count, err := vs.store.DeleteEcShardNeedle(ecVolume, n, cookie)
  75. writeDeleteResult(err, count, w, r)
  76. return
  77. }
  78. _, ok := vs.store.ReadVolumeNeedle(volumeId, n, nil, nil)
  79. if ok != nil {
  80. m := make(map[string]uint32)
  81. m["size"] = 0
  82. writeJsonQuiet(w, r, http.StatusNotFound, m)
  83. return
  84. }
  85. if n.Cookie != cookie {
  86. glog.V(0).Infoln("delete", r.URL.Path, "with unmaching cookie from ", r.RemoteAddr, "agent", r.UserAgent())
  87. writeJsonError(w, r, http.StatusBadRequest, errors.New("File Random Cookie does not match."))
  88. return
  89. }
  90. count := int64(n.Size)
  91. if n.IsChunkedManifest() {
  92. chunkManifest, e := operation.LoadChunkManifest(n.Data, n.IsCompressed())
  93. if e != nil {
  94. writeJsonError(w, r, http.StatusInternalServerError, fmt.Errorf("Load chunks manifest error: %v", e))
  95. return
  96. }
  97. // make sure all chunks had deleted before delete manifest
  98. if e := chunkManifest.DeleteChunks(vs.GetMaster, false, vs.grpcDialOption); e != nil {
  99. writeJsonError(w, r, http.StatusInternalServerError, fmt.Errorf("Delete chunks error: %v", e))
  100. return
  101. }
  102. count = chunkManifest.Size
  103. }
  104. n.LastModified = uint64(time.Now().Unix())
  105. if len(r.FormValue("ts")) > 0 {
  106. modifiedTime, err := strconv.ParseInt(r.FormValue("ts"), 10, 64)
  107. if err == nil {
  108. n.LastModified = uint64(modifiedTime)
  109. }
  110. }
  111. _, err := topology.ReplicatedDelete(vs.GetMaster, vs.grpcDialOption, vs.store, volumeId, n, r)
  112. writeDeleteResult(err, count, w, r)
  113. }
  114. func writeDeleteResult(err error, count int64, w http.ResponseWriter, r *http.Request) {
  115. if err == nil {
  116. m := make(map[string]int64)
  117. m["size"] = count
  118. writeJsonQuiet(w, r, http.StatusAccepted, m)
  119. } else {
  120. writeJsonError(w, r, http.StatusInternalServerError, fmt.Errorf("Deletion Failed: %v", err))
  121. }
  122. }
  123. func setEtag(w http.ResponseWriter, etag string) {
  124. if etag != "" {
  125. if strings.HasPrefix(etag, "\"") {
  126. w.Header().Set("ETag", etag)
  127. } else {
  128. w.Header().Set("ETag", "\""+etag+"\"")
  129. }
  130. }
  131. }
  132. func getEtag(resp *http.Response) (etag string) {
  133. etag = resp.Header.Get("ETag")
  134. if strings.HasPrefix(etag, "\"") && strings.HasSuffix(etag, "\"") {
  135. return etag[1 : len(etag)-1]
  136. }
  137. return
  138. }