|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- package svc
-
- import (
- "applet/app/db"
- "applet/app/e"
- "applet/app/lib/qiniu"
- "applet/app/md"
- "applet/app/utils"
- "errors"
- "fmt"
- "github.com/gin-gonic/gin"
- "strings"
- "time"
- )
-
- // 请求文件上传
- func ImgReqUpload(c *gin.Context, uid, dirName, fname, callbackUrl string, fsize int64) (interface{}, error) {
- ext := utils.FileExt(fname)
- if err := initStg(c, fsize, ext); err != nil {
- return nil, err
- }
- pureFileName := strings.Replace(fname, "."+ext, "", 1)
- pureFileName += "-" + utils.RandString(6, utils.AnyToString(time.Now().UnixNano()))
- newName := dirName + "/" + pureFileName + "." + ext
-
- f := &md.FileCallback{
- Uid: uid,
- DirId: dirName,
- FileName: newName,
- }
- return qiniu.ReqImgUpload(f, callbackUrl), nil
- }
-
- func initStg(c *gin.Context, fsize int64, ext string) error {
- // 获取上传配置
- fileCfg := make(map[string]string)
- masterListCfgDb := db.MasterListCfgDb{}
- masterListCfgDb.Set()
- fileCfg[md.KEY_CFG_FILE_BUCKET] = masterListCfgDb.MasterListCfgGetOneData("0", md.KEY_CFG_FILE_BUCKET)
- fileCfg[md.KEY_CFG_FILE_HOST] = masterListCfgDb.MasterListCfgGetOneData("0", md.KEY_CFG_FILE_HOST)
- fileCfg[md.KEY_CFG_FILE_AK] = masterListCfgDb.MasterListCfgGetOneData("0", md.KEY_CFG_FILE_AK)
- fileCfg[md.KEY_CFG_FILE_SK] = masterListCfgDb.MasterListCfgGetOneData("0", md.KEY_CFG_FILE_SK)
- fileCfg[md.KEY_CFG_FILE_PVD] = masterListCfgDb.MasterListCfgGetOneData("0", md.KEY_CFG_FILE_PVD)
- fileCfg[md.KEY_CFG_FILE_MAX_SIZE] = masterListCfgDb.MasterListCfgGetOneData("0", md.KEY_CFG_FILE_MAX_SIZE)
- fileCfg[md.KEY_CFG_FILE_EXT] = masterListCfgDb.MasterListCfgGetOneData("0", md.KEY_CFG_FILE_EXT)
- fileCfg[md.KEY_CFG_FILE_SCHEME] = masterListCfgDb.MasterListCfgGetOneData("0", md.KEY_CFG_FILE_SCHEME)
- utils.FilePutContents("initStg", fmt.Sprintf("[KEY_CFG_FILE_BUCKET]:%s ; [KEY_CFG_FILE_HOST]:%s ; [KEY_CFG_FILE_AK]::%s;"+
- " [KEY_CFG_FILE_SK]::%s, ;[KEY_CFG_FILE_PVD]::%s, ;[KEY_CFG_FILE_REGION]::%s, ;[KEY_CFG_FILE_MAX_SIZE]::%s, ;[KEY_CFG_FILE_EXT]::%s, ;[KEY_CFG_FILE_SCHEME]::%s, "+
- ">>>>>>>>>>>>>>>>>>>>", fileCfg[md.KEY_CFG_FILE_BUCKET], fileCfg[md.KEY_CFG_FILE_HOST], fileCfg[md.KEY_CFG_FILE_AK], fileCfg[md.KEY_CFG_FILE_SK],
- fileCfg[md.KEY_CFG_FILE_PVD], fileCfg[md.KEY_CFG_FILE_REGION], fileCfg[md.KEY_CFG_FILE_MAX_SIZE], fileCfg[md.KEY_CFG_FILE_EXT], fileCfg[md.KEY_CFG_FILE_SCHEME]))
- for _, v := range fileCfg {
- if v == "" {
- return errors.New("上传配置不完整")
- }
- }
-
- qiniu.Init(fileCfg[md.KEY_CFG_FILE_AK], fileCfg[md.KEY_CFG_FILE_SK], fileCfg[md.KEY_CFG_FILE_BUCKET], fileCfg[md.KEY_CFG_FILE_REGION], fileCfg[md.KEY_CFG_FILE_SCHEME])
-
- // 检查文件大小限制
- if utils.StrToInt64(fileCfg[md.KEY_CFG_FILE_MAX_SIZE]) < fsize {
- return e.NewErrCode(e.ERR_FILE_MAX_SIZE)
- }
- // 检查文件后缀
- if !strings.Contains(fileCfg[md.KEY_CFG_FILE_EXT], ext) {
- return e.NewErrCode(e.ERR_FILE_EXT)
- }
- return nil
- }
|