From fa5babfe1caf0ec73f61062601e9ecf05b1a5f65 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Fri, 7 Nov 2014 17:22:20 +0800 Subject: [PATCH] #138 --- file/files.go | 41 +++++++++++++++++++++++++++++++++++++++++ main.go | 1 + 2 files changed, 42 insertions(+) diff --git a/file/files.go b/file/files.go index 627438d..925cdb8 100644 --- a/file/files.go +++ b/file/files.go @@ -238,6 +238,34 @@ func RemoveFile(w http.ResponseWriter, r *http.Request) { } } +// RenameFile handles request of renaming file or directory. +func RenameFile(w http.ResponseWriter, r *http.Request) { + data := map[string]interface{}{"succ": true} + defer util.RetJSON(w, r, data) + + var args map[string]interface{} + + if err := json.NewDecoder(r.Body).Decode(&args); err != nil { + glog.Error(err) + data["succ"] = false + + return + } + + oldPath := args["oldPath"].(string) + newPath := args["newPath"].(string) + sid := args["sid"].(string) + + wSession := session.WideSessions.Get(sid) + + if !renameFile(oldPath, newPath) { + data["succ"] = false + + wSession.EventQueue.Queue <- &event.Event{Code: event.EvtCodeServerInternalError, Sid: sid, + Data: "can't rename file " + path} + } +} + // SearchText handles request of searching files under the specified directory with the specified keyword. func SearchText(w http.ResponseWriter, r *http.Request) { data := map[string]interface{}{"succ": true} @@ -442,6 +470,19 @@ func removeFile(path string) bool { return true } +// renameFile renames (moves) a file from the specified old path to the specified new path. +func renameFile(oldPath, newPath string) bool { + if err := os.Rename(oldPath, newPath); nil != err { + glog.Errorf("Renames [%s] failed: [%s]", path, err.Error()) + + return false + } + + glog.Infof("Renamed [%s] to [%s]", oldPath, newPath) + + return true +} + // search finds file under the specified dir and its sub-directories with the specified text, likes the command grep/findstr. func search(dir, extension, text string, snippets []*Snippet) []*Snippet { if !strings.HasSuffix(dir, conf.PathSeparator) { diff --git a/main.go b/main.go index 07a7306..184af9c 100644 --- a/main.go +++ b/main.go @@ -238,6 +238,7 @@ func main() { http.HandleFunc("/file/save", handlerWrapper(file.SaveFile)) http.HandleFunc("/file/new", handlerWrapper(file.NewFile)) http.HandleFunc("/file/remove", handlerWrapper(file.RemoveFile)) + http.HandleFunc("/file/rename", handlerWrapper(file.RenameFile)) http.HandleFunc("/file/search/text", handlerWrapper(file.SearchText)) // editor