more robust filer

This commit is contained in:
Chris Lu
2014-03-30 13:26:44 -07:00
parent 2861275fb6
commit 51fafdb525
4 changed files with 59 additions and 7 deletions

View File

@@ -12,6 +12,7 @@ import (
"math/rand"
"net/http"
"net/url"
"strconv"
"strings"
)
@@ -29,9 +30,35 @@ func (fs *FilerServer) filerHandler(w http.ResponseWriter, r *http.Request) {
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) {
if strings.HasSuffix(r.URL.Path, "/") {
fs.listDirectoryHandler(w, r)
return
}
fileId, err := fs.FindFile(r.URL.Path)
if err == leveldb.ErrNotFound {
glog.V(3).Infoln("Not found in db", r.URL.Path)
w.WriteHeader(http.StatusNotFound)
return
}
@@ -128,13 +155,25 @@ func (fs *FilerServer) PostHandler(w http.ResponseWriter, r *http.Request) {
writeJsonError(w, r, errors.New(ret.Error))
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
glog.V(0).Infoln("failing to write to filer server", db_err.Error())
w.WriteHeader(http.StatusInternalServerError)
writeJsonError(w, r, db_err)
return
}
w.WriteHeader(http.StatusCreated)
}
func (fs *FilerServer) DeleteHandler(w http.ResponseWriter, r *http.Request) {