|
- package svc
-
- import (
- "applet/app/db"
- "applet/app/e"
- "applet/app/enum"
- "applet/app/lib/qiniu"
- "applet/app/md"
- "applet/app/utils"
- "errors"
- "fmt"
- "strings"
- "time"
- )
-
- // 请求文件上传
- func ImgReqUpload(uid, dirName, fname, callbackUrl string, fsize int64) (interface{}, error) {
- ext := utils.FileExt(fname)
- if err := initStg(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(fsize int64, ext string) error {
- // 获取上传配置
- fileCfg := make(map[string]string)
- sysCfgDb := db.SysCfgDb{}
- sysCfgDb.Set()
- fileCfg[enum.FileBucket] = sysCfgDb.SysCfgGetWithDb(enum.FileBucket)
- fileCfg[enum.FileBucketHost] = sysCfgDb.SysCfgGetWithDb(enum.FileBucketHost)
- fileCfg[enum.FileAccessKey] = sysCfgDb.SysCfgGetWithDb(enum.FileAccessKey)
- fileCfg[enum.FileSecretKey] = sysCfgDb.SysCfgGetWithDb(enum.FileSecretKey)
- fileCfg[enum.FileBucketRegion] = sysCfgDb.SysCfgGetWithDb(enum.FileBucketRegion)
- fileCfg[enum.FileUserUploadMaxSize] = sysCfgDb.SysCfgGetWithDb(enum.FileUserUploadMaxSize)
- fileCfg[enum.FileExt] = sysCfgDb.SysCfgGetWithDb(enum.FileExt)
- fileCfg[enum.FileBucketScheme] = sysCfgDb.SysCfgGetWithDb(enum.FileBucketScheme)
- 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_REGION]::%s, ;[KEY_CFG_FILE_MAX_SIZE]::%s, ;[KEY_CFG_FILE_EXT]::%s, ;[KEY_CFG_FILE_SCHEME]::%s, "+
- ">>>>>>>>>>>>>>>>>>>>", fileCfg[enum.FileBucket], fileCfg[enum.FileBucketHost], fileCfg[enum.FileAccessKey], fileCfg[enum.FileSecretKey],
- fileCfg[enum.FileBucketRegion], fileCfg[enum.FileUserUploadMaxSize], fileCfg[enum.FileExt], fileCfg[enum.FileBucketScheme]))
- for _, v := range fileCfg {
- if v == "" {
- return errors.New("上传配置不完整")
- }
- }
-
- qiniu.Init(fileCfg[enum.FileAccessKey], fileCfg[enum.FileSecretKey], fileCfg[enum.FileBucket], fileCfg[enum.FileBucketRegion], fileCfg[enum.FileBucketScheme])
-
- // 检查文件大小限制
- if utils.StrToInt64(fileCfg[enum.FileUserUploadMaxSize]) < fsize {
- return e.NewErrCode(e.ERR_FILE_MAX_SIZE)
- }
- // 检查文件后缀
- if !strings.Contains(fileCfg[enum.FileExt], ext) {
- return e.NewErrCode(e.ERR_FILE_EXT)
- }
- return nil
- }
|