file utils refactor

This commit is contained in:
Liang Ding 2014-11-16 16:25:27 +08:00
parent ac6edadf90
commit c0ebee45dc
3 changed files with 67 additions and 53 deletions

View File

@ -219,20 +219,20 @@ func (*conf) GetExecutableInGOBIN(executable string) string {
// $GOPATH/bin/$GOOS_$GOARCH/executable // $GOPATH/bin/$GOOS_$GOARCH/executable
ret := gopath + PathSeparator + "bin" + PathSeparator + ret := gopath + PathSeparator + "bin" + PathSeparator +
os.Getenv("GOOS") + "_" + os.Getenv("GOARCH") + PathSeparator + executable os.Getenv("GOOS") + "_" + os.Getenv("GOARCH") + PathSeparator + executable
if isExist(ret) { if util.File.IsExist(ret) {
return ret return ret
} }
// $GOPATH/bin/{runtime.GOOS}_{runtime.GOARCH}/executable // $GOPATH/bin/{runtime.GOOS}_{runtime.GOARCH}/executable
ret = gopath + PathSeparator + "bin" + PathSeparator + ret = gopath + PathSeparator + "bin" + PathSeparator +
runtime.GOOS + "_" + runtime.GOARCH + PathSeparator + executable runtime.GOOS + "_" + runtime.GOARCH + PathSeparator + executable
if isExist(ret) { if util.File.IsExist(ret) {
return ret return ret
} }
// $GOPATH/bin/executable // $GOPATH/bin/executable
ret = gopath + PathSeparator + "bin" + PathSeparator + executable ret = gopath + PathSeparator + "bin" + PathSeparator + executable
if isExist(ret) { if util.File.IsExist(ret) {
return ret return ret
} }
} }
@ -390,7 +390,7 @@ func CreateWorkspaceDir(path string) {
// createDir creates a directory on the path if it not exists. // createDir creates a directory on the path if it not exists.
func createDir(path string) { func createDir(path string) {
if !isExist(path) { if !util.File.IsExist(path) {
if err := os.MkdirAll(path, 0775); nil != err { if err := os.MkdirAll(path, 0775); nil != err {
glog.Error(err) glog.Error(err)
@ -400,10 +400,3 @@ func createDir(path string) {
glog.V(7).Infof("Created a directory [%s]", path) 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)
}

View File

@ -54,8 +54,8 @@ type Snippet struct {
// GetFiles handles request of constructing user workspace file tree. // GetFiles handles request of constructing user workspace file tree.
// //
// The Go API source code package ($GOROOT/src/pkg) also as a child node, // The Go API source code package also as a child node,
// so that users can easily view the Go API source code. // so that users can easily view the Go API source code in filre tree.
func GetFiles(w http.ResponseWriter, r *http.Request) { func GetFiles(w http.ResponseWriter, r *http.Request) {
data := map[string]interface{}{"succ": true} data := map[string]interface{}{"succ": true}
defer util.RetJSON(w, r, data) defer util.RetJSON(w, r, data)
@ -84,6 +84,7 @@ func GetFiles(w http.ResponseWriter, r *http.Request) {
// construct Go API node // construct Go API node
apiPath := runtime.GOROOT() + conf.PathSeparator + "src" + conf.PathSeparator + "pkg" apiPath := runtime.GOROOT() + conf.PathSeparator + "src" + conf.PathSeparator + "pkg"
apiNode := FileNode{Name: "Go API", Path: apiPath, IconSkin: "ico-ztree-dir-api ", Type: "d", apiNode := FileNode{Name: "Go API", Path: apiPath, IconSkin: "ico-ztree-dir-api ", Type: "d",
Creatable: false, Removable: false, FileNodes: []*FileNode{}} Creatable: false, Removable: false, FileNodes: []*FileNode{}}
@ -123,7 +124,7 @@ func GetFile(w http.ResponseWriter, r *http.Request) {
extension := filepath.Ext(path) extension := filepath.Ext(path)
if isImg(extension) { if util.File.IsImg(extension) {
// image file will be open in a browser tab // image file will be open in a browser tab
data["mode"] = "img" data["mode"] = "img"
@ -143,7 +144,7 @@ func GetFile(w http.ResponseWriter, r *http.Request) {
content := string(buf) content := string(buf)
if isBinary(content) { if util.File.IsBinary(content) {
data["succ"] = false data["succ"] = false
data["msg"] = "Can't open a binary file :(" data["msg"] = "Can't open a binary file :("
} else { } else {
@ -318,7 +319,7 @@ func Find(w http.ResponseWriter, r *http.Request) {
userWorkspace := conf.Wide.GetUserWorkspace(username) userWorkspace := conf.Wide.GetUserWorkspace(username)
workspaces := filepath.SplitList(userWorkspace) workspaces := filepath.SplitList(userWorkspace)
if "" != path && !isDir(path) { if "" != path && !util.File.IsDir(path) {
path = filepath.Dir(path) path = filepath.Dir(path)
} }
@ -441,7 +442,7 @@ func listFiles(dirname string) []string {
// //
// Refers to the zTree document for CSS class names. // Refers to the zTree document for CSS class names.
func getIconSkin(filenameExtension string) string { func getIconSkin(filenameExtension string) string {
if isImg(filenameExtension) { if util.File.IsImg(filenameExtension) {
return "ico-ztree-img " return "ico-ztree-img "
} }
@ -660,7 +661,7 @@ func searchInFile(path string, text string) []*Snippet {
} }
content := string(bytes) content := string(bytes)
if isBinary(content) { if util.File.IsBinary(content) {
return ret return ret
} }
@ -679,41 +680,6 @@ func searchInFile(path string, text string) []*Snippet {
return ret 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. // GetUsre gets the user the specified path belongs to. Returns nil if not found.
func GetUsre(path string) *conf.User { func GetUsre(path string) *conf.User {
for _, user := range conf.Wide.Users { for _, user := range conf.Wide.Users {

55
util/file.go Normal file
View File

@ -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()
}