wide/file/files.go

371 lines
7.2 KiB
Go
Raw Normal View History

2014-08-18 17:45:43 +04:00
package file
import (
"encoding/json"
"io/ioutil"
"net/http"
"os"
"path/filepath"
2014-09-12 06:56:18 +04:00
"runtime"
2014-08-18 17:45:43 +04:00
"sort"
"strings"
2014-09-07 13:31:57 +04:00
"github.com/b3log/wide/conf"
"github.com/b3log/wide/user"
"github.com/b3log/wide/util"
"github.com/golang/glog"
2014-08-18 17:45:43 +04:00
)
2014-09-12 06:56:18 +04:00
// 构造用户工作空间文件树.
// 将 Go API 源码包($GOROOT/src/pkg也作为子节点这样能方便用户查看 Go API 源码.
2014-08-18 17:45:43 +04:00
func GetFiles(w http.ResponseWriter, r *http.Request) {
2014-09-01 16:50:51 +04:00
data := map[string]interface{}{"succ": true}
defer util.RetJSON(w, r, data)
2014-08-31 13:39:39 +04:00
session, _ := user.Session.Get(r, "wide-session")
2014-08-31 10:15:13 +04:00
2014-08-31 14:50:38 +04:00
username := session.Values["username"].(string)
2014-09-05 07:24:53 +04:00
userSrc := conf.Wide.GetUserWorkspace(username) + string(os.PathSeparator) + "src"
2014-08-31 10:15:13 +04:00
2014-09-05 13:39:21 +04:00
root := FileNode{Name: "projects", Path: userSrc, IconSkin: "ico-ztree-dir ", Type: "d", FileNodes: []*FileNode{}}
2014-08-18 17:45:43 +04:00
2014-09-12 06:56:18 +04:00
// 构造 Go API 节点
apiPath := runtime.GOROOT() + string(os.PathSeparator) + "src" + string(os.PathSeparator) + "pkg"
apiNode := FileNode{Name: "Go API", Path: apiPath, FileNodes: []*FileNode{}}
go func() {
fio, _ := os.Lstat(apiPath)
if fio.IsDir() {
apiNode.Type = "d"
2014-09-12 07:35:53 +04:00
// TOOD: Go API 用另外的样式
2014-09-12 06:56:18 +04:00
apiNode.IconSkin = "ico-ztree-dir "
walk(apiPath, &apiNode)
} else {
apiNode.Type = "f"
ext := filepath.Ext(apiPath)
apiNode.IconSkin = getIconSkin(ext)
apiNode.Mode = getEditorMode(ext)
}
}()
// 构造用户工作空间文件树
walk(userSrc, &root)
// 添加 Go API 节点
root.FileNodes = append(root.FileNodes, &apiNode)
2014-08-18 17:45:43 +04:00
data["root"] = root
}
func GetFile(w http.ResponseWriter, r *http.Request) {
2014-09-01 14:55:11 +04:00
data := map[string]interface{}{"succ": true}
2014-09-01 16:50:51 +04:00
defer util.RetJSON(w, r, data)
2014-09-01 14:55:11 +04:00
2014-08-18 17:45:43 +04:00
decoder := json.NewDecoder(r.Body)
var args map[string]interface{}
if err := decoder.Decode(&args); err != nil {
glog.Error(err)
2014-09-01 16:50:51 +04:00
data["succ"] = false
2014-08-18 17:45:43 +04:00
return
}
path := args["path"].(string)
buf, _ := ioutil.ReadFile(path)
2014-09-05 17:10:28 +04:00
extension := filepath.Ext(path)
2014-09-01 17:40:10 +04:00
// 通过文件扩展名判断是否是图片文件(图片在浏览器里新建 tab 打开)
if isImg(extension) {
data["mode"] = "img"
path2 := strings.Replace(path, "\\", "/", -1)
2014-09-05 17:10:28 +04:00
idx := strings.Index(path2, "/data/user_workspaces")
2014-09-01 17:40:10 +04:00
data["path"] = path2[idx:]
return
}
2014-09-01 14:55:11 +04:00
isBinary := false
2014-09-01 17:40:10 +04:00
// 判断是否是其他二进制文件
2014-09-01 14:55:11 +04:00
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)
data["mode"] = getEditorMode(extension)
2014-08-18 17:45:43 +04:00
}
}
func SaveFile(w http.ResponseWriter, r *http.Request) {
2014-09-01 16:50:51 +04:00
data := map[string]interface{}{"succ": true}
defer util.RetJSON(w, r, data)
2014-08-18 17:45:43 +04:00
decoder := json.NewDecoder(r.Body)
var args map[string]interface{}
if err := decoder.Decode(&args); err != nil {
glog.Error(err)
2014-09-01 16:50:51 +04:00
data["succ"] = false
2014-08-18 17:45:43 +04:00
return
}
filePath := args["file"].(string)
fout, err := os.Create(filePath)
if nil != err {
glog.Error(err)
2014-09-01 16:50:51 +04:00
data["succ"] = false
2014-08-18 17:45:43 +04:00
return
}
code := args["code"].(string)
fout.WriteString(code)
if err := fout.Close(); nil != err {
glog.Error(err)
2014-09-01 16:50:51 +04:00
data["succ"] = false
2014-08-18 17:45:43 +04:00
return
}
}
func NewFile(w http.ResponseWriter, r *http.Request) {
2014-09-01 16:50:51 +04:00
data := map[string]interface{}{"succ": true}
defer util.RetJSON(w, r, data)
2014-08-18 17:45:43 +04:00
decoder := json.NewDecoder(r.Body)
var args map[string]interface{}
if err := decoder.Decode(&args); err != nil {
glog.Error(err)
2014-09-01 16:50:51 +04:00
data["succ"] = false
2014-08-18 17:45:43 +04:00
return
}
path := args["path"].(string)
fileType := args["fileType"].(string)
if !createFile(path, fileType) {
data["succ"] = false
}
}
func RemoveFile(w http.ResponseWriter, r *http.Request) {
2014-09-01 16:50:51 +04:00
data := map[string]interface{}{"succ": true}
defer util.RetJSON(w, r, data)
2014-08-18 17:45:43 +04:00
decoder := json.NewDecoder(r.Body)
var args map[string]interface{}
if err := decoder.Decode(&args); err != nil {
glog.Error(err)
2014-09-01 16:50:51 +04:00
data["succ"] = false
2014-08-18 17:45:43 +04:00
return
}
path := args["path"].(string)
if !removeFile(path) {
data["succ"] = false
}
}
type FileNode struct {
Name string `json:"name"`
Path string `json:"path"`
2014-09-05 17:10:28 +04:00
IconSkin string `json:"iconSkin"` // 值的末尾应该有一个空格
2014-08-18 17:45:43 +04:00
Type string `json:"type"`
2014-09-07 13:31:57 +04:00
Mode string `json:"mode"`
2014-08-18 17:45:43 +04:00
FileNodes []*FileNode `json:"children"`
}
2014-09-12 06:56:18 +04:00
// 遍历指定的 path构造文件树.
func walk(path string, node *FileNode) {
2014-08-18 17:45:43 +04:00
files := listFiles(path)
for _, filename := range files {
fpath := filepath.Join(path, filename)
fio, _ := os.Lstat(fpath)
child := FileNode{Name: filename, Path: fpath, FileNodes: []*FileNode{}}
node.FileNodes = append(node.FileNodes, &child)
if nil == fio {
glog.Warningf("Path [%s] is nil", fpath)
continue
}
if fio.IsDir() {
child.Type = "d"
2014-09-05 13:39:21 +04:00
child.IconSkin = "ico-ztree-dir "
2014-08-23 19:06:23 +04:00
2014-09-12 06:56:18 +04:00
walk(fpath, &child)
2014-08-18 17:45:43 +04:00
} else {
child.Type = "f"
2014-09-05 17:10:28 +04:00
ext := filepath.Ext(fpath)
2014-09-05 13:01:20 +04:00
2014-09-05 17:10:28 +04:00
child.IconSkin = getIconSkin(ext)
2014-09-07 13:31:57 +04:00
child.Mode = getEditorMode(ext)
2014-08-18 17:45:43 +04:00
}
}
return
}
func listFiles(dirname string) []string {
f, _ := os.Open(dirname)
names, _ := f.Readdirnames(-1)
f.Close()
sort.Strings(names)
2014-08-23 19:06:23 +04:00
dirs := []string{}
files := []string{}
2014-08-31 10:07:35 +04:00
// 排序:目录靠前,文件靠后
2014-08-23 19:06:23 +04:00
for _, name := range names {
fio, _ := os.Lstat(filepath.Join(dirname, name))
if fio.IsDir() {
2014-08-31 10:07:35 +04:00
// 排除 .git 目录
if ".git" == fio.Name() {
continue
}
2014-08-23 19:06:23 +04:00
dirs = append(dirs, name)
} else {
files = append(files, name)
}
}
return append(dirs, files...)
2014-08-18 17:45:43 +04:00
}
2014-09-05 13:01:20 +04:00
func getIconSkin(filenameExtension string) string {
2014-09-06 18:33:09 +04:00
if "" == filenameExtension || ".exe" == filenameExtension {
2014-09-05 13:39:21 +04:00
return "ico-ztree-other "
2014-09-05 13:01:20 +04:00
}
switch filenameExtension {
2014-09-05 17:10:28 +04:00
case ".gitignore":
return "ico-ztree-other "
2014-09-05 13:01:20 +04:00
case ".json":
2014-09-05 13:39:21 +04:00
return "ico-ztree-js "
2014-09-05 13:01:20 +04:00
case ".txt":
2014-09-05 13:39:21 +04:00
return "ico-ztree-text "
2014-09-05 13:01:20 +04:00
case ".properties":
2014-09-05 13:39:21 +04:00
return "ico-ztree-pro "
2014-09-05 13:54:07 +04:00
case ".htm":
return "ico-ztree-html "
2014-09-05 13:01:20 +04:00
default:
if isImg(filenameExtension) {
2014-09-05 13:39:21 +04:00
return "ico-ztree-img "
2014-09-05 13:01:20 +04:00
}
2014-09-05 13:39:21 +04:00
return "ico-ztree-" + filenameExtension[1:] + " "
2014-09-05 13:01:20 +04:00
}
2014-09-05 13:54:07 +04:00
return ""
2014-09-05 13:01:20 +04:00
}
2014-08-18 17:45:43 +04:00
func getEditorMode(filenameExtension string) string {
switch filenameExtension {
case ".go":
2014-09-06 18:33:09 +04:00
return "text/x-go"
2014-08-18 17:45:43 +04:00
case ".html":
2014-09-06 18:33:09 +04:00
return "text/html"
2014-08-18 17:45:43 +04:00
case ".md":
2014-09-06 18:33:09 +04:00
return "text/x-markdown"
2014-09-06 18:20:10 +04:00
case ".js":
2014-09-06 18:33:09 +04:00
return "text/javascript"
2014-09-06 18:20:10 +04:00
case ".json":
return "application/json"
2014-08-18 17:45:43 +04:00
case ".css":
2014-09-06 18:33:09 +04:00
return "text/css"
2014-08-18 17:45:43 +04:00
case ".xml":
2014-09-06 18:33:09 +04:00
return "application/xml"
2014-08-18 17:45:43 +04:00
case ".sh":
2014-09-06 18:33:09 +04:00
return "text/x-sh"
2014-08-18 17:45:43 +04:00
case ".sql":
2014-09-06 18:33:09 +04:00
return "text/x-sql"
2014-08-18 17:45:43 +04:00
default:
2014-09-06 18:33:09 +04:00
return "text/plain"
2014-08-18 17:45:43 +04:00
}
}
func createFile(path, fileType string) bool {
switch fileType {
case "f":
file, err := os.OpenFile(path, os.O_CREATE, 0664)
if nil != err {
glog.Info(err)
return false
}
defer file.Close()
glog.Infof("Created file [%s]", path)
return true
case "d":
err := os.Mkdir(path, 0775)
if nil != err {
glog.Info(err)
}
glog.Infof("Created directory [%s]", path)
return true
default:
glog.Infof("Unsupported file type [%s]", fileType)
return false
}
}
func removeFile(path string) bool {
if err := os.RemoveAll(path); nil != err {
glog.Errorf("Removes [%s] failed: [%s]", path, err.Error())
return false
}
glog.Infof("Removed [%s]", path)
return true
}
2014-09-01 17:40:10 +04:00
func isImg(extension string) bool {
ext := strings.ToLower(extension)
switch ext {
case ".jpg", ".jpeg", ".bmp", ".gif", ".png", ".svg", ".ico":
return true
default:
return false
}
}