This commit is contained in:
Liang Ding 2014-10-31 13:15:40 +08:00
parent 5b5581392d
commit c7f702cd15
1 changed files with 18 additions and 9 deletions

View File

@ -122,19 +122,13 @@ func GetFile(w http.ResponseWriter, r *http.Request) {
return return
} }
isBinary := false content := string(buf)
// determine whether it is a binary file
for _, b := range buf {
if 0 == b {
isBinary = true
}
}
if isBinary { if 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 {
data["content"] = string(buf) data["content"] = content
data["mode"] = getEditorMode(extension) data["mode"] = getEditorMode(extension)
data["path"] = path data["path"] = path
} }
@ -491,6 +485,10 @@ func searchInFile(path string, text string) []*Snippet {
} }
content := string(bytes) content := string(bytes)
if isBinary(content) {
return ret
}
lines := strings.Split(content, "\n") lines := strings.Split(content, "\n")
for idx, line := range lines { for idx, line := range lines {
@ -506,6 +504,17 @@ 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. // isImg determines whether the specified extension is a image.
func isImg(extension string) bool { func isImg(extension string) bool {
ext := strings.ToLower(extension) ext := strings.ToLower(extension)