附近小店
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 

105 rader
3.1 KiB

  1. package svc
  2. import (
  3. "fmt"
  4. "strings"
  5. "applet/app/e"
  6. "applet/app/lib/qiniu"
  7. "applet/app/md"
  8. "applet/app/utils"
  9. "applet/app/utils/logx"
  10. "github.com/gin-gonic/gin"
  11. )
  12. // 请求文件上传
  13. func ImgReqUpload(c *gin.Context, uid, dirName, fname, callbackUrl string, fsize int64) (interface{}, error) {
  14. ext := utils.FileExt(fname)
  15. if err := initStg(c, fsize, ext); err != nil {
  16. return nil, err
  17. }
  18. // logx.Warn(uid)
  19. newName := dirName + "_" + fmt.Sprintf("%010s", uid)
  20. // if dirName == md.FILE_DIR_FEEDBACK || dirName == md.FILE_DIR_STYLE {
  21. // newName += "_" + utils.FormatNanoUnix() + utils.RandString(4, "0123456789")
  22. // }
  23. // 默认都加时间戳
  24. newName += "_" + utils.FormatNanoUnix() + utils.RandString(4, "0123456789")
  25. newName += "." + ext
  26. f := &md.FileCallback{
  27. Uid: uid,
  28. DirId: md.FileUserDir[dirName],
  29. FileName: newName,
  30. }
  31. // logx.Warn(f.Uid)
  32. return qiniu.ReqImgUpload(f, callbackUrl), nil
  33. }
  34. func initStg(c *gin.Context, fsize int64, ext string) error {
  35. // 获取上传配置
  36. stgInfo := SysCfgFind(
  37. c,
  38. md.KEY_CFG_FILE_BUCKET,
  39. md.KEY_CFG_FILE_HOST,
  40. md.KEY_CFG_FILE_AK,
  41. md.KEY_CFG_FILE_SK,
  42. md.KEY_CFG_FILE_PVD,
  43. md.KEY_CFG_FILE_REGION,
  44. md.KEY_CFG_FILE_MAX_SIZE,
  45. md.KEY_CFG_FILE_EXT,
  46. md.KEY_CFG_FILE_SCHEME,
  47. md.KEY_CFG_FILE_AVATAR_THUMBNAIL,
  48. )
  49. //?imageView2/1/w/120/h/120/format/webp/interlace/1
  50. if stgInfo == nil {
  51. return e.NewErrCode(e.ERR_CFG)
  52. }
  53. // todo 目前仅支持七牛
  54. if v, ok := stgInfo[md.KEY_CFG_FILE_PVD]; !ok || v != "qiniu" {
  55. return e.NewErrCode(e.ERR_CFG)
  56. }
  57. if v, ok := stgInfo[md.KEY_CFG_FILE_REGION]; !ok || v == "" {
  58. return e.NewErrCode(e.ERR_CFG)
  59. }
  60. if v, ok := stgInfo[md.KEY_CFG_FILE_AK]; !ok || v == "" {
  61. return e.NewErrCode(e.ERR_CFG)
  62. }
  63. if v, ok := stgInfo[md.KEY_CFG_FILE_SK]; !ok || v == "" {
  64. return e.NewErrCode(e.ERR_CFG)
  65. }
  66. if v, ok := stgInfo[md.KEY_CFG_FILE_BUCKET]; !ok || v == "" {
  67. return e.NewErrCode(e.ERR_CFG)
  68. }
  69. if v, ok := stgInfo[md.KEY_CFG_FILE_SCHEME]; !ok || v == "" {
  70. stgInfo[md.KEY_CFG_FILE_SCHEME] = "http"
  71. SysCfgSet(c, md.KEY_CFG_FILE_SCHEME, stgInfo[md.KEY_CFG_FILE_SCHEME], "文件域名HTTP协议")
  72. }
  73. qiniu.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])
  74. if v, ok := stgInfo[md.KEY_CFG_FILE_HOST]; !ok || v == "" {
  75. var err error
  76. stgInfo[md.KEY_CFG_FILE_HOST], err = qiniu.BucketGetDomain(stgInfo[md.KEY_CFG_FILE_BUCKET])
  77. if err != nil {
  78. logx.Error(err)
  79. return e.NewErrCode(e.ERR_CFG)
  80. }
  81. SysCfgSet(c, md.KEY_CFG_FILE_HOST, stgInfo[md.KEY_CFG_FILE_HOST], "文件域名地址")
  82. }
  83. // 头像缩略图参数
  84. if v, ok := stgInfo[md.KEY_CFG_FILE_AVATAR_THUMBNAIL]; !ok || v == "" {
  85. SysCfgSet(c, md.KEY_CFG_FILE_AVATAR_THUMBNAIL, "?imageView2/1/w/200/h/200/format/png", "文件用户头像缩略图参数")
  86. }
  87. // 检查文件大小限制
  88. if v, ok := stgInfo[md.KEY_CFG_FILE_MAX_SIZE]; ok && v != "" && utils.StrToInt64(v) < fsize {
  89. return e.NewErrCode(e.ERR_FILE_MAX_SIZE)
  90. }
  91. // 检查文件后缀
  92. if v, ok := stgInfo[md.KEY_CFG_FILE_EXT]; ok && v != "" && !strings.Contains(v, ext) {
  93. return e.NewErrCode(e.ERR_FILE_EXT)
  94. }
  95. return nil
  96. }