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.
 
 
 
 

92 lines
2.8 KiB

  1. package svc
  2. import (
  3. "context"
  4. "egg-im/internal/business/comm/md"
  5. "egg-im/internal/business/comm/service/qiniu"
  6. "egg-im/internal/business/comm/utils"
  7. "egg-im/internal/business/comm/utils/logx"
  8. "errors"
  9. "fmt"
  10. "strings"
  11. )
  12. // 请求文件上传
  13. func FileReqUpload(ctx context.Context, uid, dirName, fName, callbackUrl string, fSize int64) (interface{}, error) {
  14. ext := utils.FileExt(fName)
  15. if err := initStg(ctx, fSize, ext); err != nil {
  16. return nil, err
  17. }
  18. newName := dirName + "_" + fmt.Sprintf("%010s", uid)
  19. // 默认都加时间戳
  20. newName += "_" + utils.FormatNanoUnix() + utils.RandString(4, "0123456789")
  21. newName += "." + ext
  22. f := &md.FileCallback{
  23. Uid: uid,
  24. DirId: md.FileUserDir[dirName],
  25. FileName: newName,
  26. }
  27. return qiniu.ReqImgUpload(f, callbackUrl), nil
  28. }
  29. func initStg(ctx context.Context, fSize int64, ext string) error {
  30. // 获取上传配置
  31. stgInfo := SysCfgFind(
  32. md.KEY_CFG_FILE_BUCKET,
  33. md.KEY_CFG_FILE_HOST,
  34. md.KEY_CFG_FILE_AK,
  35. md.KEY_CFG_FILE_SK,
  36. md.KEY_CFG_FILE_PVD,
  37. md.KEY_CFG_FILE_REGION,
  38. md.KEY_CFG_FILE_MAX_SIZE,
  39. md.KEY_CFG_FILE_EXT,
  40. md.KEY_CFG_FILE_SCHEME,
  41. )
  42. if stgInfo == nil {
  43. return errors.New("获取配置信息出错")
  44. }
  45. // todo 目前仅支持七牛
  46. if v, ok := stgInfo[md.KEY_CFG_FILE_PVD]; !ok || v != "qiniu" {
  47. return errors.New("file_provider error")
  48. }
  49. if v, ok := stgInfo[md.KEY_CFG_FILE_REGION]; !ok || v == "" {
  50. return errors.New("file_bucket_region error")
  51. }
  52. if v, ok := stgInfo[md.KEY_CFG_FILE_AK]; !ok || v == "" {
  53. return errors.New("file_access_key error")
  54. }
  55. if v, ok := stgInfo[md.KEY_CFG_FILE_SK]; !ok || v == "" {
  56. return errors.New("file_secret_key error")
  57. }
  58. if v, ok := stgInfo[md.KEY_CFG_FILE_BUCKET]; !ok || v == "" {
  59. return errors.New("file_bucket error")
  60. }
  61. if v, ok := stgInfo[md.KEY_CFG_FILE_SCHEME]; !ok || v == "" {
  62. stgInfo[md.KEY_CFG_FILE_SCHEME] = "http"
  63. SysCfgSet(md.KEY_CFG_FILE_SCHEME, stgInfo[md.KEY_CFG_FILE_SCHEME], "文件域名HTTP协议")
  64. }
  65. 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])
  66. if v, ok := stgInfo[md.KEY_CFG_FILE_HOST]; !ok || v == "" {
  67. var err error
  68. stgInfo[md.KEY_CFG_FILE_HOST], err = qiniu.BucketGetDomain(stgInfo[md.KEY_CFG_FILE_BUCKET])
  69. if err != nil {
  70. logx.Error(err)
  71. return errors.New("服务器配置错误")
  72. }
  73. SysCfgSet(md.KEY_CFG_FILE_HOST, stgInfo[md.KEY_CFG_FILE_HOST], "文件域名地址")
  74. }
  75. // 检查文件大小限制
  76. if v, ok := stgInfo[md.KEY_CFG_FILE_MAX_SIZE]; ok && v != "" && utils.StrToInt64(v) < fSize {
  77. return errors.New("file_user_upload_max_size error")
  78. }
  79. // 检查文件后缀
  80. if v, ok := stgInfo[md.KEY_CFG_FILE_EXT]; ok && v != "" && !strings.Contains(v, ext) {
  81. return errors.New("file_ext error")
  82. }
  83. return nil
  84. }