filer_server_handlers_write.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package weed_server
  2. import (
  3. "context"
  4. "net/http"
  5. "os"
  6. "strings"
  7. "time"
  8. "github.com/chrislusf/seaweedfs/weed/util/log"
  9. "github.com/chrislusf/seaweedfs/weed/operation"
  10. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  11. "github.com/chrislusf/seaweedfs/weed/security"
  12. "github.com/chrislusf/seaweedfs/weed/stats"
  13. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  14. "github.com/chrislusf/seaweedfs/weed/util"
  15. )
  16. var (
  17. OS_UID = uint32(os.Getuid())
  18. OS_GID = uint32(os.Getgid())
  19. )
  20. type FilerPostResult struct {
  21. Name string `json:"name,omitempty"`
  22. Size int64 `json:"size,omitempty"`
  23. Error string `json:"error,omitempty"`
  24. Fid string `json:"fid,omitempty"`
  25. Url string `json:"url,omitempty"`
  26. }
  27. func (fs *FilerServer) assignNewFileInfo(so *operation.StorageOption) (fileId, urlLocation string, auth security.EncodedJwt, err error) {
  28. stats.FilerRequestCounter.WithLabelValues("assign").Inc()
  29. start := time.Now()
  30. defer func() { stats.FilerRequestHistogram.WithLabelValues("assign").Observe(time.Since(start).Seconds()) }()
  31. ar, altRequest := so.ToAssignRequests(1)
  32. assignResult, ae := operation.Assign(fs.filer.GetMaster(), fs.grpcDialOption, ar, altRequest)
  33. if ae != nil {
  34. log.Errorf("failing to assign a file id: %v", ae)
  35. err = ae
  36. return
  37. }
  38. fileId = assignResult.Fid
  39. urlLocation = "http://" + assignResult.Url + "/" + assignResult.Fid
  40. if so.Fsync {
  41. urlLocation += "?fsync=true"
  42. }
  43. auth = assignResult.Auth
  44. return
  45. }
  46. func (fs *FilerServer) PostHandler(w http.ResponseWriter, r *http.Request) {
  47. ctx := context.Background()
  48. query := r.URL.Query()
  49. so := fs.detectStorageOption0(r.RequestURI,
  50. query.Get("collection"),
  51. query.Get("replication"),
  52. query.Get("ttl"),
  53. query.Get("dataCenter"),
  54. query.Get("rack"),
  55. )
  56. fs.autoChunk(ctx, w, r, so)
  57. }
  58. // curl -X DELETE http://localhost:8888/path/to
  59. // curl -X DELETE http://localhost:8888/path/to?recursive=true
  60. // curl -X DELETE http://localhost:8888/path/to?recursive=true&ignoreRecursiveError=true
  61. // curl -X DELETE http://localhost:8888/path/to?recursive=true&skipChunkDeletion=true
  62. func (fs *FilerServer) DeleteHandler(w http.ResponseWriter, r *http.Request) {
  63. isRecursive := r.FormValue("recursive") == "true"
  64. if !isRecursive && fs.option.recursiveDelete {
  65. if r.FormValue("recursive") != "false" {
  66. isRecursive = true
  67. }
  68. }
  69. ignoreRecursiveError := r.FormValue("ignoreRecursiveError") == "true"
  70. skipChunkDeletion := r.FormValue("skipChunkDeletion") == "true"
  71. objectPath := r.URL.Path
  72. if len(r.URL.Path) > 1 && strings.HasSuffix(objectPath, "/") {
  73. objectPath = objectPath[0 : len(objectPath)-1]
  74. }
  75. err := fs.filer.DeleteEntryMetaAndData(context.Background(), util.FullPath(objectPath), isRecursive, ignoreRecursiveError, !skipChunkDeletion, false, nil)
  76. if err != nil {
  77. log.Debug("deleting", objectPath, ":", err.Error())
  78. httpStatus := http.StatusInternalServerError
  79. if err == filer_pb.ErrNotFound {
  80. httpStatus = http.StatusNotFound
  81. }
  82. writeJsonError(w, r, httpStatus, err)
  83. return
  84. }
  85. w.WriteHeader(http.StatusNoContent)
  86. }
  87. func (fs *FilerServer) detectStorageOption(requestURI, qCollection, qReplication string, ttlSeconds int32, dataCenter, rack string) *operation.StorageOption {
  88. collection := util.Nvl(qCollection, fs.option.Collection)
  89. replication := util.Nvl(qReplication, fs.option.DefaultReplication)
  90. // required by buckets folder
  91. bucketDefaultReplication, fsync := "", false
  92. if strings.HasPrefix(requestURI, fs.filer.DirBucketsPath+"/") {
  93. bucketAndObjectKey := requestURI[len(fs.filer.DirBucketsPath)+1:]
  94. t := strings.Index(bucketAndObjectKey, "/")
  95. if t < 0 {
  96. collection = bucketAndObjectKey
  97. }
  98. if t > 0 {
  99. collection = bucketAndObjectKey[:t]
  100. }
  101. bucketDefaultReplication, fsync = fs.filer.ReadBucketOption(collection)
  102. }
  103. if replication == "" {
  104. replication = bucketDefaultReplication
  105. }
  106. rule := fs.filer.FilerConf.MatchStorageRule(requestURI)
  107. if ttlSeconds == 0 {
  108. ttl, err := needle.ReadTTL(rule.GetTtl())
  109. if err != nil {
  110. log.Errorf("fail to parse %s ttl setting %s: %v", rule.LocationPrefix, rule.Ttl, err)
  111. }
  112. ttlSeconds = int32(ttl.Minutes()) * 60
  113. }
  114. return &operation.StorageOption{
  115. Replication: util.Nvl(replication, rule.Replication),
  116. Collection: util.Nvl(collection, rule.Collection),
  117. DataCenter: util.Nvl(dataCenter, fs.option.DataCenter),
  118. Rack: util.Nvl(rack, fs.option.Rack),
  119. TtlSeconds: ttlSeconds,
  120. Fsync: fsync || rule.Fsync,
  121. }
  122. }
  123. func (fs *FilerServer) detectStorageOption0(requestURI, qCollection, qReplication string, qTtl string, dataCenter, rack string) *operation.StorageOption {
  124. ttl, err := needle.ReadTTL(qTtl)
  125. if err != nil {
  126. log.Errorf("fail to parse ttl %s: %v", qTtl, err)
  127. }
  128. return fs.detectStorageOption(requestURI, qCollection, qReplication, int32(ttl.Minutes())*60, dataCenter, rack)
  129. }