Skip to content

Commit

Permalink
扫描本地目录
Browse files Browse the repository at this point in the history
  • Loading branch information
zouhuigang committed Oct 12, 2019
1 parent 3616192 commit feafeb1
Show file tree
Hide file tree
Showing 6 changed files with 239 additions and 50 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ fox.wiki/*
fox.exe

fox.wiki

go.sum
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
157 changes: 113 additions & 44 deletions http/controller/md.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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 {
Expand All @@ -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)
Expand Down
74 changes: 68 additions & 6 deletions util/file.go
Original file line number Diff line number Diff line change
@@ -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 [email protected]

package util

import (
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"sync"
"github.com/zouhuigang/package/zfileutil"
)

// 检查文件或目录是否存在
Expand Down Expand Up @@ -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
}
26 changes: 26 additions & 0 deletions util/hashids.go
Original file line number Diff line number Diff line change
@@ -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]
}
26 changes: 26 additions & 0 deletions 右键菜单.reg
Original file line number Diff line number Diff line change
@@ -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\""

0 comments on commit feafeb1

Please sign in to comment.