mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2025-10-15 20:06:19 +08:00
more robust filer
This commit is contained in:
@@ -17,6 +17,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type UploadResult struct {
|
type UploadResult struct {
|
||||||
|
Name string
|
||||||
Size int
|
Size int
|
||||||
Error string
|
Error string
|
||||||
}
|
}
|
||||||
|
@@ -57,7 +57,11 @@ func (fs *FilerServer) ListDirectories(fullpath string) (dirs []string, err erro
|
|||||||
if e != nil {
|
if e != nil {
|
||||||
return nil, e
|
return nil, e
|
||||||
}
|
}
|
||||||
return strings.Split(string(data), ":"), nil
|
val := string(data)
|
||||||
|
if val == "" {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
return strings.Split(val, ":"), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fs *FilerServer) ListFiles(fullpath string, start, limit int) (files []string) {
|
func (fs *FilerServer) ListFiles(fullpath string, start, limit int) (files []string) {
|
||||||
@@ -82,6 +86,9 @@ func (fs *FilerServer) ListFiles(fullpath string, start, limit int) (files []str
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
fileName := key[len(fullpath):]
|
fileName := key[len(fullpath):]
|
||||||
|
if fileName == "" {
|
||||||
|
continue //skip the directory entry
|
||||||
|
}
|
||||||
if strings.Contains(fileName, "/") {
|
if strings.Contains(fileName, "/") {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@@ -129,15 +136,17 @@ func (fs *FilerServer) findEntry(fullpath string) (value string, err error) {
|
|||||||
func (fs *FilerServer) ensureFileFolder(fullFileName string) (err error) {
|
func (fs *FilerServer) ensureFileFolder(fullFileName string) (err error) {
|
||||||
parts := strings.Split(fullFileName, "/")
|
parts := strings.Split(fullFileName, "/")
|
||||||
path := "/"
|
path := "/"
|
||||||
for i := 1; i < len(parts)-1; i++ {
|
for i := 1; i < len(parts); i++ {
|
||||||
sub := parts[i]
|
sub := parts[i]
|
||||||
if sub == "" {
|
if i == len(parts)-1 {
|
||||||
continue
|
sub = ""
|
||||||
}
|
}
|
||||||
if err = fs.ensureFolderHasEntry(path, sub); err != nil {
|
if err = fs.ensureFolderHasEntry(path, sub); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
path = path + sub + "/"
|
if sub != "" {
|
||||||
|
path = path + sub + "/"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@@ -12,6 +12,7 @@ import (
|
|||||||
"math/rand"
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -29,9 +30,35 @@ func (fs *FilerServer) filerHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
fs.PostHandler(w, r)
|
fs.PostHandler(w, r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
func (fs *FilerServer) listDirectoryHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if !strings.HasSuffix(r.URL.Path, "/") {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
dirlist, err := fs.ListDirectories(r.URL.Path)
|
||||||
|
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
|
||||||
|
m["Subdirectories"] = dirlist
|
||||||
|
start, _ := strconv.Atoi(r.FormValue("start"))
|
||||||
|
limit, limit_err := strconv.Atoi(r.FormValue("limit"))
|
||||||
|
if limit_err != nil {
|
||||||
|
limit = 100
|
||||||
|
}
|
||||||
|
m["Files"] = fs.ListFiles(r.URL.Path, start, limit)
|
||||||
|
writeJsonQuiet(w, r, m)
|
||||||
|
}
|
||||||
func (fs *FilerServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request, isGetMethod bool) {
|
func (fs *FilerServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request, isGetMethod bool) {
|
||||||
|
if strings.HasSuffix(r.URL.Path, "/") {
|
||||||
|
fs.listDirectoryHandler(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
fileId, err := fs.FindFile(r.URL.Path)
|
fileId, err := fs.FindFile(r.URL.Path)
|
||||||
if err == leveldb.ErrNotFound {
|
if err == leveldb.ErrNotFound {
|
||||||
|
glog.V(3).Infoln("Not found in db", r.URL.Path)
|
||||||
w.WriteHeader(http.StatusNotFound)
|
w.WriteHeader(http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -128,13 +155,25 @@ func (fs *FilerServer) PostHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
writeJsonError(w, r, errors.New(ret.Error))
|
writeJsonError(w, r, errors.New(ret.Error))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if db_err := fs.CreateFile(r.URL.Path, assignResult.Fid); db_err != nil {
|
path := r.URL.Path
|
||||||
|
if strings.HasSuffix(path, "/") {
|
||||||
|
if ret.Name != "" {
|
||||||
|
path += ret.Name
|
||||||
|
} else {
|
||||||
|
operation.DeleteFile(fs.master, assignResult.Fid) //clean up
|
||||||
|
glog.V(0).Infoln("Can not to write to folder", path, "without a file name!")
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
writeJsonError(w, r, errors.New("Can not to write to folder "+path+" without a file name"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if db_err := fs.CreateFile(path, assignResult.Fid); db_err != nil {
|
||||||
operation.DeleteFile(fs.master, assignResult.Fid) //clean up
|
operation.DeleteFile(fs.master, assignResult.Fid) //clean up
|
||||||
glog.V(0).Infoln("failing to write to filer server", db_err.Error())
|
glog.V(0).Infoln("failing to write to filer server", db_err.Error())
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
writeJsonError(w, r, db_err)
|
writeJsonError(w, r, db_err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
w.WriteHeader(http.StatusCreated)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fs *FilerServer) DeleteHandler(w http.ResponseWriter, r *http.Request) {
|
func (fs *FilerServer) DeleteHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
@@ -221,6 +221,9 @@ func (vs *VolumeServer) PostHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
m["error"] = errorStatus
|
m["error"] = errorStatus
|
||||||
}
|
}
|
||||||
|
if needle.HasName() {
|
||||||
|
m["name"] = string(needle.Name)
|
||||||
|
}
|
||||||
m["size"] = ret
|
m["size"] = ret
|
||||||
writeJsonQuiet(w, r, m)
|
writeJsonQuiet(w, r, m)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user