seaweedfs/weed/server/filer_server_handlers_write.go

230 lines
6.5 KiB
Go
Raw Normal View History

2016-06-03 03:05:34 +00:00
package weed_server
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"net/url"
2019-02-15 08:09:19 +00:00
"os"
"strconv"
"strings"
2018-05-28 06:59:49 +00:00
"time"
2018-05-27 18:52:26 +00:00
"github.com/chrislusf/seaweedfs/weed/filer2"
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"
2016-06-03 03:05:34 +00:00
"github.com/chrislusf/seaweedfs/weed/util"
)
var (
OS_UID = uint32(os.Getuid())
OS_GID = uint32(os.Getgid())
2016-06-03 03:05:34 +00:00
)
type FilerPostResult struct {
Name string `json:"name,omitempty"`
Size uint32 `json:"size,omitempty"`
Error string `json:"error,omitempty"`
Fid string `json:"fid,omitempty"`
Url string `json:"url,omitempty"`
}
2019-02-15 08:09:19 +00:00
func (fs *FilerServer) assignNewFileInfo(w http.ResponseWriter, r *http.Request, replication, collection string, dataCenter string) (fileId, urlLocation string, auth security.EncodedJwt, err error) {
2016-06-26 02:50:18 +00:00
ar := &operation.VolumeAssignRequest{
Count: 1,
Replication: replication,
Collection: collection,
Ttl: r.URL.Query().Get("ttl"),
DataCenter: dataCenter,
2016-06-26 02:50:18 +00:00
}
2018-07-10 07:20:50 +00:00
var altRequest *operation.VolumeAssignRequest
if dataCenter != "" {
2018-07-10 07:20:50 +00:00
altRequest = &operation.VolumeAssignRequest{
Count: 1,
Replication: replication,
Collection: collection,
Ttl: r.URL.Query().Get("ttl"),
DataCenter: "",
}
}
2019-02-18 20:11:52 +00:00
assignResult, ae := operation.Assign(fs.filer.GetMaster(), fs.grpcDialOption, ar, altRequest)
2016-06-08 07:46:14 +00:00
if ae != nil {
glog.Errorf("failing to assign a file id: %v", ae)
2016-06-08 07:46:14 +00:00
writeJsonError(w, r, http.StatusInternalServerError, ae)
err = ae
return
}
fileId = assignResult.Fid
urlLocation = "http://" + assignResult.Url + "/" + assignResult.Fid
2019-02-15 08:09:19 +00:00
auth = assignResult.Auth
2016-06-08 07:46:14 +00:00
return
}
2016-06-03 03:05:34 +00:00
func (fs *FilerServer) PostHandler(w http.ResponseWriter, r *http.Request) {
2016-06-03 03:05:34 +00:00
query := r.URL.Query()
replication := query.Get("replication")
if replication == "" {
2018-07-07 09:18:47 +00:00
replication = fs.option.DefaultReplication
2016-06-03 03:05:34 +00:00
}
collection := query.Get("collection")
if collection == "" {
2018-07-07 09:18:47 +00:00
collection = fs.option.Collection
2016-06-03 03:05:34 +00:00
}
dataCenter := query.Get("dataCenter")
if dataCenter == "" {
dataCenter = fs.option.DataCenter
}
2016-06-03 03:05:34 +00:00
if autoChunked := fs.autoChunk(w, r, replication, collection, dataCenter); autoChunked {
return
}
2019-02-15 08:09:19 +00:00
fileId, urlLocation, auth, err := fs.assignNewFileInfo(w, r, replication, collection, dataCenter)
if err != nil || fileId == "" || urlLocation == "" {
glog.V(0).Infof("fail to allocate volume for %s, collection:%s, datacenter:%s", r.URL.Path, collection, dataCenter)
return
}
2018-09-12 07:46:12 +00:00
glog.V(4).Infof("write %s to %v", r.URL.Path, urlLocation)
2016-06-03 03:05:34 +00:00
u, _ := url.Parse(urlLocation)
// This allows a client to generate a chunk manifest and submit it to the filer -- it is a little off
// because they need to provide FIDs instead of file paths...
cm, _ := strconv.ParseBool(query.Get("cm"))
if cm {
q := u.Query()
q.Set("cm", "true")
u.RawQuery = q.Encode()
}
2016-06-03 03:05:34 +00:00
glog.V(4).Infoln("post to", u)
2018-07-22 00:54:14 +00:00
// send request to volume server
2016-06-03 03:05:34 +00:00
request := &http.Request{
Method: r.Method,
URL: u,
Proto: r.Proto,
ProtoMajor: r.ProtoMajor,
ProtoMinor: r.ProtoMinor,
Header: r.Header,
Body: r.Body,
Host: r.Host,
ContentLength: r.ContentLength,
}
2019-02-15 08:09:19 +00:00
if auth != "" {
request.Header.Set("Authorization", "BEARER "+string(auth))
}
2016-06-03 03:05:34 +00:00
resp, do_err := util.Do(request)
if do_err != nil {
glog.Errorf("failing to connect to volume server %s: %v, %+v", r.RequestURI, do_err, r.Method)
2016-06-03 03:05:34 +00:00
writeJsonError(w, r, http.StatusInternalServerError, do_err)
return
}
defer resp.Body.Close()
2018-09-09 23:25:43 +00:00
etag := resp.Header.Get("ETag")
2016-06-03 03:05:34 +00:00
resp_body, ra_err := ioutil.ReadAll(resp.Body)
if ra_err != nil {
glog.V(0).Infoln("failing to upload to volume server", r.RequestURI, ra_err.Error())
writeJsonError(w, r, http.StatusInternalServerError, ra_err)
return
}
glog.V(4).Infoln("post result", string(resp_body))
var ret operation.UploadResult
unmarshal_err := json.Unmarshal(resp_body, &ret)
if unmarshal_err != nil {
glog.V(0).Infoln("failing to read upload resonse", r.RequestURI, string(resp_body))
writeJsonError(w, r, http.StatusInternalServerError, unmarshal_err)
return
}
if ret.Error != "" {
glog.V(0).Infoln("failing to post to volume server", r.RequestURI, ret.Error)
writeJsonError(w, r, http.StatusInternalServerError, errors.New(ret.Error))
return
}
2018-07-22 00:54:14 +00:00
// find correct final path
2016-06-03 03:05:34 +00:00
path := r.URL.Path
if strings.HasSuffix(path, "/") {
if ret.Name != "" {
path += ret.Name
} else {
fs.filer.DeleteFileByFileId(fileId)
2016-06-03 03:05:34 +00:00
glog.V(0).Infoln("Can not to write to folder", path, "without a file name!")
writeJsonError(w, r, http.StatusInternalServerError,
errors.New("Can not to write to folder "+path+" without a file name"))
return
}
}
2018-07-22 00:54:14 +00:00
// update metadata in filer store
2018-12-17 08:20:00 +00:00
existingEntry, err := fs.filer.FindEntry(filer2.FullPath(path))
crTime := time.Now()
if err == nil && existingEntry != nil {
// glog.V(4).Infof("existing %s => %+v", path, existingEntry)
2018-12-17 08:20:00 +00:00
if existingEntry.IsDirectory() {
2018-12-17 08:21:34 +00:00
path += "/" + ret.Name
2018-12-17 08:20:00 +00:00
} else {
crTime = existingEntry.Crtime
}
}
2018-05-14 06:56:16 +00:00
entry := &filer2.Entry{
FullPath: filer2.FullPath(path),
Attr: filer2.Attr{
Mtime: time.Now(),
2018-12-17 08:20:00 +00:00
Crtime: crTime,
Mode: 0660,
Uid: OS_UID,
Gid: OS_GID,
Replication: replication,
Collection: collection,
TtlSec: int32(util.ParseInt(r.URL.Query().Get("ttl"), 0)),
2018-05-14 06:56:16 +00:00
},
Chunks: []*filer_pb.FileChunk{{
FileId: fileId,
Size: uint64(ret.Size),
2018-05-16 07:54:44 +00:00
Mtime: time.Now().UnixNano(),
2018-09-09 23:25:43 +00:00
ETag: etag,
2018-05-14 06:56:16 +00:00
}},
}
// glog.V(4).Infof("saving %s => %+v", path, entry)
2018-05-14 06:56:16 +00:00
if db_err := fs.filer.CreateEntry(entry); db_err != nil {
fs.filer.DeleteFileByFileId(fileId)
2016-06-03 03:05:34 +00:00
glog.V(0).Infof("failing to write %s to filer server : %v", path, db_err)
writeJsonError(w, r, http.StatusInternalServerError, db_err)
return
}
2018-07-22 00:54:14 +00:00
// send back post result
reply := FilerPostResult{
Name: ret.Name,
Size: ret.Size,
Error: ret.Error,
Fid: fileId,
Url: urlLocation,
}
2018-09-12 07:46:12 +00:00
setEtag(w, etag)
writeJsonQuiet(w, r, http.StatusCreated, reply)
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
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"
err := fs.filer.DeleteEntryMetaAndData(filer2.FullPath(r.URL.Path), isRecursive, true)
2018-05-14 06:56:16 +00:00
if err != nil {
2018-07-22 01:47:23 +00:00
glog.V(1).Infoln("deleting", r.URL.Path, ":", err.Error())
2016-06-03 03:05:34 +00:00
writeJsonError(w, r, http.StatusInternalServerError, 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
}