码农笔录博客源码
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

148 rindas
3.9 KiB

pirms 6 gadiem
package controllers
import (
"beeblog/models"
pirms 6 gadiem
"beeblog/service"
pirms 4 gadiem
"crypto/md5"
"fmt"
beego "github.com/beego/beego/v2/server/web"
"math/rand"
"os"
"path"
"time"
pirms 6 gadiem
)
type FileController struct {
beego.Controller
}
func (this *FileController) Upload() {
filename := ""
pirms 6 gadiem
for filename, _ = range this.Ctx.Request.MultipartForm.File {
}
pirms 6 gadiem
f, h, filerr := this.GetFile(filename) //获取上传的文件
if filerr != nil {
fmt.Println("err:", filerr)
this.Data["json"] = models.ReurnError(500, "后缀名不符合上传要求")
this.ServeJSON()
return
}
ext := path.Ext(h.Filename)
//验证后缀名是否符合要求
var AllowExtMap map[string]bool = map[string]bool{
".jpg": true,
".jpeg": true,
pirms 5 gadiem
".gif": true,
pirms 5 gadiem
".png": true,
".GIF": true,
".JPG": true,
".PNG": true,
pirms 6 gadiem
}
if _, ok := AllowExtMap[ext]; !ok {
this.Data["json"] = models.ReurnError(500, "后缀名不符合上传要求")
this.ServeJSON()
return
}
//创建目录
pirms 6 gadiem
urlDir := time.Now().Format("2006/01/02/")
uploadDir, _ := beego.AppConfig.String("uploadDir")
pirms 4 gadiem
uploadDir = uploadDir + urlDir
pirms 6 gadiem
err := os.MkdirAll(uploadDir, 777)
if err != nil {
this.Data["json"] = models.ReurnError(500, "")
this.ServeJSON()
return
}
//构造文件名称
rand.Seed(time.Now().UnixNano())
randNum := fmt.Sprintf("%d", rand.Intn(9999)+1000)
pirms 4 gadiem
hashName := md5.Sum([]byte(time.Now().Format("2006_01_02_15_04_05_") + randNum))
pirms 6 gadiem
fileName := fmt.Sprintf("%x", hashName) + ext
fpath := uploadDir + fileName
pirms 6 gadiem
urlDir += fileName
pirms 6 gadiem
defer f.Close() //关闭上传的文件,不然的话会出现临时文件不能清除的情况
err = this.SaveToFile(filename, fpath)
if err != nil {
fmt.Println(err)
this.Data["json"] = models.ReurnError(500, "")
this.ServeJSON()
return
}
imgUrlPrefix, _ := beego.AppConfig.String("imgUrlPrefix")
pirms 4 gadiem
urlDir = imgUrlPrefix + urlDir
pirms 6 gadiem
this.Data["json"] = models.ReurnData("", urlDir)
pirms 6 gadiem
this.ServeJSON()
return
}
func (this *FileController) HeadImgUpload() {
uid := this.GetSession("userid")
if uid == nil {
this.Data["json"] = models.ReurnError(401, "")
this.ServeJSON()
return
}
filename := "imgfile"
f, h, filerr := this.GetFile(filename) //获取上传的文件
if filerr != nil {
fmt.Println("err:", filerr)
this.Data["json"] = models.ReurnError(500, "后缀名不符合上传要求")
this.ServeJSON()
return
}
ext := path.Ext(h.Filename)
//验证后缀名是否符合要求
var AllowExtMap map[string]bool = map[string]bool{
".jpg": true,
".jpeg": true,
".gif": true,
".png": true,
".GIF": true,
".JPG": true,
".PNG": true,
}
if _, ok := AllowExtMap[ext]; !ok {
this.Data["json"] = models.ReurnError(500, "后缀名不符合上传要求")
this.ServeJSON()
return
}
//创建目录
urlDir := time.Now().Format("2006/01/02/")
uploadDir, _ := beego.AppConfig.String("uploadDir")
pirms 4 gadiem
uploadDir = uploadDir + urlDir
pirms 6 gadiem
err := os.MkdirAll(uploadDir, 777)
if err != nil {
this.Data["json"] = models.ReurnError(500, "")
this.ServeJSON()
return
}
//构造文件名称
rand.Seed(time.Now().UnixNano())
randNum := fmt.Sprintf("%d", rand.Intn(9999)+1000)
pirms 4 gadiem
hashName := md5.Sum([]byte(time.Now().Format("2006_01_02_15_04_05_") + randNum))
pirms 6 gadiem
fileName := fmt.Sprintf("%x", hashName) + ext
fpath := uploadDir + fileName
urlDir += fileName
defer f.Close() //关闭上传的文件,不然的话会出现临时文件不能清除的情况
err = this.SaveToFile(filename, fpath)
if err != nil {
fmt.Println(err)
this.Data["json"] = models.ReurnError(500, "")
this.ServeJSON()
return
}
imgUrlPrefix, _ := beego.AppConfig.String("imgUrlPrefix")
pirms 4 gadiem
urlDir = imgUrlPrefix + urlDir
pirms 6 gadiem
user := &models.User{Id: uid.(int64), Headimg: urlDir}
pirms 6 gadiem
userSerivce := service.UserService{}
if _, err = userSerivce.EditHeadImg(user); err != nil {
pirms 6 gadiem
this.Data["json"] = models.ReurnError(500, "")
this.ServeJSON()
return
}
this.SetSession("headimg", urlDir)
pirms 6 gadiem
this.Data["json"] = models.ReurnData("", urlDir)
pirms 6 gadiem
this.ServeJSON()
return
}