智盟项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
2.8 KiB

  1. package svc
  2. import (
  3. "applet/app/db"
  4. "applet/app/e"
  5. "applet/app/lib/qiniu"
  6. "applet/app/md"
  7. "applet/app/utils"
  8. "errors"
  9. "fmt"
  10. "github.com/gin-gonic/gin"
  11. "strings"
  12. "time"
  13. )
  14. // 请求文件上传
  15. func ImgReqUpload(c *gin.Context, uid, dirName, fname, callbackUrl string, fsize int64) (interface{}, error) {
  16. ext := utils.FileExt(fname)
  17. if err := initStg(c, fsize, ext); err != nil {
  18. return nil, err
  19. }
  20. pureFileName := strings.Replace(fname, "."+ext, "", 1)
  21. pureFileName += "-" + utils.RandString(6, utils.AnyToString(time.Now().UnixNano()))
  22. newName := dirName + "/" + pureFileName + "." + ext
  23. f := &md.FileCallback{
  24. Uid: uid,
  25. DirId: dirName,
  26. FileName: newName,
  27. }
  28. return qiniu.ReqImgUpload(f, callbackUrl), nil
  29. }
  30. func initStg(c *gin.Context, fsize int64, ext string) error {
  31. // 获取上传配置
  32. fileCfg := make(map[string]string)
  33. masterListCfgDb := db.MasterListCfgDb{}
  34. masterListCfgDb.Set()
  35. fileCfg[md.KEY_CFG_FILE_BUCKET] = masterListCfgDb.MasterListCfgGetOneData("0", md.KEY_CFG_FILE_BUCKET)
  36. fileCfg[md.KEY_CFG_FILE_HOST] = masterListCfgDb.MasterListCfgGetOneData("0", md.KEY_CFG_FILE_HOST)
  37. fileCfg[md.KEY_CFG_FILE_AK] = masterListCfgDb.MasterListCfgGetOneData("0", md.KEY_CFG_FILE_AK)
  38. fileCfg[md.KEY_CFG_FILE_SK] = masterListCfgDb.MasterListCfgGetOneData("0", md.KEY_CFG_FILE_SK)
  39. fileCfg[md.KEY_CFG_FILE_PVD] = masterListCfgDb.MasterListCfgGetOneData("0", md.KEY_CFG_FILE_PVD)
  40. fileCfg[md.KEY_CFG_FILE_MAX_SIZE] = masterListCfgDb.MasterListCfgGetOneData("0", md.KEY_CFG_FILE_MAX_SIZE)
  41. fileCfg[md.KEY_CFG_FILE_EXT] = masterListCfgDb.MasterListCfgGetOneData("0", md.KEY_CFG_FILE_EXT)
  42. fileCfg[md.KEY_CFG_FILE_SCHEME] = masterListCfgDb.MasterListCfgGetOneData("0", md.KEY_CFG_FILE_SCHEME)
  43. utils.FilePutContents("initStg", fmt.Sprintf("[KEY_CFG_FILE_BUCKET]:%s ; [KEY_CFG_FILE_HOST]:%s ; [KEY_CFG_FILE_AK]::%s;"+
  44. " [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, "+
  45. ">>>>>>>>>>>>>>>>>>>>", fileCfg[md.KEY_CFG_FILE_BUCKET], fileCfg[md.KEY_CFG_FILE_HOST], fileCfg[md.KEY_CFG_FILE_AK], fileCfg[md.KEY_CFG_FILE_SK],
  46. 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]))
  47. for _, v := range fileCfg {
  48. if v == "" {
  49. return errors.New("上传配置不完整")
  50. }
  51. }
  52. 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])
  53. // 检查文件大小限制
  54. if utils.StrToInt64(fileCfg[md.KEY_CFG_FILE_MAX_SIZE]) < fsize {
  55. return e.NewErrCode(e.ERR_FILE_MAX_SIZE)
  56. }
  57. // 检查文件后缀
  58. if !strings.Contains(fileCfg[md.KEY_CFG_FILE_EXT], ext) {
  59. return e.NewErrCode(e.ERR_FILE_EXT)
  60. }
  61. return nil
  62. }