From 05c8159601dd10301aaa3fefb98604c3fa361adc Mon Sep 17 00:00:00 2001 From: hoteas <925970985@qq.com> Date: Sun, 4 Jul 2021 04:03:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=B8=8A=E4=BC=A0=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/config.go | 4 ++-- example/config/config.json | 4 ++-- example/main.go | 44 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/code/config.go b/code/config.go index 05482fc..f461426 100644 --- a/code/config.go +++ b/code/config.go @@ -49,7 +49,7 @@ type ColumnShow struct { Info bool Must bool Type string //空字符串表示 - Strict bool + Strict bool //name严格匹配必须是这个词才行 } var ColumnNameType = []ColumnShow{ @@ -86,6 +86,6 @@ var ColumnNameType = []ColumnShow{ {"file", false, true, true, false, "file", false}, {"age", false, true, true, false, "", false}, {"email", false, true, true, false, "", false}, - + {"time", true, false, false, false, "", false}, {"level", false, false, true, false, "", false}, } diff --git a/example/config/config.json b/example/config/config.json index 7d9edf8..5437206 100644 --- a/example/config/config.json +++ b/example/config/config.json @@ -12,8 +12,8 @@ "crossDomain": "auto", "db": { "mysql": { - "host": "192.168.6.250", - "name": "reg_gqt", + "host": "192.168.6.253", + "name": "hotimego", "password": "root", "port": "3306", "prefix": "", diff --git a/example/main.go b/example/main.go index 32c7ba0..94cf62d 100644 --- a/example/main.go +++ b/example/main.go @@ -2,10 +2,14 @@ package main import ( "../../hotime" + "../../hotime/common" "./admin" "errors" "fmt" "golang.org/x/net/websocket" + "io" + "os" + "strings" "time" ) @@ -14,6 +18,46 @@ func main() { appIns := hotime.Init("config/config.json") //RESTfull接口适配 appIns.SetConnectListener(func(context *hotime.Context) bool { + //文件上传接口 + if len(context.RouterString) == 1 && context.RouterString[0] == "file" && context.Req.Method == "POST" { + + //读取网络文件 + fi, fheader, err := context.Req.FormFile("file") + if err != nil { + context.Display(3, err) + return false + + } + filePath := context.Config.GetString("filePath") + if filePath == "" { + filePath = "file/2006/01/02/" + } + + path := time.Now().Format(filePath) + e := os.MkdirAll(context.Config.GetString("tpt")+"/"+path, os.ModeDir) + if e != nil { + context.Display(3, e) + return false + } + filePath = path + common.Md5(common.ObjToStr(common.RandX(100000, 9999999))) + fheader.Filename[strings.LastIndex(fheader.Filename, "."):] + newFile, e := os.Create(context.Config.GetString("tpt") + "/" + filePath) + + if e != nil { + context.Display(3, e) + return false + } + + _, e = io.Copy(newFile, fi) + + if e != nil { + context.Display(3, e) + return false + } + + context.Display(0, filePath) + return false + } + if len(context.RouterString) < 2 || len(context.RouterString) > 3 || !(context.Router[context.RouterString[0]] != nil && context.Router[context.RouterString[0]][context.RouterString[1]] != nil) {