golang-im聊天
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.

svc_file_upload.go 2.8 KiB

2 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package svc
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "gim/internal/business/comm/md"
  7. "gim/internal/business/comm/service/qiniu"
  8. "gim/internal/business/comm/utils"
  9. "gim/internal/business/comm/utils/logx"
  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. ctx,
  33. md.KEY_CFG_FILE_BUCKET,
  34. md.KEY_CFG_FILE_HOST,
  35. md.KEY_CFG_FILE_AK,
  36. md.KEY_CFG_FILE_SK,
  37. md.KEY_CFG_FILE_PVD,
  38. md.KEY_CFG_FILE_REGION,
  39. md.KEY_CFG_FILE_MAX_SIZE,
  40. md.KEY_CFG_FILE_EXT,
  41. md.KEY_CFG_FILE_SCHEME,
  42. )
  43. if stgInfo == nil {
  44. return errors.New("获取配置信息出错")
  45. }
  46. // todo 目前仅支持七牛
  47. if v, ok := stgInfo[md.KEY_CFG_FILE_PVD]; !ok || v != "qiniu" {
  48. return errors.New("file_provider error")
  49. }
  50. if v, ok := stgInfo[md.KEY_CFG_FILE_REGION]; !ok || v == "" {
  51. return errors.New("file_bucket_region error")
  52. }
  53. if v, ok := stgInfo[md.KEY_CFG_FILE_AK]; !ok || v == "" {
  54. return errors.New("file_access_key error")
  55. }
  56. if v, ok := stgInfo[md.KEY_CFG_FILE_SK]; !ok || v == "" {
  57. return errors.New("file_secret_key error")
  58. }
  59. if v, ok := stgInfo[md.KEY_CFG_FILE_BUCKET]; !ok || v == "" {
  60. return errors.New("file_bucket error")
  61. }
  62. if v, ok := stgInfo[md.KEY_CFG_FILE_SCHEME]; !ok || v == "" {
  63. stgInfo[md.KEY_CFG_FILE_SCHEME] = "http"
  64. SysCfgSet(ctx, md.KEY_CFG_FILE_SCHEME, stgInfo[md.KEY_CFG_FILE_SCHEME], "文件域名HTTP协议")
  65. }
  66. 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])
  67. if v, ok := stgInfo[md.KEY_CFG_FILE_HOST]; !ok || v == "" {
  68. var err error
  69. stgInfo[md.KEY_CFG_FILE_HOST], err = qiniu.BucketGetDomain(stgInfo[md.KEY_CFG_FILE_BUCKET])
  70. if err != nil {
  71. logx.Error(err)
  72. return errors.New("服务器配置错误")
  73. }
  74. SysCfgSet(ctx, md.KEY_CFG_FILE_HOST, stgInfo[md.KEY_CFG_FILE_HOST], "文件域名地址")
  75. }
  76. // 检查文件大小限制
  77. if v, ok := stgInfo[md.KEY_CFG_FILE_MAX_SIZE]; ok && v != "" && utils.StrToInt64(v) < fSize {
  78. return errors.New("file_user_upload_max_size error")
  79. }
  80. // 检查文件后缀
  81. if v, ok := stgInfo[md.KEY_CFG_FILE_EXT]; ok && v != "" && !strings.Contains(v, ext) {
  82. return errors.New("file_ext error")
  83. }
  84. return nil
  85. }