diff --git a/file/files.go b/file/files.go index 51b710e..84fadb2 100644 --- a/file/files.go +++ b/file/files.go @@ -51,8 +51,25 @@ func GetFile(w http.ResponseWriter, r *http.Request) { buf, _ := ioutil.ReadFile(path) + idx := strings.LastIndex(path, ".") + extension := "" + if 0 <= idx { + extension = path[idx:] + } + + // 通过文件扩展名判断是否是图片文件(图片在浏览器里新建 tab 打开) + if isImg(extension) { + data["mode"] = "img" + + path2 := strings.Replace(path, "\\", "/", -1) + idx = strings.Index(path2, "/data/user_repos") + data["path"] = path2[idx:] + + return + } + isBinary := false - // 判断是否是二进制文件 + // 判断是否是其他二进制文件 for _, b := range buf { if 0 == b { // 包含 0 字节就认为是二进制文件 isBinary = true @@ -64,14 +81,7 @@ func GetFile(w http.ResponseWriter, r *http.Request) { data["succ"] = false data["msg"] = "Can't open a binary file :(" } else { - idx := strings.LastIndex(path, ".") data["content"] = string(buf) - - extension := "" - if 0 <= idx { - extension = path[idx:] - } - data["mode"] = getEditorMode(extension) } } @@ -274,7 +284,6 @@ func createFile(path, fileType string) bool { return true default: - glog.Infof("Unsupported file type [%s]", fileType) return false @@ -292,3 +301,14 @@ func removeFile(path string) bool { return true } + +func isImg(extension string) bool { + ext := strings.ToLower(extension) + + switch ext { + case ".jpg", ".jpeg", ".bmp", ".gif", ".png", ".svg", ".ico": + return true + default: + return false + } +} diff --git a/main.go b/main.go index 28bb578..691f4ee 100644 --- a/main.go +++ b/main.go @@ -49,6 +49,9 @@ func main() { // 静态资源 http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static")))) + // 库资源 + http.Handle("/data/", http.StripPrefix("/data/", http.FileServer(http.Dir("data")))) + // IDE 首页 http.HandleFunc("/", indexHandler) diff --git a/static/js/tree.js b/static/js/tree.js index 1e2e853..2b64bc4 100644 --- a/static/js/tree.js +++ b/static/js/tree.js @@ -120,6 +120,14 @@ var tree = { return false; } + + if ("img" == data.mode) { // 是图片文件的话新建 tab 打开 + // 最好是开 tab,但这个最终取决于浏览器设置 + var w = window.open(data.path); + + return false; + } + editors.newEditor(data); } });