|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- package qiniu
-
- import (
- "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/e"
- "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/md"
- zhios_third_party_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/utils"
- "fmt"
- "strings"
- )
-
- func ImgReqUpload(arg map[string]string, uid, dirName, fname, callbackUrl string, fsize int64) (interface{}, error) {
- ext := zhios_third_party_utils.FileExt(fname)
- if err := initStg(arg, fsize, ext); err != nil {
- return nil, err
- }
- newName := dirName + "_" + fmt.Sprintf("%010s", uid)
- // 默认都加时间戳
- newName += "_" + zhios_third_party_utils.FormatNanoUnix() + zhios_third_party_utils.RandString(4, "0123456789")
- newName += "." + ext
- f := &md.FileCallback{
- Uid: uid,
- DirId: md.FileUserDir[dirName],
- FileName: newName,
- }
- return ReqImgUpload(f, callbackUrl), nil
- }
-
- func initStg(stgInfo map[string]string, fsize int64, ext string) error {
- // todo 目前仅支持七牛
- if v, ok := stgInfo[md.KEY_CFG_FILE_REGION]; !ok || v == "" {
- return e.NewErrCode(e.ERR_CFG)
- }
- if v, ok := stgInfo[md.KEY_CFG_FILE_AK]; !ok || v == "" {
- return e.NewErrCode(e.ERR_CFG)
- }
- if v, ok := stgInfo[md.KEY_CFG_FILE_SK]; !ok || v == "" {
- return e.NewErrCode(e.ERR_CFG)
- }
- if v, ok := stgInfo[md.KEY_CFG_FILE_BUCKET]; !ok || v == "" {
- return e.NewErrCode(e.ERR_CFG)
- }
- if v, ok := stgInfo[md.KEY_CFG_FILE_SCHEME]; !ok || v == "" {
- stgInfo[md.KEY_CFG_FILE_SCHEME] = "http"
- }
- Init(stgInfo[md.KEY_CFG_FILE_AK], stgInfo[md.KEY_CFG_FILE_SK], stgInfo[md.KEY_CFG_FILE_BUCKET], stgInfo[md.KEY_CFG_FILE_REGION], stgInfo[md.KEY_CFG_FILE_SCHEME])
- if v, ok := stgInfo[md.KEY_CFG_FILE_HOST]; !ok || v == "" {
- var err error
- stgInfo[md.KEY_CFG_FILE_HOST], err = BucketGetDomain(stgInfo[md.KEY_CFG_FILE_BUCKET])
- if err != nil {
- return e.NewErrCode(e.ERR_CFG)
- }
- }
- // 头像缩略图参数
- if v, ok := stgInfo[md.KEY_CFG_FILE_AVATAR_THUMBNAIL]; !ok || v == "" {
- stgInfo[md.KEY_CFG_FILE_AVATAR_THUMBNAIL] = "?imageView2/1/w/200/h/200/format/png"
- }
-
- // 检查文件大小限制
- if v, ok := stgInfo[md.KEY_CFG_FILE_MAX_SIZE]; ok && v != "" && zhios_third_party_utils.StrToInt64(v) < fsize {
- return e.NewErrCode(e.ERR_FILE_MAX_SIZE)
- }
- // 检查文件后缀
- if v, ok := stgInfo[md.KEY_CFG_FILE_EXT]; ok && v != "" && !strings.Contains(v, ext) {
- return e.NewErrCode(e.ERR_FILE_EXT)
- }
- return nil
- }
|