第三方api接口
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.

4 月之前
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package qiniu
  2. import (
  3. "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/e"
  4. "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/md"
  5. zhios_third_party_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/utils"
  6. "fmt"
  7. "strings"
  8. )
  9. func ImgReqUpload(arg map[string]string, uid, dirName, fname, callbackUrl string, fsize int64) (interface{}, error) {
  10. ext := zhios_third_party_utils.FileExt(fname)
  11. if err := initStg(arg, fsize, ext); err != nil {
  12. return nil, err
  13. }
  14. newName := dirName + "_" + fmt.Sprintf("%010s", uid)
  15. // 默认都加时间戳
  16. newName += "_" + zhios_third_party_utils.FormatNanoUnix() + zhios_third_party_utils.RandString(4, "0123456789")
  17. newName += "." + ext
  18. f := &md.FileCallback{
  19. Uid: uid,
  20. DirId: md.FileUserDir[dirName],
  21. FileName: newName,
  22. }
  23. return ReqImgUpload(f, callbackUrl), nil
  24. }
  25. func initStg(stgInfo map[string]string, fsize int64, ext string) error {
  26. // todo 目前仅支持七牛
  27. if v, ok := stgInfo[md.KEY_CFG_FILE_REGION]; !ok || v == "" {
  28. return e.NewErrCode(e.ERR_CFG)
  29. }
  30. if v, ok := stgInfo[md.KEY_CFG_FILE_AK]; !ok || v == "" {
  31. return e.NewErrCode(e.ERR_CFG)
  32. }
  33. if v, ok := stgInfo[md.KEY_CFG_FILE_SK]; !ok || v == "" {
  34. return e.NewErrCode(e.ERR_CFG)
  35. }
  36. if v, ok := stgInfo[md.KEY_CFG_FILE_BUCKET]; !ok || v == "" {
  37. return e.NewErrCode(e.ERR_CFG)
  38. }
  39. if v, ok := stgInfo[md.KEY_CFG_FILE_SCHEME]; !ok || v == "" {
  40. stgInfo[md.KEY_CFG_FILE_SCHEME] = "http"
  41. }
  42. 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])
  43. if v, ok := stgInfo[md.KEY_CFG_FILE_HOST]; !ok || v == "" {
  44. var err error
  45. stgInfo[md.KEY_CFG_FILE_HOST], err = BucketGetDomain(stgInfo[md.KEY_CFG_FILE_BUCKET])
  46. if err != nil {
  47. return e.NewErrCode(e.ERR_CFG)
  48. }
  49. }
  50. // 头像缩略图参数
  51. if v, ok := stgInfo[md.KEY_CFG_FILE_AVATAR_THUMBNAIL]; !ok || v == "" {
  52. stgInfo[md.KEY_CFG_FILE_AVATAR_THUMBNAIL] = "?imageView2/1/w/200/h/200/format/png"
  53. }
  54. // 检查文件大小限制
  55. if v, ok := stgInfo[md.KEY_CFG_FILE_MAX_SIZE]; ok && v != "" && zhios_third_party_utils.StrToInt64(v) < fsize {
  56. return e.NewErrCode(e.ERR_FILE_MAX_SIZE)
  57. }
  58. // 检查文件后缀
  59. if v, ok := stgInfo[md.KEY_CFG_FILE_EXT]; ok && v != "" && !strings.Contains(v, ext) {
  60. return e.NewErrCode(e.ERR_FILE_EXT)
  61. }
  62. return nil
  63. }