增加上传接口

This commit is contained in:
hoteas 2021-07-04 04:03:29 +08:00
parent efb6cb980e
commit 05c8159601
3 changed files with 48 additions and 4 deletions

View File

@ -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},
}

View File

@ -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": "",

View File

@ -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) {