From c0ebee45dc34e525db9f01ca20ec1a75ca3f7e98 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Sun, 16 Nov 2014 16:25:27 +0800 Subject: [PATCH] file utils refactor --- conf/wide.go | 15 ++++---------- file/files.go | 50 ++++++++-------------------------------------- util/file.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 53 deletions(-) create mode 100644 util/file.go diff --git a/conf/wide.go b/conf/wide.go index a297c4c..813a55c 100644 --- a/conf/wide.go +++ b/conf/wide.go @@ -219,20 +219,20 @@ func (*conf) GetExecutableInGOBIN(executable string) string { // $GOPATH/bin/$GOOS_$GOARCH/executable ret := gopath + PathSeparator + "bin" + PathSeparator + os.Getenv("GOOS") + "_" + os.Getenv("GOARCH") + PathSeparator + executable - if isExist(ret) { + if util.File.IsExist(ret) { return ret } // $GOPATH/bin/{runtime.GOOS}_{runtime.GOARCH}/executable ret = gopath + PathSeparator + "bin" + PathSeparator + runtime.GOOS + "_" + runtime.GOARCH + PathSeparator + executable - if isExist(ret) { + if util.File.IsExist(ret) { return ret } // $GOPATH/bin/executable ret = gopath + PathSeparator + "bin" + PathSeparator + executable - if isExist(ret) { + if util.File.IsExist(ret) { return ret } } @@ -390,7 +390,7 @@ func CreateWorkspaceDir(path string) { // createDir creates a directory on the path if it not exists. func createDir(path string) { - if !isExist(path) { + if !util.File.IsExist(path) { if err := os.MkdirAll(path, 0775); nil != err { glog.Error(err) @@ -400,10 +400,3 @@ func createDir(path string) { glog.V(7).Infof("Created a directory [%s]", path) } } - -// isExist determines whether the file spcified by the given filename is exists. -func isExist(filename string) bool { - _, err := os.Stat(filename) - - return err == nil || os.IsExist(err) -} diff --git a/file/files.go b/file/files.go index b88a153..82977e6 100644 --- a/file/files.go +++ b/file/files.go @@ -54,8 +54,8 @@ type Snippet struct { // GetFiles handles request of constructing user workspace file tree. // -// The Go API source code package ($GOROOT/src/pkg) also as a child node, -// so that users can easily view the Go API source code. +// The Go API source code package also as a child node, +// so that users can easily view the Go API source code in filre tree. func GetFiles(w http.ResponseWriter, r *http.Request) { data := map[string]interface{}{"succ": true} defer util.RetJSON(w, r, data) @@ -84,6 +84,7 @@ func GetFiles(w http.ResponseWriter, r *http.Request) { // construct Go API node apiPath := runtime.GOROOT() + conf.PathSeparator + "src" + conf.PathSeparator + "pkg" + apiNode := FileNode{Name: "Go API", Path: apiPath, IconSkin: "ico-ztree-dir-api ", Type: "d", Creatable: false, Removable: false, FileNodes: []*FileNode{}} @@ -123,7 +124,7 @@ func GetFile(w http.ResponseWriter, r *http.Request) { extension := filepath.Ext(path) - if isImg(extension) { + if util.File.IsImg(extension) { // image file will be open in a browser tab data["mode"] = "img" @@ -143,7 +144,7 @@ func GetFile(w http.ResponseWriter, r *http.Request) { content := string(buf) - if isBinary(content) { + if util.File.IsBinary(content) { data["succ"] = false data["msg"] = "Can't open a binary file :(" } else { @@ -318,7 +319,7 @@ func Find(w http.ResponseWriter, r *http.Request) { userWorkspace := conf.Wide.GetUserWorkspace(username) workspaces := filepath.SplitList(userWorkspace) - if "" != path && !isDir(path) { + if "" != path && !util.File.IsDir(path) { path = filepath.Dir(path) } @@ -441,7 +442,7 @@ func listFiles(dirname string) []string { // // Refers to the zTree document for CSS class names. func getIconSkin(filenameExtension string) string { - if isImg(filenameExtension) { + if util.File.IsImg(filenameExtension) { return "ico-ztree-img " } @@ -660,7 +661,7 @@ func searchInFile(path string, text string) []*Snippet { } content := string(bytes) - if isBinary(content) { + if util.File.IsBinary(content) { return ret } @@ -679,41 +680,6 @@ func searchInFile(path string, text string) []*Snippet { return ret } -// isBinary determines whether the specified content is a binary file content. -func isBinary(content string) bool { - for _, b := range content { - if 0 == b { - return true - } - } - - return false -} - -// isImg determines whether the specified extension is a image. -func isImg(extension string) bool { - ext := strings.ToLower(extension) - - switch ext { - case ".jpg", ".jpeg", ".bmp", ".gif", ".png", ".svg", ".ico": - return true - default: - return false - } -} - -// isDir determines whether the specified path is a directory. -func isDir(path string) bool { - fio, err := os.Lstat(path) - if nil != err { - glog.Warningf("Determines whether [%s] is a directory failed: [%v]", path, err) - - return false - } - - return fio.IsDir() -} - // GetUsre gets the user the specified path belongs to. Returns nil if not found. func GetUsre(path string) *conf.User { for _, user := range conf.Wide.Users { diff --git a/util/file.go b/util/file.go new file mode 100644 index 0000000..dc4d086 --- /dev/null +++ b/util/file.go @@ -0,0 +1,55 @@ +package util + +import ( + "os" + "strings" + + "github.com/golang/glog" +) + +type myfile struct{} + +// File utilities. +var File = myfile{} + +// IsExist determines whether the file spcified by the given path is exists. +func (*myfile) IsExist(path string) bool { + _, err := os.Stat(path) + + return err == nil || os.IsExist(err) +} + +// IsBinary determines whether the specified content is a binary file content. +func (*myfile) IsBinary(content string) bool { + for _, b := range content { + if 0 == b { + return true + } + } + + return false +} + +// IsImg determines whether the specified extension is a image. +func (*myfile) IsImg(extension string) bool { + ext := strings.ToLower(extension) + + switch ext { + case ".jpg", ".jpeg", ".bmp", ".gif", ".png", ".svg", ".ico": + return true + default: + return false + } +} + +// IsDir determines whether the specified path is a directory. +func (*myfile) IsDir(path string) bool { + fio, err := os.Lstat(path) + if nil != err { + glog.Warningf("Determines whether [%s] is a directory failed: [%v]", path, err) + + return false + } + + return fio.IsDir() +}