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.

94 lines
3.1 KiB

  1. package upload
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "gin-vue-admin/global"
  7. "github.com/qiniu/api.v7/v7/auth/qbox"
  8. "github.com/qiniu/api.v7/v7/storage"
  9. "go.uber.org/zap"
  10. "mime/multipart"
  11. "time"
  12. )
  13. type Qiniu struct{}
  14. //@author: [piexlmax](https://github.com/piexlmax)
  15. //@author: [ccfish86](https://github.com/ccfish86)
  16. //@author: [SliverHorn](https://github.com/SliverHorn)
  17. //@object: *Qiniu
  18. //@function: UploadFile
  19. //@description: 上传文件
  20. //@param: file *multipart.FileHeader
  21. //@return: string, string, error
  22. func (*Qiniu) UploadFile(file *multipart.FileHeader) (string, string, error) {
  23. putPolicy := storage.PutPolicy{Scope: global.GVA_CONFIG.Qiniu.Bucket}
  24. mac := qbox.NewMac(global.GVA_CONFIG.Qiniu.AccessKey, global.GVA_CONFIG.Qiniu.SecretKey)
  25. upToken := putPolicy.UploadToken(mac)
  26. cfg := qiniuConfig()
  27. formUploader := storage.NewFormUploader(cfg)
  28. ret := storage.PutRet{}
  29. putExtra := storage.PutExtra{Params: map[string]string{"x:name": "github logo"}}
  30. f, openError := file.Open()
  31. if openError != nil {
  32. global.GVA_LOG.Error("function file.Open() Filed", zap.Any("err", openError.Error()))
  33. return "", "", errors.New("function file.Open() Filed, err:" + openError.Error())
  34. }
  35. fileKey := fmt.Sprintf("%d%s", time.Now().Unix(), file.Filename) // 文件名格式 自己可以改 建议保证唯一性
  36. putErr := formUploader.Put(context.Background(), &ret, upToken, fileKey, f, file.Size, &putExtra)
  37. if putErr != nil {
  38. global.GVA_LOG.Error("function formUploader.Put() Filed", zap.Any("err", putErr.Error()))
  39. return "", "", errors.New("function formUploader.Put() Filed, err:" + putErr.Error())
  40. }
  41. return global.GVA_CONFIG.Qiniu.ImgPath + "/" + ret.Key, ret.Key, nil
  42. }
  43. //@author: [piexlmax](https://github.com/piexlmax)
  44. //@author: [ccfish86](https://github.com/ccfish86)
  45. //@author: [SliverHorn](https://github.com/SliverHorn)
  46. //@object: *Qiniu
  47. //@function: DeleteFile
  48. //@description: 删除文件
  49. //@param: key string
  50. //@return: error
  51. func (*Qiniu) DeleteFile(key string) error {
  52. mac := qbox.NewMac(global.GVA_CONFIG.Qiniu.AccessKey, global.GVA_CONFIG.Qiniu.SecretKey)
  53. cfg := qiniuConfig()
  54. bucketManager := storage.NewBucketManager(mac, cfg)
  55. if err := bucketManager.Delete(global.GVA_CONFIG.Qiniu.Bucket, key); err != nil {
  56. global.GVA_LOG.Error("function bucketManager.Delete() Filed", zap.Any("err", err.Error()))
  57. return errors.New("function bucketManager.Delete() Filed, err:" + err.Error())
  58. }
  59. return nil
  60. }
  61. //@author: [SliverHorn](https://github.com/SliverHorn)
  62. //@object: *Qiniu
  63. //@function: qiniuConfig
  64. //@description: 根据配置文件进行返回七牛云的配置
  65. //@return: *storage.Config
  66. func qiniuConfig() *storage.Config {
  67. cfg := storage.Config{
  68. UseHTTPS: global.GVA_CONFIG.Qiniu.UseHTTPS,
  69. UseCdnDomains: global.GVA_CONFIG.Qiniu.UseCdnDomains,
  70. }
  71. switch global.GVA_CONFIG.Qiniu.Zone { // 根据配置文件进行初始化空间对应的机房
  72. case "ZoneHuadong":
  73. cfg.Zone = &storage.ZoneHuadong
  74. case "ZoneHuabei":
  75. cfg.Zone = &storage.ZoneHuabei
  76. case "ZoneHuanan":
  77. cfg.Zone = &storage.ZoneHuanan
  78. case "ZoneBeimei":
  79. cfg.Zone = &storage.ZoneBeimei
  80. case "ZoneXinjiapo":
  81. cfg.Zone = &storage.ZoneXinjiapo
  82. }
  83. return &cfg
  84. }