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.

96 lines
3.3 KiB

3 years ago
3 years ago
  1. package upload
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "mime/multipart"
  7. "time"
  8. "github.com/flipped-aurora/gin-vue-admin/server/global"
  9. "github.com/qiniu/api.v7/v7/auth/qbox"
  10. "github.com/qiniu/api.v7/v7/storage"
  11. "go.uber.org/zap"
  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. defer f.Close() // 创建文件 defer 关闭
  36. fileKey := fmt.Sprintf("%d%s", time.Now().Unix(), file.Filename) // 文件名格式 自己可以改 建议保证唯一性
  37. putErr := formUploader.Put(context.Background(), &ret, upToken, fileKey, f, file.Size, &putExtra)
  38. if putErr != nil {
  39. global.GVA_LOG.Error("function formUploader.Put() Filed", zap.Any("err", putErr.Error()))
  40. return "", "", errors.New("function formUploader.Put() Filed, err:" + putErr.Error())
  41. }
  42. return global.GVA_CONFIG.Qiniu.ImgPath + "/" + ret.Key, ret.Key, nil
  43. }
  44. //@author: [piexlmax](https://github.com/piexlmax)
  45. //@author: [ccfish86](https://github.com/ccfish86)
  46. //@author: [SliverHorn](https://github.com/SliverHorn)
  47. //@object: *Qiniu
  48. //@function: DeleteFile
  49. //@description: 删除文件
  50. //@param: key string
  51. //@return: error
  52. func (*Qiniu) DeleteFile(key string) error {
  53. mac := qbox.NewMac(global.GVA_CONFIG.Qiniu.AccessKey, global.GVA_CONFIG.Qiniu.SecretKey)
  54. cfg := qiniuConfig()
  55. bucketManager := storage.NewBucketManager(mac, cfg)
  56. if err := bucketManager.Delete(global.GVA_CONFIG.Qiniu.Bucket, key); err != nil {
  57. global.GVA_LOG.Error("function bucketManager.Delete() Filed", zap.Any("err", err.Error()))
  58. return errors.New("function bucketManager.Delete() Filed, err:" + err.Error())
  59. }
  60. return nil
  61. }
  62. //@author: [SliverHorn](https://github.com/SliverHorn)
  63. //@object: *Qiniu
  64. //@function: qiniuConfig
  65. //@description: 根据配置文件进行返回七牛云的配置
  66. //@return: *storage.Config
  67. func qiniuConfig() *storage.Config {
  68. cfg := storage.Config{
  69. UseHTTPS: global.GVA_CONFIG.Qiniu.UseHTTPS,
  70. UseCdnDomains: global.GVA_CONFIG.Qiniu.UseCdnDomains,
  71. }
  72. switch global.GVA_CONFIG.Qiniu.Zone { // 根据配置文件进行初始化空间对应的机房
  73. case "ZoneHuadong":
  74. cfg.Zone = &storage.ZoneHuadong
  75. case "ZoneHuabei":
  76. cfg.Zone = &storage.ZoneHuabei
  77. case "ZoneHuanan":
  78. cfg.Zone = &storage.ZoneHuanan
  79. case "ZoneBeimei":
  80. cfg.Zone = &storage.ZoneBeimei
  81. case "ZoneXinjiapo":
  82. cfg.Zone = &storage.ZoneXinjiapo
  83. }
  84. return &cfg
  85. }