savefilelogic.go 1.7 KB

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