upload.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * @desc:上传适配器
  3. * @company:云南省奇讯科技有限公司
  4. * @Author: yixiaohu
  5. * @Date: 2021/7/7 8:54
  6. */
  7. package adapter
  8. import (
  9. "github.com/gogf/gf/frame/g"
  10. "github.com/gogf/gf/net/ghttp"
  11. )
  12. // FileInfo 上传的文件信息
  13. type FileInfo struct {
  14. FileName string `json:"fileName"`
  15. FileSize int64 `json:"fileSize"`
  16. FileUrl string `json:"fileUrl"`
  17. FileType string `json:"fileType"`
  18. }
  19. type UploadAdapter interface {
  20. UpImg(file *ghttp.UploadFile) (fileInfo *FileInfo, err error)
  21. UpFile(file *ghttp.UploadFile) (fileInfo *FileInfo, err error)
  22. UpImgs(files []*ghttp.UploadFile) (fileInfos []*FileInfo, err error)
  23. UpFiles(files []*ghttp.UploadFile) (fileInfos []*FileInfo, err error)
  24. }
  25. type upload struct {
  26. adapter UploadAdapter
  27. }
  28. var (
  29. upType = g.Cfg().GetString("upload.type")
  30. Upload *upload
  31. )
  32. func init() {
  33. var adp UploadAdapter
  34. switch upType {
  35. case "local":
  36. //使用本地上传
  37. adp = UploadLocalAdapter{
  38. UpPath: "/pub_upload/",
  39. UploadPath: g.Cfg().GetString("server.ServerRoot") + "/pub_upload/",
  40. }
  41. case "tencentCOS":
  42. //使用腾讯云COS上传
  43. adp = UploadTencentCOSAdapter{
  44. UpPath: g.Cfg().GetString("upload.tencentCOS.UpPath"),
  45. RawUrl: g.Cfg().GetString("upload.tencentCOS.RawUrl"),
  46. SecretID: g.Cfg().GetString("upload.tencentCOS.SecretID"),
  47. SecretKey: g.Cfg().GetString("upload.tencentCOS.SecretKey"),
  48. }
  49. }
  50. Upload = &upload{
  51. adapter: adp,
  52. }
  53. }
  54. func (u upload) UpImg(file *ghttp.UploadFile) (fileInfo *FileInfo, err error) {
  55. return u.adapter.UpImg(file)
  56. }
  57. func (u upload) UpFile(file *ghttp.UploadFile) (fileInfo *FileInfo, err error) {
  58. return u.adapter.UpFile(file)
  59. }
  60. func (u upload) UpImgs(files []*ghttp.UploadFile) (fileInfos []*FileInfo, err error) {
  61. return u.adapter.UpImgs(files)
  62. }
  63. func (u upload) UpFiles(files []*ghttp.UploadFile) (fileInfos []*FileInfo, err error) {
  64. return u.adapter.UpFiles(files)
  65. }