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.

req_img_upload.go 1.4 KiB

пре 2 година
123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package qiniu
  2. import (
  3. "gim/internal/business/comm/md"
  4. "gim/internal/business/comm/utils"
  5. "time"
  6. "github.com/qiniu/api.v7/v7/auth/qbox"
  7. _ "github.com/qiniu/api.v7/v7/conf"
  8. "github.com/qiniu/api.v7/v7/storage"
  9. )
  10. // 请求图片上传地址信息
  11. func ReqImgUpload(f *md.FileCallback, callbackUrl string) interface{} {
  12. if ext := utils.FileExt(f.FileName); ext == "png" || ext == "jpg" || ext == "jpeg" || ext == "gif" || ext == "bmp" || ext == "webp" {
  13. f.Width = "$(imageInfo.width)"
  14. f.Height = "$(imageInfo.height)"
  15. }
  16. f.Provider = "qiniu"
  17. f.FileSize = "$(fsize)"
  18. f.Hash = "$(etag)"
  19. f.Bucket = "$(bucket)"
  20. f.Mime = "$(mimeType)"
  21. f.Time = utils.Int64ToStr(time.Now().Unix())
  22. f.Sign = Sign(f.Time)
  23. putPolicy := storage.PutPolicy{
  24. Scope: BUCKET + ":" + f.FileName, // 使用覆盖方式时候必须请求里面有key,否则报错
  25. Expires: Expires,
  26. ForceSaveKey: true,
  27. SaveKey: f.FileName,
  28. //MimeLimit: "image/*", // 只允许上传图片
  29. CallbackURL: callbackUrl,
  30. CallbackBody: utils.SerializeStr(f),
  31. CallbackBodyType: "application/json",
  32. }
  33. return &struct {
  34. Method string `json:"method"`
  35. Key string `json:"key"`
  36. Host string `json:"host"`
  37. Token string `json:"token"`
  38. }{Key: f.FileName, Method: "POST", Host: BUCKET_SCHEME + "://" + BUCKET_REGION, Token: putPolicy.UploadToken(qbox.NewMac(AK, SK))}
  39. }