二进制文件打开处理

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

View File

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

View File

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

View File

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