2014-03-31 02:28:04 +08:00
|
|
|
package weed_server
|
|
|
|
|
|
|
|
import (
|
2016-01-13 16:58:07 +08:00
|
|
|
"bytes"
|
2014-03-31 02:28:04 +08:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2014-03-31 04:26:44 +08:00
|
|
|
"strconv"
|
2014-03-31 02:28:04 +08:00
|
|
|
"strings"
|
2014-10-27 02:34:55 +08:00
|
|
|
|
2015-04-17 03:18:06 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/go/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/go/operation"
|
2016-01-13 16:58:07 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/go/storage"
|
2015-04-17 03:18:06 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/go/util"
|
2014-10-27 02:34:55 +08:00
|
|
|
"github.com/syndtr/goleveldb/leveldb"
|
2014-03-31 02:28:04 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func (fs *FilerServer) filerHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
switch r.Method {
|
|
|
|
case "GET":
|
|
|
|
fs.GetOrHeadHandler(w, r, true)
|
|
|
|
case "HEAD":
|
|
|
|
fs.GetOrHeadHandler(w, r, false)
|
|
|
|
case "DELETE":
|
|
|
|
fs.DeleteHandler(w, r)
|
|
|
|
case "PUT":
|
|
|
|
fs.PostHandler(w, r)
|
|
|
|
case "POST":
|
|
|
|
fs.PostHandler(w, r)
|
|
|
|
}
|
|
|
|
}
|
2015-04-15 01:09:46 +08:00
|
|
|
|
|
|
|
// listDirectoryHandler lists directories and folers under a directory
|
|
|
|
// files are sorted by name and paginated via "lastFileName" and "limit".
|
|
|
|
// sub directories are listed on the first page, when "lastFileName"
|
|
|
|
// is empty.
|
2014-03-31 04:26:44 +08:00
|
|
|
func (fs *FilerServer) listDirectoryHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if !strings.HasSuffix(r.URL.Path, "/") {
|
|
|
|
return
|
|
|
|
}
|
2014-04-10 00:44:58 +08:00
|
|
|
dirlist, err := fs.filer.ListDirectories(r.URL.Path)
|
2014-03-31 04:26:44 +08:00
|
|
|
if err == leveldb.ErrNotFound {
|
|
|
|
glog.V(3).Infoln("Directory Not Found in db", r.URL.Path)
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
m := make(map[string]interface{})
|
|
|
|
m["Directory"] = r.URL.Path
|
2014-04-10 12:01:48 +08:00
|
|
|
lastFileName := r.FormValue("lastFileName")
|
2015-04-15 01:09:46 +08:00
|
|
|
if lastFileName == "" {
|
|
|
|
m["Subdirectories"] = dirlist
|
|
|
|
}
|
2014-03-31 04:26:44 +08:00
|
|
|
limit, limit_err := strconv.Atoi(r.FormValue("limit"))
|
|
|
|
if limit_err != nil {
|
|
|
|
limit = 100
|
|
|
|
}
|
2014-04-10 12:01:48 +08:00
|
|
|
m["Files"], _ = fs.filer.ListFiles(r.URL.Path, lastFileName, limit)
|
2015-01-08 16:19:32 +08:00
|
|
|
writeJsonQuiet(w, r, http.StatusOK, m)
|
2014-03-31 04:26:44 +08:00
|
|
|
}
|
2016-01-17 12:30:23 +08:00
|
|
|
|
2014-03-31 02:28:04 +08:00
|
|
|
func (fs *FilerServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request, isGetMethod bool) {
|
2014-03-31 04:26:44 +08:00
|
|
|
if strings.HasSuffix(r.URL.Path, "/") {
|
2015-04-14 14:38:46 +08:00
|
|
|
if fs.disableDirListing {
|
|
|
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
2014-03-31 04:26:44 +08:00
|
|
|
fs.listDirectoryHandler(w, r)
|
|
|
|
return
|
|
|
|
}
|
2016-01-17 12:30:23 +08:00
|
|
|
|
|
|
|
fileId, err := fs.filer.FindFile(r.URL.Path)
|
|
|
|
if err == leveldb.ErrNotFound {
|
|
|
|
glog.V(3).Infoln("Not found in db", r.URL.Path)
|
2014-03-31 02:28:04 +08:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
2016-01-17 12:30:23 +08:00
|
|
|
|
|
|
|
urlLocation, err := operation.LookupFileId(fs.master, fileId)
|
|
|
|
if err != nil {
|
|
|
|
glog.V(1).Infoln("operation LookupFileId %s failed, err is %s", fileId, err.Error())
|
2014-03-31 02:28:04 +08:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
2016-01-17 12:30:23 +08:00
|
|
|
urlString := urlLocation
|
2014-12-09 12:27:26 +08:00
|
|
|
if fs.redirectOnRead {
|
2014-12-09 12:34:27 +08:00
|
|
|
http.Redirect(w, r, urlString, http.StatusFound)
|
|
|
|
return
|
2014-12-09 12:27:26 +08:00
|
|
|
}
|
|
|
|
u, _ := url.Parse(urlString)
|
2014-03-31 02:28:04 +08: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,
|
|
|
|
}
|
|
|
|
glog.V(3).Infoln("retrieving from", u)
|
|
|
|
resp, do_err := util.Do(request)
|
|
|
|
if do_err != nil {
|
|
|
|
glog.V(0).Infoln("failing to connect to volume server", do_err.Error())
|
2015-01-08 16:19:32 +08:00
|
|
|
writeJsonError(w, r, http.StatusInternalServerError, do_err)
|
2014-03-31 02:28:04 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2014-07-22 05:25:51 +08:00
|
|
|
for k, v := range resp.Header {
|
|
|
|
w.Header()[k] = v
|
|
|
|
}
|
2014-08-26 03:02:04 +08:00
|
|
|
w.WriteHeader(resp.StatusCode)
|
2014-03-31 02:28:04 +08:00
|
|
|
io.Copy(w, resp.Body)
|
|
|
|
}
|
|
|
|
|
2016-01-13 16:58:07 +08:00
|
|
|
type analogueReader struct {
|
|
|
|
*bytes.Buffer
|
|
|
|
}
|
|
|
|
|
|
|
|
// So that it implements the io.ReadCloser interface
|
|
|
|
func (m analogueReader) Close() error { return nil }
|
|
|
|
|
2014-03-31 02:28:04 +08:00
|
|
|
func (fs *FilerServer) PostHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
query := r.URL.Query()
|
2014-12-09 12:27:26 +08:00
|
|
|
replication := query.Get("replication")
|
|
|
|
if replication == "" {
|
|
|
|
replication = fs.defaultReplication
|
|
|
|
}
|
2016-01-04 09:52:32 +08:00
|
|
|
collection := query.Get("collection")
|
|
|
|
if collection == "" {
|
|
|
|
collection = fs.collection
|
|
|
|
}
|
2016-01-13 16:58:07 +08:00
|
|
|
|
|
|
|
var fileId string
|
|
|
|
var err error
|
|
|
|
var urlLocation string
|
2016-01-17 12:30:23 +08:00
|
|
|
if r.Method == "PUT" {
|
|
|
|
buf, _ := ioutil.ReadAll(r.Body)
|
|
|
|
r.Body = analogueReader{bytes.NewBuffer(buf)}
|
|
|
|
fileName, _, _, _, _, _, _, pe := storage.ParseUpload(r)
|
|
|
|
if pe != nil {
|
|
|
|
glog.V(0).Infoln("failing to parse post body", pe.Error())
|
|
|
|
writeJsonError(w, r, http.StatusInternalServerError, pe)
|
2016-01-13 16:58:07 +08:00
|
|
|
return
|
|
|
|
}
|
2016-01-17 12:30:23 +08:00
|
|
|
//reconstruct http request body for following new request to volume server
|
|
|
|
r.Body = analogueReader{bytes.NewBuffer(buf)}
|
|
|
|
|
|
|
|
path := r.URL.Path
|
|
|
|
if strings.HasSuffix(path, "/") {
|
|
|
|
if fileName != "" {
|
|
|
|
path += fileName
|
|
|
|
}
|
2016-01-13 16:58:07 +08:00
|
|
|
}
|
2016-01-17 12:30:23 +08:00
|
|
|
|
|
|
|
if fileId, err = fs.filer.FindFile(path); err != nil && err != leveldb.ErrNotFound {
|
|
|
|
glog.V(0).Infoln("failing to find path in filer store", path, err.Error())
|
|
|
|
writeJsonError(w, r, http.StatusInternalServerError, err)
|
2016-01-13 16:58:07 +08:00
|
|
|
return
|
2016-01-17 12:30:23 +08:00
|
|
|
} else if fileId != "" && err == nil {
|
|
|
|
var le error
|
|
|
|
urlLocation, le = operation.LookupFileId(fs.master, fileId)
|
|
|
|
if le != nil {
|
|
|
|
glog.V(1).Infoln("operation LookupFileId %s failed, err is %s", fileId, le.Error())
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
2016-01-13 16:58:07 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assignResult, ae := operation.Assign(fs.master, 1, replication, collection, query.Get("ttl"))
|
|
|
|
if ae != nil {
|
|
|
|
glog.V(0).Infoln("failing to assign a file id", ae.Error())
|
|
|
|
writeJsonError(w, r, http.StatusInternalServerError, ae)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fileId = assignResult.Fid
|
2016-01-17 12:30:23 +08:00
|
|
|
urlLocation = "http://" + assignResult.Url + "/" + assignResult.Fid
|
2014-03-31 02:28:04 +08:00
|
|
|
}
|
|
|
|
|
2016-01-17 12:30:23 +08:00
|
|
|
u, _ := url.Parse(urlLocation)
|
2014-04-10 00:44:58 +08:00
|
|
|
glog.V(4).Infoln("post to", u)
|
2014-03-31 02:28:04 +08: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 {
|
2014-09-21 03:38:59 +08:00
|
|
|
glog.V(0).Infoln("failing to connect to volume server", r.RequestURI, do_err.Error())
|
2015-01-08 16:19:32 +08:00
|
|
|
writeJsonError(w, r, http.StatusInternalServerError, do_err)
|
2014-03-31 02:28:04 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
resp_body, ra_err := ioutil.ReadAll(resp.Body)
|
|
|
|
if ra_err != nil {
|
2014-09-21 03:38:59 +08:00
|
|
|
glog.V(0).Infoln("failing to upload to volume server", r.RequestURI, ra_err.Error())
|
2015-01-08 16:19:32 +08:00
|
|
|
writeJsonError(w, r, http.StatusInternalServerError, ra_err)
|
2014-03-31 02:28:04 +08:00
|
|
|
return
|
|
|
|
}
|
2014-04-10 00:44:58 +08:00
|
|
|
glog.V(4).Infoln("post result", string(resp_body))
|
2014-03-31 02:28:04 +08:00
|
|
|
var ret operation.UploadResult
|
|
|
|
unmarshal_err := json.Unmarshal(resp_body, &ret)
|
|
|
|
if unmarshal_err != nil {
|
2014-09-21 03:38:59 +08:00
|
|
|
glog.V(0).Infoln("failing to read upload resonse", r.RequestURI, string(resp_body))
|
2015-01-08 16:19:32 +08:00
|
|
|
writeJsonError(w, r, http.StatusInternalServerError, unmarshal_err)
|
2014-03-31 02:28:04 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if ret.Error != "" {
|
2014-09-21 03:38:59 +08:00
|
|
|
glog.V(0).Infoln("failing to post to volume server", r.RequestURI, ret.Error)
|
2015-01-08 16:19:32 +08:00
|
|
|
writeJsonError(w, r, http.StatusInternalServerError, errors.New(ret.Error))
|
2014-03-31 02:28:04 +08:00
|
|
|
return
|
|
|
|
}
|
2016-01-17 12:30:23 +08:00
|
|
|
path := r.URL.Path
|
2014-03-31 04:26:44 +08:00
|
|
|
if strings.HasSuffix(path, "/") {
|
|
|
|
if ret.Name != "" {
|
|
|
|
path += ret.Name
|
|
|
|
} else {
|
2016-01-13 16:58:07 +08:00
|
|
|
operation.DeleteFile(fs.master, fileId, fs.jwt(fileId)) //clean up
|
2014-03-31 04:26:44 +08:00
|
|
|
glog.V(0).Infoln("Can not to write to folder", path, "without a file name!")
|
2015-01-08 16:19:32 +08:00
|
|
|
writeJsonError(w, r, http.StatusInternalServerError,
|
|
|
|
errors.New("Can not to write to folder "+path+" without a file name"))
|
2014-03-31 04:26:44 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2016-01-13 16:58:07 +08:00
|
|
|
glog.V(4).Infoln("saving", path, "=>", fileId)
|
|
|
|
if db_err := fs.filer.CreateFile(path, fileId); db_err != nil {
|
|
|
|
operation.DeleteFile(fs.master, fileId, fs.jwt(fileId)) //clean up
|
2015-01-06 15:03:27 +08:00
|
|
|
glog.V(0).Infof("failing to write %s to filer server : %v", path, db_err)
|
2015-01-08 16:19:32 +08:00
|
|
|
writeJsonError(w, r, http.StatusInternalServerError, db_err)
|
2014-03-31 04:26:44 +08:00
|
|
|
return
|
2014-03-31 02:28:04 +08:00
|
|
|
}
|
2014-07-22 05:25:51 +08:00
|
|
|
w.WriteHeader(http.StatusCreated)
|
2015-01-09 19:24:49 +08:00
|
|
|
w.Write(resp_body)
|
2014-03-31 02:28:04 +08:00
|
|
|
}
|
|
|
|
|
2014-04-18 13:33:21 +08:00
|
|
|
// curl -X DELETE http://localhost:8888/path/to
|
|
|
|
// curl -X DELETE http://localhost:8888/path/to?recursive=true
|
2014-03-31 02:28:04 +08:00
|
|
|
func (fs *FilerServer) DeleteHandler(w http.ResponseWriter, r *http.Request) {
|
2014-04-10 00:44:58 +08:00
|
|
|
var err error
|
|
|
|
var fid string
|
|
|
|
if strings.HasSuffix(r.URL.Path, "/") {
|
2014-04-18 13:33:21 +08:00
|
|
|
isRecursive := r.FormValue("recursive") == "true"
|
|
|
|
err = fs.filer.DeleteDirectory(r.URL.Path, isRecursive)
|
2014-04-10 00:44:58 +08:00
|
|
|
} else {
|
|
|
|
fid, err = fs.filer.DeleteFile(r.URL.Path)
|
2015-08-22 05:09:33 +08:00
|
|
|
if err == nil && fid != "" {
|
2015-02-08 07:35:28 +08:00
|
|
|
err = operation.DeleteFile(fs.master, fid, fs.jwt(fid))
|
2014-03-31 02:28:04 +08:00
|
|
|
}
|
|
|
|
}
|
2014-04-10 12:01:48 +08:00
|
|
|
if err == nil {
|
2015-01-08 16:19:32 +08:00
|
|
|
writeJsonQuiet(w, r, http.StatusAccepted, map[string]string{"error": ""})
|
2014-03-31 02:28:04 +08:00
|
|
|
} else {
|
2014-04-10 12:01:48 +08:00
|
|
|
glog.V(4).Infoln("deleting", r.URL.Path, ":", err.Error())
|
2015-01-08 16:19:32 +08:00
|
|
|
writeJsonError(w, r, http.StatusInternalServerError, err)
|
2014-03-31 02:28:04 +08:00
|
|
|
}
|
|
|
|
}
|