2016-06-03 03:05:34 +00:00
|
|
|
package weed_server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2017-01-08 01:16:29 +00:00
|
|
|
"strconv"
|
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
|
|
|
|
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"
|
2016-06-03 03:05:34 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
|
|
|
)
|
|
|
|
|
2016-06-03 03:44:50 +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"`
|
|
|
|
}
|
|
|
|
|
2016-06-08 07:46:14 +00:00
|
|
|
func (fs *FilerServer) queryFileInfoByPath(w http.ResponseWriter, r *http.Request, path string) (fileId, urlLocation string, err error) {
|
2018-05-14 06:56:16 +00:00
|
|
|
var entry *filer2.Entry
|
2018-05-26 10:49:46 +00:00
|
|
|
if entry, err = fs.filer.FindEntry(filer2.FullPath(path)); err != nil {
|
2016-06-08 07:46:14 +00:00
|
|
|
glog.V(0).Infoln("failing to find path in filer store", path, err.Error())
|
|
|
|
writeJsonError(w, r, http.StatusInternalServerError, err)
|
2018-05-26 10:49:46 +00:00
|
|
|
} else {
|
2018-05-16 07:08:44 +00:00
|
|
|
fileId = entry.Chunks[0].FileId
|
2016-06-08 07:46:14 +00:00
|
|
|
urlLocation, err = operation.LookupFileId(fs.getMasterNode(), fileId)
|
|
|
|
if err != nil {
|
|
|
|
glog.V(1).Infoln("operation LookupFileId %s failed, err is %s", fileId, err.Error())
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FilerServer) assignNewFileInfo(w http.ResponseWriter, r *http.Request, replication, collection string) (fileId, urlLocation string, 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"),
|
|
|
|
}
|
|
|
|
assignResult, ae := operation.Assign(fs.getMasterNode(), ar)
|
2016-06-08 07:46:14 +00:00
|
|
|
if ae != nil {
|
|
|
|
glog.V(0).Infoln("failing to assign a file id", ae.Error())
|
|
|
|
writeJsonError(w, r, http.StatusInternalServerError, ae)
|
|
|
|
err = ae
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fileId = assignResult.Fid
|
|
|
|
urlLocation = "http://" + assignResult.Url + "/" + assignResult.Fid
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-06-03 03:05:34 +00:00
|
|
|
func (fs *FilerServer) PostHandler(w http.ResponseWriter, r *http.Request) {
|
2016-08-05 22:01:30 +00:00
|
|
|
|
2016-06-03 03:05:34 +00:00
|
|
|
query := r.URL.Query()
|
|
|
|
replication := query.Get("replication")
|
|
|
|
if replication == "" {
|
|
|
|
replication = fs.defaultReplication
|
|
|
|
}
|
|
|
|
collection := query.Get("collection")
|
|
|
|
if collection == "" {
|
|
|
|
collection = fs.collection
|
|
|
|
}
|
|
|
|
|
2016-08-05 22:01:30 +00:00
|
|
|
if autoChunked := fs.autoChunk(w, r, replication, collection); autoChunked {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-06-08 07:46:14 +00:00
|
|
|
var fileId, urlLocation string
|
2016-06-03 03:05:34 +00:00
|
|
|
var err error
|
2016-06-08 04:30:20 +00:00
|
|
|
|
|
|
|
if strings.HasPrefix(r.Header.Get("Content-Type"), "multipart/form-data; boundary=") {
|
2016-06-08 07:46:14 +00:00
|
|
|
fileId, urlLocation, err = fs.multipartUploadAnalyzer(w, r, replication, collection)
|
|
|
|
if err != nil {
|
2016-06-03 03:05:34 +00:00
|
|
|
return
|
|
|
|
}
|
2016-06-08 07:46:14 +00:00
|
|
|
} else {
|
|
|
|
fileId, urlLocation, err = fs.monolithicUploadAnalyzer(w, r, replication, collection)
|
|
|
|
if err != nil {
|
2016-06-03 03:05:34 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
u, _ := url.Parse(urlLocation)
|
2016-08-05 22:01:30 +00:00
|
|
|
|
|
|
|
// 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)
|
2016-08-05 22:01:30 +00:00
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
resp, do_err := util.Do(request)
|
|
|
|
if do_err != nil {
|
|
|
|
glog.V(0).Infoln("failing to connect to volume server", r.RequestURI, do_err.Error())
|
|
|
|
writeJsonError(w, r, http.StatusInternalServerError, do_err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
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
|
|
|
|
}
|
|
|
|
path := r.URL.Path
|
|
|
|
if strings.HasSuffix(path, "/") {
|
|
|
|
if ret.Name != "" {
|
|
|
|
path += ret.Name
|
|
|
|
} else {
|
2016-06-07 03:45:00 +00:00
|
|
|
operation.DeleteFile(fs.getMasterNode(), fileId, fs.jwt(fileId)) //clean up
|
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
|
|
|
|
}
|
|
|
|
}
|
2016-06-03 07:24:55 +00:00
|
|
|
|
|
|
|
// also delete the old fid unless PUT operation
|
|
|
|
if r.Method != "PUT" {
|
2018-05-26 10:49:46 +00:00
|
|
|
if entry, err := fs.filer.FindEntry(filer2.FullPath(path)); err == nil {
|
2018-05-16 07:08:44 +00:00
|
|
|
oldFid := entry.Chunks[0].FileId
|
2016-06-07 03:45:00 +00:00
|
|
|
operation.DeleteFile(fs.getMasterNode(), oldFid, fs.jwt(oldFid))
|
2018-05-27 18:52:15 +00:00
|
|
|
} else if err != nil && err != filer2.ErrNotFound {
|
2016-09-08 03:35:54 +00:00
|
|
|
glog.V(0).Infof("error %v occur when finding %s in filer store", err, path)
|
2016-06-03 07:24:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-03 03:05:34 +00:00
|
|
|
glog.V(4).Infoln("saving", path, "=>", fileId)
|
2018-05-14 06:56:16 +00:00
|
|
|
entry := &filer2.Entry{
|
|
|
|
FullPath: filer2.FullPath(path),
|
|
|
|
Attr: filer2.Attr{
|
|
|
|
Mode: 0660,
|
|
|
|
},
|
2018-05-16 07:08:44 +00:00
|
|
|
Chunks: []*filer_pb.FileChunk{{
|
|
|
|
FileId: fileId,
|
|
|
|
Size: uint64(r.ContentLength),
|
2018-05-16 07:54:44 +00:00
|
|
|
Mtime: time.Now().UnixNano(),
|
2018-05-14 06:56:16 +00:00
|
|
|
}},
|
|
|
|
}
|
|
|
|
if db_err := fs.filer.CreateEntry(entry); db_err != nil {
|
2016-06-07 03:45:00 +00:00
|
|
|
operation.DeleteFile(fs.getMasterNode(), fileId, fs.jwt(fileId)) //clean up
|
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
|
|
|
|
}
|
2016-06-03 03:44:50 +00:00
|
|
|
|
|
|
|
reply := FilerPostResult{
|
|
|
|
Name: ret.Name,
|
|
|
|
Size: ret.Size,
|
|
|
|
Error: ret.Error,
|
|
|
|
Fid: fileId,
|
|
|
|
Url: urlLocation,
|
|
|
|
}
|
|
|
|
writeJsonQuiet(w, r, http.StatusCreated, reply)
|
2016-06-03 03:05:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// curl -X DELETE http://localhost:8888/path/to
|
|
|
|
func (fs *FilerServer) DeleteHandler(w http.ResponseWriter, r *http.Request) {
|
2018-05-14 06:56:16 +00:00
|
|
|
|
|
|
|
entry, err := fs.filer.DeleteEntry(filer2.FullPath(r.URL.Path))
|
|
|
|
if err != nil {
|
2016-06-03 03:05:34 +00:00
|
|
|
glog.V(4).Infoln("deleting", r.URL.Path, ":", err.Error())
|
|
|
|
writeJsonError(w, r, http.StatusInternalServerError, err)
|
2018-05-14 06:56:16 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if entry != nil && !entry.IsDirectory() {
|
|
|
|
for _, chunk := range entry.Chunks {
|
2018-05-16 07:08:44 +00:00
|
|
|
oldFid := chunk.FileId
|
2018-05-14 06:56:16 +00:00
|
|
|
operation.DeleteFile(fs.getMasterNode(), oldFid, fs.jwt(oldFid))
|
|
|
|
}
|
2016-06-03 03:05:34 +00:00
|
|
|
}
|
2018-05-14 06:56:16 +00:00
|
|
|
|
|
|
|
writeJsonQuiet(w, r, http.StatusAccepted, map[string]string{"error": ""})
|
2016-06-03 03:05:34 +00:00
|
|
|
}
|