upload.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 Upload = &upload{
  29. //使用本地上传
  30. adapter: UploadLocalAdapter{
  31. UpPath: "/pub_upload/",
  32. UploadPath: g.Cfg().GetString("server.ServerRoot") + "/pub_upload/",
  33. },
  34. //使用腾讯云COS上传
  35. /*adapter: UploadTencentCOSAdapter{
  36. UpPath: "/gfast/",
  37. RawUrl: "https://您的cos空间域名.cos.ap-chongqing.myqcloud.com",
  38. SecretID: "填写您的SecretID",
  39. SecretKey: "填写您的SecretKey",
  40. },*/
  41. }
  42. func (u upload) UpImg(file *ghttp.UploadFile) (fileInfo *FileInfo, err error) {
  43. return u.adapter.UpImg(file)
  44. }
  45. func (u upload) UpFile(file *ghttp.UploadFile) (fileInfo *FileInfo, err error) {
  46. return u.adapter.UpFile(file)
  47. }
  48. func (u upload) UpImgs(files []*ghttp.UploadFile) (fileInfos []*FileInfo, err error) {
  49. return u.adapter.UpImgs(files)
  50. }
  51. func (u upload) UpFiles(files []*ghttp.UploadFile) (fileInfos []*FileInfo, err error) {
  52. return u.adapter.UpFiles(files)
  53. }