<commit_before>// 文件树操作.
package file

import (
	"encoding/json"
	"io/ioutil"
	"net/http"
	"os"
	"path/filepath"
	"runtime"
	"sort"
	"strings"

	"github.com/b3log/wide/conf"
	"github.com/b3log/wide/user"
	"github.com/b3log/wide/util"
	"github.com/golang/glog"
)

// 构造用户工作空间文件树.
// 将 Go API 源码包（$GOROOT/src/pkg）也作为子节点，这样能方便用户查看 Go API 源码.
func GetFiles(w http.ResponseWriter, r *http.Request) {
	data := map[string]interface{}{"succ": true}
	defer util.RetJSON(w, r, data)

	session, _ := user.Session.Get(r, "wide-session")

	username := session.Values["username"].(string)
	userSrc := conf.Wide.GetUserWorkspace(username) + string(os.PathSeparator) + "src"

	root := FileNode{Name: "projects", Path: userSrc, IconSkin: "ico-ztree-dir ", Type: "d", FileNodes: []*FileNode{}}

	// 构造 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"
			// TOOD: Go API 用另外的样式
			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)

	data["root"] = root
}

// 编辑器打开一个文件.
func GetFile(w http.ResponseWriter, r *http.Request) {
	data := map[string]interface{}{"succ": true}
	defer util.RetJSON(w, r, data)

	decoder := json.NewDecoder(r.Body)

	var args map[string]interface{}

	if err := decoder.Decode(&args); err != nil {
		glog.Error(err)
		data["succ"] = false

		return
	}

	path := args["path"].(string)<commit_msg>修复文件树异步构建问题<commit_after>// 文件树操作.
package file

import (
	"encoding/json"
	"io/ioutil"
	"net/http"
	"os"
	"path/filepath"
	"runtime"
	"sort"
	"strings"

	"github.com/b3log/wide/conf"
	"github.com/b3log/wide/user"
	"github.com/b3log/wide/util"
	"github.com/golang/glog"
)

// 构造用户工作空间文件树.
// 将 Go API 源码包（$GOROOT/src/pkg）也作为子节点，这样能方便用户查看 Go API 源码.
func GetFiles(w http.ResponseWriter, r *http.Request) {
	data := map[string]interface{}{"succ": true}
	defer util.RetJSON(w, r, data)

	session, _ := user.Session.Get(r, "wide-session")

	username := session.Values["username"].(string)
	userSrc := conf.Wide.GetUserWorkspace(username) + string(os.PathSeparator) + "src"

	root := FileNode{Name: "projects", Path: userSrc, IconSkin: "ico-ztree-dir ", Type: "d", FileNodes: []*FileNode{}}

	// 构造 Go API 节点
	apiPath := runtime.GOROOT() + string(os.PathSeparator) + "src" + string(os.PathSeparator) + "pkg"
	apiNode := FileNode{Name: "Go API", Path: apiPath, FileNodes: []*FileNode{}}

	goapiBuildOKSignal := make(chan bool)
	go func() {
		apiNode.Type = "d"
		// TOOD: Go API 用另外的样式
		apiNode.IconSkin = "ico-ztree-dir "

		walk(apiPath, &apiNode)

		// 放行信号
		close(goapiBuildOKSignal)
	}()

	// 构造用户工作空间文件树
	walk(userSrc, &root)

	// 等待放行
	<-goapiBuildOKSignal

	// 添加 Go API 节点
	root.FileNodes = append(root.FileNodes, &apiNode)

	data["root"] = root
}

// 编辑器打开一个文件.
func GetFile(w http.ResponseWriter, r *http.Request) {
	data := map[string]interface{}{"succ": true}
	defer util.RetJSON(w, r, data)

	decoder := json.NewDecoder(r.Body)

	var args map[string]interface{}

	if err := decoder.Decode(&args); err != nil {
		glog.Error(err)
		data["succ"] = false

		return
	}

	path := args["path"].(string)
{'fraction_non_alphanumeric': 0.1783366676193198, 'fraction_numerical': 0.0017147756501857674, 'mean_word_length': 4.065123010130246, 'pattern_counts': {'":': 4, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6834663', 'n_tokens_mistral': 1572, 'n_tokens_neox': 1543, 'n_words': 275}