diff --git a/go/operation/upload_content.go b/go/operation/upload_content.go index 10e9db315..646df6886 100644 --- a/go/operation/upload_content.go +++ b/go/operation/upload_content.go @@ -17,6 +17,7 @@ import ( ) type UploadResult struct { + Name string Size int Error string } diff --git a/go/weed/weed_server/filer_server.go b/go/weed/weed_server/filer_server.go index 80ffb0861..ffcb64cfc 100644 --- a/go/weed/weed_server/filer_server.go +++ b/go/weed/weed_server/filer_server.go @@ -57,7 +57,11 @@ func (fs *FilerServer) ListDirectories(fullpath string) (dirs []string, err erro if e != nil { 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) { @@ -82,6 +86,9 @@ func (fs *FilerServer) ListFiles(fullpath string, start, limit int) (files []str break } fileName := key[len(fullpath):] + if fileName == "" { + continue //skip the directory entry + } if strings.Contains(fileName, "/") { break } @@ -129,15 +136,17 @@ func (fs *FilerServer) findEntry(fullpath string) (value string, err error) { func (fs *FilerServer) ensureFileFolder(fullFileName string) (err error) { parts := strings.Split(fullFileName, "/") path := "/" - for i := 1; i < len(parts)-1; i++ { + for i := 1; i < len(parts); i++ { sub := parts[i] - if sub == "" { - continue + if i == len(parts)-1 { + sub = "" } if err = fs.ensureFolderHasEntry(path, sub); err != nil { return } - path = path + sub + "/" + if sub != "" { + path = path + sub + "/" + } } return nil } diff --git a/go/weed/weed_server/filer_server_handlers.go b/go/weed/weed_server/filer_server_handlers.go index 5bdbdc96d..a9ead0e33 100644 --- a/go/weed/weed_server/filer_server_handlers.go +++ b/go/weed/weed_server/filer_server_handlers.go @@ -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) { diff --git a/go/weed/weed_server/volume_server_handlers.go b/go/weed/weed_server/volume_server_handlers.go index aaa441004..0649ce7f4 100644 --- a/go/weed/weed_server/volume_server_handlers.go +++ b/go/weed/weed_server/volume_server_handlers.go @@ -221,6 +221,9 @@ func (vs *VolumeServer) PostHandler(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusInternalServerError) m["error"] = errorStatus } + if needle.HasName() { + m["name"] = string(needle.Name) + } m["size"] = ret writeJsonQuiet(w, r, m) }