From feafeb1c0dcf68ca3dae71fdc0e87b16549a6841 Mon Sep 17 00:00:00 2001 From: zouhuigang <952750120@qq.com> Date: Sat, 12 Oct 2019 15:57:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=AB=E6=8F=8F=E6=9C=AC=E5=9C=B0=E7=9B=AE?= =?UTF-8?q?=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 + go.mod | 4 + http/controller/md.go | 157 +++++++++++++----- util/file.go | 74 ++++++++- util/hashids.go | 26 +++ ...3\351\224\256\350\217\234\345\215\225.reg" | 26 +++ 6 files changed, 239 insertions(+), 50 deletions(-) create mode 100644 util/hashids.go create mode 100644 "\345\217\263\351\224\256\350\217\234\345\215\225.reg" diff --git a/.gitignore b/.gitignore index 5831a4d..02c7e62 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ fox.wiki/* fox.exe fox.wiki + +go.sum diff --git a/go.mod b/go.mod index 7cc52a8..59b3b2f 100644 --- a/go.mod +++ b/go.mod @@ -7,13 +7,17 @@ require ( github.com/kataras/pio v0.0.0-20190103105442-ea782b38602d // indirect github.com/labstack/echo v3.3.10+incompatible github.com/labstack/gommon v0.3.0 // indirect + github.com/microcosm-cc/bluemonday v1.0.2 github.com/polaris1119/goutils v0.0.0-20190815094239-73c47df9b896 + github.com/russross/blackfriday v1.5.2 github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337 // indirect + github.com/speps/go-hashids v2.0.0+incompatible github.com/spf13/cast v1.3.0 github.com/spf13/cobra v0.0.5 github.com/spf13/pflag v1.0.5 github.com/spf13/viper v1.4.0 github.com/zouhuigang/golog v0.0.0-20190624001437-99c81de45f40 + github.com/zouhuigang/package v0.0.0-20190928004120-6174f77677ed github.com/zouhuigang/package/ztime v0.0.0-20190928004120-6174f77677ed gopkg.in/yaml.v2 v2.2.2 ) diff --git a/http/controller/md.go b/http/controller/md.go index d1804a7..12ab73b 100644 --- a/http/controller/md.go +++ b/http/controller/md.go @@ -3,14 +3,19 @@ package controller import ( "fmt" "fox/system" + "io/ioutil" + "path" + "time" // "fmt" - // "html/template" "fox/inits/parse" + "fox/util" + "html/template" "os" "github.com/labstack/echo" - // "github.com/microcosm-cc/bluemonday" - // "github.com/russross/blackfriday" + "github.com/microcosm-cc/bluemonday" + "github.com/russross/blackfriday" + "github.com/zouhuigang/package/zfile" ) type MdController struct{} @@ -29,8 +34,67 @@ type TitleMeta struct { //注册路由 func (this *MdController) RegisterRoute(g *echo.Group) { g.GET("/", this.Editor) - g.GET("/make/file", this.mk) - // g.GET("/vm/:id", this.View) + g.POST("/make/file", this.mk) + g.POST("/markdown/info", this.mdInfo) +} + +//文件列表 +type File_list struct { + ID int64 `json:"-"` // xorm默认自动递增 + Eid string + IsDir bool //是否是目录 + Dir string `xorm:"varchar(500)"` //绝对目录 + Path string `xorm:"varchar(500)"` + Ext string `xorm:"varchar(50)"` //后缀 + Name string `xorm:"varchar(200)"` //名称 + Icon string `xorm:"varchar(20)"` //图标 + Size int64 + SizeStr string `xorm:"varchar(20)"` + CreatedAt time.Time `xorm:"created"` + ModTime int64 `xorm:"updated"` +} + +func scan(rootPath string) (error, []*File_list) { //扫描文件 + err := zfile.PathExistsAndMkDir(rootPath) + if err != nil { + return err, nil + } + + err, filelist := util.Walkdir(rootPath) + if err != nil { + return err, nil + } + + //排除目录 + mFileList := make([]*File_list, 0) + for _, cur_f := range filelist { + var icon string = "" + //排除不是word的文档 + if cur_f.Ext == ".md" { + icon = "icon-md" + } else { + continue + } + + //if folder := dest + filepath.Dir(f.Name); !strings.Contains(folder, "__MACOSX") {} + + sizeStr := zfile.FormatByte(zfile.Interface2Int(cur_f.Size)) + cur_file := &File_list{} + cur_file.IsDir = cur_f.IsDir + cur_file.Dir = cur_f.Dir + cur_file.Path = cur_f.Path + cur_file.Ext = cur_f.Ext + cur_file.Name = cur_f.Name + cur_file.Size = cur_f.Size + cur_file.CreatedAt = time.Now() + cur_file.ModTime = cur_f.ModTime + cur_file.Icon = icon + cur_file.SizeStr = sizeStr + mFileList = append(mFileList, cur_file) + + } + + return nil, mFileList } func (MdController) Editor(ctx echo.Context) error { @@ -53,59 +117,64 @@ func (MdController) Editor(ctx echo.Context) error { pages = fmt.Sprintf("%s,%s", pages, "compoents/left2.html") pages = fmt.Sprintf("%s,%s", pages, "compoents/preview.html") pages = fmt.Sprintf("%s,%s", pages, "compoents/save.html") + + //读取本地文件 + _, fileList := scan(parse.EnvConfig.CmdRoot) + data["fileList"] = fileList return render(ctx, pages, data) } -// func (MdController) View(ctx echo.Context) error { -// data := map[string]interface{}{} -// data, is_open := model.Welcome.CommonHeaderFooter(ctx, 12) -// if !is_open { //停机维护中。。 -// return render(ctx, "welcome/repairs.html", data) -// } -// md, err := model.MdModelFindById(ctx, string(ctx.Param("id"))) -// if err != nil { -// return render(ctx, "markdown/no-view.html,common/template.html", data) -// } - -// if md.Is_open == 0 { -// return render(ctx, "markdown/no-view.html,common/template.html", data) -// } - -// //解密笔记 -// session1 := model.Session.GetCookieSession(ctx, "MDDCLS") -// sharePassword, _ := session1.Values["MDDCL"] -// if md.Is_open == 2 && md.Password_code != sharePassword { -// data["id"] = md.Id -// return render(ctx, "markdown/pass-view.html,common/template.html", data) -// } -// var output template.HTML -// if md.Is_html == 0 { -// unsafe := blackfriday.MarkdownCommon([]byte(md.Content)) -// outputHtml := bluemonday.UGCPolicy().SanitizeBytes(unsafe) -// output = template.HTML(outputHtml) -// } else { -// output = template.HTML(md.Content) -// } - -// bgMp3, _ := model.MdModelMp3ById(md.Bg_mp3) -// data["output"] = output -// data["info"] = md -// data["bgMp3"] = bgMp3 -// return render(ctx, "markdown/view.html,common/template.html", data) -// } +func (MdController) mdInfo(ctx echo.Context) error { + data := map[string]interface{}{} + fileName := ctx.FormValue("fileName") + + if fileName == "" { + return system.ResponeJson(ctx, system.ErrUnknown, data, "文件路径不能为空") + } + + //检测文件是否存在 + if !zfile.IsFileExist(fileName) { + return system.ResponeJson(ctx, system.ErrUnknown, data, "文件不存在") + } + + //读取文件内容 + f, err := ioutil.ReadFile(fileName) + if err != nil { + return system.ResponeJson(ctx, system.ErrUnknown, data, err.Error()) + } + + // content := template.HTML(blackfriday.Run(f)) + + var output template.HTML + unsafe := blackfriday.MarkdownCommon(f) + outputHtml := bluemonday.UGCPolicy().SanitizeBytes(unsafe) + output = template.HTML(outputHtml) + + data["output"] = output + data["info"] = string(f) + return system.ResponeJson(ctx, system.ErrOk, data) +} //创建本地文件 func (this *MdController) mk(ctx echo.Context) error { data := map[string]interface{}{} - fileName := ctx.QueryParam("fileName") + //fileName := ctx.QueryParam("fileName") + fileName := ctx.FormValue("fileName") if fileName == "" { return system.ResponeJson(ctx, system.ErrUnknown, data, "文件名不能为空") } - f, err := os.Create(parse.EnvConfig.Root + fileName + ".md") + rfile := path.Join(parse.EnvConfig.CmdRoot, fileName+".md") + + //检测文件是否存在 + if zfile.IsFileExist(rfile) { + return system.ResponeJson(ctx, system.ErrUnknown, data, "文件已存在") + } + + f, err := os.Create(rfile) defer f.Close() if err != nil { return system.ResponeJson(ctx, system.ErrUnknown, data) diff --git a/util/file.go b/util/file.go index 0dadb28..560eb15 100644 --- a/util/file.go +++ b/util/file.go @@ -1,16 +1,13 @@ -// Copyright 2014 The StudyGolang Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. -// http://studygolang.com -// Author: polaris polaris@studygolang.com - package util import ( + "io/ioutil" "log" "os" "path/filepath" "strings" + "sync" + "github.com/zouhuigang/package/zfileutil" ) // 检查文件或目录是否存在 @@ -67,3 +64,68 @@ func GetCurrentDirectory() string { } return strings.Replace(dir, "\\", "/", -1) //将\替换成/ } + +func Walkdir(path string) (error, []zfileutil.FileList) { + var wg sync.WaitGroup + + //读取文件 + var filelist []zfileutil.FileList + c := make(chan []zfileutil.FileList) + is_success := make(chan bool) + + dir, err := ioutil.ReadDir(path) + if err != nil { + return err, nil + } + for _, fi := range dir { + fpath := filepath.FromSlash(path + "/" + fi.Name()) + + if fi.IsDir() { + if strings.HasPrefix(fi.Name(), ".") { + continue + } + if strings.HasPrefix(fi.Name(), "..") { + continue + } + if strings.Contains(fi.Name(), "lost+found") { + continue + } + + if strings.Contains(fi.Name(), "fox.theme") { + continue + } + wg.Add(1) + go scanDir(&wg, fpath, c) + } else { + cur_file := zfileutil.GetFormatFileInfo(fpath, fi) + filelist = append(filelist, cur_file) + } + } + + //一直阻塞直到chan c关闭 + go func() { + for { + select { + case result := <-c: + filelist = append(filelist, result...) + case <-is_success: + return + } + } + }() + + wg.Wait() + + is_success <- true + return nil, filelist + +} + +func scanDir(wg *sync.WaitGroup, rootPath string, c chan []zfileutil.FileList) { + defer wg.Done() + filelist := make([]zfileutil.FileList, 0) + if f, _ := zfileutil.ScanFiles(rootPath); len(f) > 0 { + filelist = append(filelist, f...) + } + c <- filelist +} diff --git a/util/hashids.go b/util/hashids.go new file mode 100644 index 0000000..9082115 --- /dev/null +++ b/util/hashids.go @@ -0,0 +1,26 @@ +package util + +import ( + "github.com/speps/go-hashids" +) + +//salt 盐值 +const salt = "fox_" + +//Encode 混淆 +func Encode(data int) string { + hd := hashids.NewData() + hd.Salt = salt + h, _ := hashids.NewWithData(hd) + e, _ := h.Encode([]int{data}) + return e +} + +//Decode 还原混淆 +func Decode(data string) int { + hd := hashids.NewData() + hd.Salt = salt + h, _ := hashids.NewWithData(hd) + e, _ := h.DecodeWithError(data) + return e[0] +} \ No newline at end of file diff --git "a/\345\217\263\351\224\256\350\217\234\345\215\225.reg" "b/\345\217\263\351\224\256\350\217\234\345\215\225.reg" new file mode 100644 index 0000000..84f2076 --- /dev/null +++ "b/\345\217\263\351\224\256\350\217\234\345\215\225.reg" @@ -0,0 +1,26 @@ +Windows Registry Editor Version 5.00 + +[HKEY_CLASSES_ROOT\*\shell\fox] +@="Open with Fox" +"Icon"="D:\\workspacego\\src\\fox\\fox.exe" + +[HKEY_CLASSES_ROOT\*\shell\fox\command] +@="\"D:\\workspacego\\src\\fox\\fox.exe\" \"%1\"" + +Windows Registry Editor Version 5.00 + +[HKEY_CLASSES_ROOT\Directory\shell\fox] +@="Open with Fox" +"Icon"="D:\\workspacego\\src\\fox\\fox.exe" + +[HKEY_CLASSES_ROOT\Directory\shell\fox\command] +@="\"D:\\workspacego\\src\\fox\\fox.exe\" \"%V\"" + +Windows Registry Editor Version 5.00 + +[HKEY_CLASSES_ROOT\Directory\Background\shell\fox] +@="Open with Fox" +"Icon"="D:\\workspacego\\src\\fox\\fox.exe" + +[HKEY_CLASSES_ROOT\Directory\Background\shell\fox\command] +@="\"D:\\workspacego\\src\\fox\\fox.exe\" \"%V\"" \ No newline at end of file