2016-06-03 03:05:34 +00:00
|
|
|
package weed_server
|
|
|
|
|
|
|
|
import (
|
2019-03-15 22:26:09 +00:00
|
|
|
"context"
|
2021-06-04 08:03:41 +00:00
|
|
|
"errors"
|
2016-06-03 03:05:34 +00:00
|
|
|
"net/http"
|
2019-02-15 08:09:19 +00:00
|
|
|
"os"
|
2017-01-08 14:34:26 +00:00
|
|
|
"strings"
|
2018-05-28 06:59:49 +00:00
|
|
|
"time"
|
2017-01-08 01:16:29 +00:00
|
|
|
|
2016-06-03 03:05:34 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/operation"
|
2018-05-27 18:52:26 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
2019-02-15 08:09:19 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/security"
|
2019-06-15 19:21:44 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/stats"
|
2020-03-09 07:16:10 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/storage/needle"
|
2016-06-03 03:05:34 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
2018-07-22 00:47:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
OS_UID = uint32(os.Getuid())
|
|
|
|
OS_GID = uint32(os.Getgid())
|
2021-06-04 08:03:41 +00:00
|
|
|
|
|
|
|
ErrReadOnly = errors.New("read only")
|
2016-06-03 03:05:34 +00:00
|
|
|
)
|
|
|
|
|
2016-06-03 03:44:50 +00:00
|
|
|
type FilerPostResult struct {
|
|
|
|
Name string `json:"name,omitempty"`
|
2020-02-04 01:04:06 +00:00
|
|
|
Size int64 `json:"size,omitempty"`
|
2016-06-03 03:44:50 +00:00
|
|
|
Error string `json:"error,omitempty"`
|
|
|
|
Fid string `json:"fid,omitempty"`
|
|
|
|
Url string `json:"url,omitempty"`
|
|
|
|
}
|
|
|
|
|
2020-11-16 00:58:48 +00:00
|
|
|
func (fs *FilerServer) assignNewFileInfo(so *operation.StorageOption) (fileId, urlLocation string, auth security.EncodedJwt, err error) {
|
2019-06-23 05:53:52 +00:00
|
|
|
|
|
|
|
stats.FilerRequestCounter.WithLabelValues("assign").Inc()
|
|
|
|
start := time.Now()
|
|
|
|
defer func() { stats.FilerRequestHistogram.WithLabelValues("assign").Observe(time.Since(start).Seconds()) }()
|
|
|
|
|
2020-11-16 00:58:48 +00:00
|
|
|
ar, altRequest := so.ToAssignRequests(1)
|
2018-07-21 17:39:02 +00:00
|
|
|
|
2021-02-18 04:55:55 +00:00
|
|
|
assignResult, ae := operation.Assign(fs.filer.GetMaster, fs.grpcDialOption, ar, altRequest)
|
2016-06-08 07:46:14 +00:00
|
|
|
if ae != nil {
|
2018-07-21 17:39:02 +00:00
|
|
|
glog.Errorf("failing to assign a file id: %v", ae)
|
2016-06-08 07:46:14 +00:00
|
|
|
err = ae
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fileId = assignResult.Fid
|
|
|
|
urlLocation = "http://" + assignResult.Url + "/" + assignResult.Fid
|
2020-11-15 22:41:56 +00:00
|
|
|
if so.Fsync {
|
2020-04-12 06:37:10 +00:00
|
|
|
urlLocation += "?fsync=true"
|
|
|
|
}
|
2019-02-15 08:09:19 +00:00
|
|
|
auth = assignResult.Auth
|
2016-06-08 07:46:14 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-31 04:07:34 +00:00
|
|
|
func (fs *FilerServer) PostHandler(w http.ResponseWriter, r *http.Request, contentLength int64) {
|
2016-08-05 22:01:30 +00:00
|
|
|
|
2019-03-15 22:55:34 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2016-06-03 03:05:34 +00:00
|
|
|
query := r.URL.Query()
|
2021-06-04 08:03:41 +00:00
|
|
|
so, err := fs.detectStorageOption0(r.RequestURI,
|
2020-11-16 00:58:48 +00:00
|
|
|
query.Get("collection"),
|
|
|
|
query.Get("replication"),
|
|
|
|
query.Get("ttl"),
|
2020-12-13 20:05:31 +00:00
|
|
|
query.Get("disk"),
|
2020-11-16 00:58:48 +00:00
|
|
|
query.Get("dataCenter"),
|
|
|
|
query.Get("rack"),
|
|
|
|
)
|
2021-06-04 08:03:41 +00:00
|
|
|
if err != nil {
|
|
|
|
if err == ErrReadOnly {
|
|
|
|
w.WriteHeader(http.StatusInsufficientStorage)
|
|
|
|
} else {
|
|
|
|
glog.V(1).Infoln("post", r.RequestURI, ":", err.Error())
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2020-11-15 22:41:56 +00:00
|
|
|
|
2021-03-31 04:07:34 +00:00
|
|
|
fs.autoChunk(ctx, w, r, contentLength, so)
|
2020-11-30 10:45:00 +00:00
|
|
|
util.CloseRequest(r)
|
2019-06-23 05:53:52 +00:00
|
|
|
|
2016-06-03 03:05:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// curl -X DELETE http://localhost:8888/path/to
|
2018-07-25 05:33:26 +00:00
|
|
|
// curl -X DELETE http://localhost:8888/path/to?recursive=true
|
2019-09-12 03:26:20 +00:00
|
|
|
// curl -X DELETE http://localhost:8888/path/to?recursive=true&ignoreRecursiveError=true
|
2019-12-11 22:58:22 +00:00
|
|
|
// curl -X DELETE http://localhost:8888/path/to?recursive=true&skipChunkDeletion=true
|
2016-06-03 03:05:34 +00:00
|
|
|
func (fs *FilerServer) DeleteHandler(w http.ResponseWriter, r *http.Request) {
|
2018-05-14 06:56:16 +00:00
|
|
|
|
2018-07-25 05:33:26 +00:00
|
|
|
isRecursive := r.FormValue("recursive") == "true"
|
2019-12-31 19:52:54 +00:00
|
|
|
if !isRecursive && fs.option.recursiveDelete {
|
|
|
|
if r.FormValue("recursive") != "false" {
|
|
|
|
isRecursive = true
|
|
|
|
}
|
|
|
|
}
|
2019-09-12 03:26:20 +00:00
|
|
|
ignoreRecursiveError := r.FormValue("ignoreRecursiveError") == "true"
|
2019-12-11 22:58:22 +00:00
|
|
|
skipChunkDeletion := r.FormValue("skipChunkDeletion") == "true"
|
2018-07-25 05:33:26 +00:00
|
|
|
|
2020-06-22 17:01:00 +00:00
|
|
|
objectPath := r.URL.Path
|
|
|
|
if len(r.URL.Path) > 1 && strings.HasSuffix(objectPath, "/") {
|
|
|
|
objectPath = objectPath[0 : len(objectPath)-1]
|
|
|
|
}
|
|
|
|
|
2020-08-29 06:48:48 +00:00
|
|
|
err := fs.filer.DeleteEntryMetaAndData(context.Background(), util.FullPath(objectPath), isRecursive, ignoreRecursiveError, !skipChunkDeletion, false, nil)
|
2018-05-14 06:56:16 +00:00
|
|
|
if err != nil {
|
2020-06-22 17:01:00 +00:00
|
|
|
glog.V(1).Infoln("deleting", objectPath, ":", err.Error())
|
2019-12-19 05:04:40 +00:00
|
|
|
httpStatus := http.StatusInternalServerError
|
2020-03-08 01:01:39 +00:00
|
|
|
if err == filer_pb.ErrNotFound {
|
2021-02-03 08:40:31 +00:00
|
|
|
httpStatus = http.StatusNoContent
|
2019-12-19 05:04:40 +00:00
|
|
|
}
|
|
|
|
writeJsonError(w, r, httpStatus, err)
|
2018-05-14 06:56:16 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-22 01:49:47 +00:00
|
|
|
w.WriteHeader(http.StatusNoContent)
|
2016-06-03 03:05:34 +00:00
|
|
|
}
|
2020-02-25 06:28:45 +00:00
|
|
|
|
2021-06-04 08:03:41 +00:00
|
|
|
func (fs *FilerServer) detectStorageOption(requestURI, qCollection, qReplication string, ttlSeconds int32, diskType, dataCenter, rack string) (*operation.StorageOption, error) {
|
2020-11-16 00:58:48 +00:00
|
|
|
collection := util.Nvl(qCollection, fs.option.Collection)
|
|
|
|
replication := util.Nvl(qReplication, fs.option.DefaultReplication)
|
2020-02-25 06:28:45 +00:00
|
|
|
|
|
|
|
// required by buckets folder
|
2020-11-16 00:58:48 +00:00
|
|
|
bucketDefaultReplication, fsync := "", false
|
2020-02-25 06:28:45 +00:00
|
|
|
if strings.HasPrefix(requestURI, fs.filer.DirBucketsPath+"/") {
|
2020-12-11 07:50:32 +00:00
|
|
|
collection = fs.filer.DetectBucket(util.FullPath(requestURI))
|
2020-10-19 07:12:42 +00:00
|
|
|
bucketDefaultReplication, fsync = fs.filer.ReadBucketOption(collection)
|
|
|
|
}
|
|
|
|
if replication == "" {
|
|
|
|
replication = bucketDefaultReplication
|
2020-02-25 06:28:45 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 00:58:48 +00:00
|
|
|
rule := fs.filer.FilerConf.MatchStorageRule(requestURI)
|
|
|
|
|
2021-06-04 08:03:41 +00:00
|
|
|
if rule.ReadOnly {
|
|
|
|
return nil, ErrReadOnly
|
|
|
|
}
|
|
|
|
|
2020-11-16 00:58:48 +00:00
|
|
|
if ttlSeconds == 0 {
|
|
|
|
ttl, err := needle.ReadTTL(rule.GetTtl())
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("fail to parse %s ttl setting %s: %v", rule.LocationPrefix, rule.Ttl, err)
|
|
|
|
}
|
2020-11-16 00:59:28 +00:00
|
|
|
ttlSeconds = int32(ttl.Minutes()) * 60
|
2020-11-16 00:58:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &operation.StorageOption{
|
2020-11-17 09:00:02 +00:00
|
|
|
Replication: util.Nvl(replication, rule.Replication),
|
|
|
|
Collection: util.Nvl(collection, rule.Collection),
|
|
|
|
DataCenter: util.Nvl(dataCenter, fs.option.DataCenter),
|
|
|
|
Rack: util.Nvl(rack, fs.option.Rack),
|
|
|
|
TtlSeconds: ttlSeconds,
|
2020-12-16 17:10:14 +00:00
|
|
|
DiskType: util.Nvl(diskType, rule.DiskType),
|
2020-11-17 09:00:02 +00:00
|
|
|
Fsync: fsync || rule.Fsync,
|
|
|
|
VolumeGrowthCount: rule.VolumeGrowthCount,
|
2021-06-04 08:03:41 +00:00
|
|
|
}, nil
|
2020-11-16 00:58:48 +00:00
|
|
|
}
|
|
|
|
|
2021-06-04 08:03:41 +00:00
|
|
|
func (fs *FilerServer) detectStorageOption0(requestURI, qCollection, qReplication string, qTtl string, diskType string, dataCenter, rack string) (*operation.StorageOption, error) {
|
2020-11-16 00:58:48 +00:00
|
|
|
|
|
|
|
ttl, err := needle.ReadTTL(qTtl)
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("fail to parse ttl %s: %v", qTtl, err)
|
|
|
|
}
|
|
|
|
|
2020-12-13 19:59:32 +00:00
|
|
|
return fs.detectStorageOption(requestURI, qCollection, qReplication, int32(ttl.Minutes())*60, diskType, dataCenter, rack)
|
2020-02-25 06:28:45 +00:00
|
|
|
}
|