From 346879f774c21a7932cacaee341224e6ad5dcda8 Mon Sep 17 00:00:00 2001 From: zhucheer Date: Fri, 27 Dec 2019 16:50:37 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BE=85=E5=8A=A9=E7=BC=96=E8=AF=91=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- captcha/captcha.go | 8 ++- main.go | 108 ++++++++++++++++++++++++++++++- project/config/config.toml | 2 +- project/http/controller/index.go | 4 +- 4 files changed, 116 insertions(+), 6 deletions(-) diff --git a/captcha/captcha.go b/captcha/captcha.go index a2848cb..85322b8 100644 --- a/captcha/captcha.go +++ b/captcha/captcha.go @@ -30,6 +30,12 @@ func CaptchaImgShow(c *app.Context, n int, size ...int) error { return c.ResponseWrite(imgByte) } -func CaptchaVerify(code string) { +func CaptchaVerify(c *app.Context, code string)bool { + codeMd5:=utils.Md5ToString(code) + sessionCode:= c.Session().Get(captchaSessionName) + if codeMd5 == sessionCode{ + return true + } + return false } diff --git a/main.go b/main.go index 2062dcb..5dd0be3 100644 --- a/main.go +++ b/main.go @@ -13,6 +13,9 @@ import ( "runtime" "strings" "time" + "errors" + "path/filepath" + "io" ) func main() { @@ -48,6 +51,10 @@ func buildProject() { buildProjectWithWindows() } + //移动静态文件 + CopyPath("config", "build/config") + CopyPath("storage", "build/storage") + } // windows下编译打包 @@ -61,9 +68,7 @@ func buildProjectWithWindows() { } projectName := pwdArr[len(pwdArr)-1] - ret, _ := execShell( fmt.Sprintf("go build -o build/%s.exe", projectName)) - - fmt.Println(projectName, ret) + execShell( fmt.Sprintf("go build -o build/%s.exe", projectName)) } func createProject(projectName string) { @@ -188,6 +193,103 @@ func dirDot() string { } } + + +//拷贝文件 +func CopyFile(src, dst string) bool { + if len(src) == 0 || len(dst) == 0 { + return false + } + srcFile, e := os.OpenFile(src, os.O_RDONLY, os.ModePerm) + if e != nil { + panic(e) + } + defer srcFile.Close() + + dst = strings.Replace(dst, "\\", "/", -1) + dstPathArr := strings.Split(dst, "/") + dstPathArr = dstPathArr[0 : len(dstPathArr)-1] + dstPath := strings.Join(dstPathArr, "/") + + dstFileInfo := GetFileInfo(dstPath) + if dstFileInfo == nil { + if e := os.MkdirAll(dstPath, os.ModePerm); e != nil { + panic(e) + } + } + //这里要把O_TRUNC 加上,否则会出现新旧文件内容出现重叠现象 + dstFile, e := os.OpenFile(dst, os.O_CREATE|os.O_TRUNC|os.O_RDWR, os.ModePerm) + if e != nil { + panic(e) + } + defer dstFile.Close() + //fileInfo, e := srcFile.Stat() + //fileInfo.Size() > 1024 + //byteBuffer := make([]byte, 10) + if _, e := io.Copy(dstFile, srcFile); e != nil { + panic(e) + } else { + return true + } + +} + +//拷贝目录 +func CopyPath(src, dst string) bool { + srcFileInfo := GetFileInfo(src) + if srcFileInfo == nil || !srcFileInfo.IsDir() { + return false + } + err := filepath.Walk(src, func(path string, info os.FileInfo, err error) error { + if err != nil { + panic(err) + } + relationPath := strings.Replace(path, src, "/", -1) + dstPath := strings.TrimRight(strings.TrimRight(dst, "/"), "\\") + relationPath + if !info.IsDir() { + if CopyFile(path, dstPath) { + return nil + } else { + return errors.New(path + " copy fail") + } + } else { + if _, err := os.Stat(dstPath); err != nil { + if os.IsNotExist(err) { + if err := os.MkdirAll(dstPath, os.ModePerm); err != nil { + panic(err) + return err + } else { + return nil + } + } else { + panic(err) + return err + } + } else { + return nil + } + } + }) + + if err != nil { + panic(err) + } + return true + +} + +//判断文件或目录是否存在 +func GetFileInfo(src string) os.FileInfo { + if fileInfo, e := os.Stat(src); e != nil { + if os.IsNotExist(e) { + return nil + } + return nil + } else { + return fileInfo + } +} + func getProjectPath(subdirs ...string) string { basepath := getGoPath() diff --git a/project/config/config.toml b/project/config/config.toml index 94575d2..9acbbb9 100644 --- a/project/config/config.toml +++ b/project/config/config.toml @@ -8,7 +8,7 @@ [app.logger] level = "INFO" type = "text" - path = "./logs" + path = "" syncInterval = 2 [app.session] isOpen = true diff --git a/project/http/controller/index.go b/project/http/controller/index.go index a12d4e0..35fca59 100644 --- a/project/http/controller/index.go +++ b/project/http/controller/index.go @@ -55,8 +55,10 @@ func VerifyImg(c *app.Context) error { fmt.Println(code) + ret:= captcha.CaptchaVerify(c, code) + return c.ToJson(map[string]interface{}{ - "result": "upload success", + "result": ret, }) }