二进制文件打开处理

This commit is contained in:
Liang Ding 2014-09-01 18:55:11 +08:00
parent a049e97e7f
commit 880940cce7
4 changed files with 40 additions and 19 deletions

View File

@ -36,6 +36,8 @@ func GetFiles(w http.ResponseWriter, r *http.Request) {
}
func GetFile(w http.ResponseWriter, r *http.Request) {
data := map[string]interface{}{"succ": true}
decoder := json.NewDecoder(r.Body)
var args map[string]interface{}
@ -53,16 +55,29 @@ func GetFile(w http.ResponseWriter, r *http.Request) {
buf, _ := ioutil.ReadFile(path)
content := string(buf)
data := map[string]interface{}{"succ": true}
data["content"] = content
extension := ""
if 0 <= idx {
extension = path[idx:]
isBinary := false
// 判断是否是二进制文件
for _, b := range buf {
if 0 == b { // 包含 0 字节就认为是二进制文件
isBinary = true
}
}
if isBinary {
// 是二进制文件的话前端编辑器不打开
data["succ"] = false
data["msg"] = "Can't open a binary file :("
} else {
data["content"] = string(buf)
extension := ""
if 0 <= idx {
extension = path[idx:]
}
data["mode"] = getEditorMode(extension)
}
data["mode"] = getEditorMode(extension)
ret, _ := json.Marshal(data)

14
main.go
View File

@ -20,14 +20,14 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
session, _ := user.Session.Get(r, "wide-session")
// TODO: if session.IsNew {
// TODO: 以 admin 作为用户登录
name := conf.Wide.Users[0].Name
glog.Infof("[%s] logged in", name)
if session.IsNew {
// TODO: 以 admin 作为用户登录
name := conf.Wide.Users[0].Name
glog.Infof("[%s] logged in", name)
session.Values["username"] = name
session.Values["id"] = strconv.Itoa(rand.Int())
// }
session.Values["username"] = name
session.Values["id"] = strconv.Itoa(rand.Int())
}
session.Save(r, w)

View File

@ -162,10 +162,12 @@ func BuildHandler(w http.ResponseWriter, r *http.Request) {
// 先把可执行文件删了
os.RemoveAll(executable)
data := map[string]interface{}{"succ": true}
stdout, err := cmd.StdoutPipe()
if nil != err {
glog.Error(err)
http.Error(w, err.Error(), 500)
data["succ"] = false
return
}
@ -173,7 +175,7 @@ func BuildHandler(w http.ResponseWriter, r *http.Request) {
stderr, err := cmd.StderrPipe()
if nil != err {
glog.Error(err)
http.Error(w, err.Error(), 500)
data["succ"] = false
return
}
@ -196,6 +198,8 @@ func BuildHandler(w http.ResponseWriter, r *http.Request) {
channelRet["nextCmd"] = "run"
channelRet["executable"] = executable
glog.Info(outputWS)
glog.Info(sid)
if nil != outputWS[sid] {
glog.Infof("Session [%s] 's build [id=%d, file=%s] has done", sid, runningId, filePath)
@ -207,7 +211,7 @@ func BuildHandler(w http.ResponseWriter, r *http.Request) {
}(rand.Int())
ret, _ := json.Marshal(map[string]interface{}{"succ": true})
ret, _ := json.Marshal(data)
w.Header().Set("Content-Type", "application/json")
w.Write(ret)

View File

@ -116,6 +116,8 @@ var tree = {
dataType: "json",
success: function(data) {
if (!data.succ) {
alert(data.msg);
return false;
}
editors.newEditor(data);