常见图片文件打开

This commit is contained in:
Liang Ding 2014-09-01 21:40:10 +08:00
parent bee9486f00
commit 732a1ec3c4
3 changed files with 40 additions and 9 deletions

View File

@ -51,8 +51,25 @@ func GetFile(w http.ResponseWriter, r *http.Request) {
buf, _ := ioutil.ReadFile(path) 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 isBinary := false
// 判断是否是二进制文件 // 判断是否是其他二进制文件
for _, b := range buf { for _, b := range buf {
if 0 == b { // 包含 0 字节就认为是二进制文件 if 0 == b { // 包含 0 字节就认为是二进制文件
isBinary = true isBinary = true
@ -64,14 +81,7 @@ func GetFile(w http.ResponseWriter, r *http.Request) {
data["succ"] = false data["succ"] = false
data["msg"] = "Can't open a binary file :(" data["msg"] = "Can't open a binary file :("
} else { } else {
idx := strings.LastIndex(path, ".")
data["content"] = string(buf) data["content"] = string(buf)
extension := ""
if 0 <= idx {
extension = path[idx:]
}
data["mode"] = getEditorMode(extension) data["mode"] = getEditorMode(extension)
} }
} }
@ -274,7 +284,6 @@ func createFile(path, fileType string) bool {
return true return true
default: default:
glog.Infof("Unsupported file type [%s]", fileType) glog.Infof("Unsupported file type [%s]", fileType)
return false return false
@ -292,3 +301,14 @@ func removeFile(path string) bool {
return true 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
}
}

View File

@ -49,6 +49,9 @@ func main() {
// 静态资源 // 静态资源
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static")))) http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
// 库资源
http.Handle("/data/", http.StripPrefix("/data/", http.FileServer(http.Dir("data"))))
// IDE 首页 // IDE 首页
http.HandleFunc("/", indexHandler) http.HandleFunc("/", indexHandler)

View File

@ -120,6 +120,14 @@ var tree = {
return false; return false;
} }
if ("img" == data.mode) { // 是图片文件的话新建 tab 打开
// 最好是开 tab但这个最终取决于浏览器设置
var w = window.open(data.path);
return false;
}
editors.newEditor(data); editors.newEditor(data);
} }
}); });