savefilelogic.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package logic
  2. import (
  3. "context"
  4. "errors"
  5. "strings"
  6. "bytes"
  7. "app.yhyue.com/moapp/jyfs/rpc/filesystem"
  8. "app.yhyue.com/moapp/jyfs/rpc/internal/redis"
  9. "app.yhyue.com/moapp/jyfs/rpc/internal/svc"
  10. // "app.yhyue.com/moapp/jyfs/rpc/internal/redis"
  11. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  12. "github.com/tal-tech/go-zero/core/logx"
  13. )
  14. type SaveFileLogic struct {
  15. ctx context.Context
  16. svcCtx *svc.ServiceContext
  17. logx.Logger
  18. }
  19. func NewSaveFileLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SaveFileLogic {
  20. return &SaveFileLogic{
  21. ctx: ctx,
  22. svcCtx: svcCtx,
  23. Logger: logx.WithContext(ctx),
  24. }
  25. }
  26. // 保存文件
  27. func (l *SaveFileLogic) SaveFile(in *filesystem.SaveFileReq) (*filesystem.FileOpResp, error) {
  28. // 获取存储空间。
  29. bucket, err := l.svcCtx.OssClient.Bucket(in.Domain)
  30. if err != nil {
  31. return &filesystem.FileOpResp{State: false}, err
  32. }
  33. meta := redis.GetStr("jyfs", "jyfs_"+in.Domain)
  34. if meta != "" {
  35. for k, _ := range in.Meta {
  36. if !strings.Contains(meta, k) {
  37. return &filesystem.FileOpResp{}, errors.New("缺少元信息")
  38. }
  39. }
  40. }
  41. // 设置文件元信息:过期时间为2049年1月10日 23:00:00 GMT,访问权限为公共读,自定义元信息为MyProp(取值MyPropVal)。
  42. // expires := time.Date(2049, time.January, 10, 23, 0, 0, 0, time.UTC)
  43. options := []oss.Option{
  44. // oss.Expires(expires),
  45. // oss.ObjectACL(oss.ACLPublicRead),
  46. // oss.Meta("MyProp", "MyPropVal"),
  47. }
  48. for k, v := range in.Meta {
  49. options = append(options, oss.Meta(k, v))
  50. }
  51. // 上传Byte数组。
  52. err = bucket.PutObject(in.FileId, bytes.NewReader(in.RawFileContent), options...)
  53. if err != nil {
  54. return &filesystem.FileOpResp{}, err
  55. }
  56. return &filesystem.FileOpResp{State: true, FileId: in.FileId}, nil
  57. }