123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- package controllers
- import (
- "ColdVerify_server/conf"
- "ColdVerify_server/lib"
- "crypto/md5"
- "fmt"
- beego "github.com/beego/beego/v2/server/web"
- "math/rand"
- "os"
- "path"
- "time"
- )
- type UpFileController struct {
- beego.Controller
- }
- func (c *UpFileController) UpFile() {
- f, h, _ := c.GetFile("file") //获取上传的文件
- ext := path.Ext(h.Filename)
- //验证后缀名是否符合要求
- var AllowExtMap map[string]bool = map[string]bool{
- ".jpg": true,
- ".jpeg": true,
- ".png": true,
- ".pdf": true,
- }
- if _, ok := AllowExtMap[ext]; !ok {
- c.Ctx.WriteString("后缀名不符合上传要求")
- return
- }
- //创建目录
- uploadDir := conf.Oss_file // + time.Now().Format("2006/01/02/")
- err := os.MkdirAll(uploadDir, 777)
- if err != nil {
- c.Ctx.WriteString(fmt.Sprintf("%v", err))
- return
- }
- //构造文件名称
- rand.Seed(time.Now().UnixNano())
- randNum := fmt.Sprintf("%d", rand.Intn(9999)+1000)
- hashName := md5.Sum([]byte(time.Now().Format("2006_01_02_15_04_05_") + randNum))
- fileName := fmt.Sprintf("%x", hashName) + ext
- //this.Ctx.WriteString( fileName )
- fpath := uploadDir + fileName
- println(fpath)
- defer f.Close() //关闭上传的文件,不然的话会出现临时文件不能清除的情况
- err = c.SaveToFile("file", fpath)
- if err != nil {
- c.Ctx.WriteString(fmt.Sprintf("%v", err))
- }
- c.Ctx.WriteString("{\n \"code\": 0\n ,\"msg\": \"ok!\"\n ,\"data\": {\n \"src\": \"" + conf.Oss + fileName + "\"\n }\n} ")
- }
- // 列表 -
- func (c *UpFileController) ConfigUpFileToken() {
- // 验证登录 User_r, User_x
- //b_, User_r, _ := Account.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
- //if !b_ {
- // c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
- // c.ServeJSON()
- // return
- //}
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: lib.UploadToken(c.GetString("T_suffix"))}
- c.ServeJSON()
- return
- }
|