updatefilemetalogic.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package logic
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "strings"
  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. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  11. "github.com/tal-tech/go-zero/core/logx"
  12. )
  13. type UpdateFileMetaLogic struct {
  14. ctx context.Context
  15. svcCtx *svc.ServiceContext
  16. logx.Logger
  17. }
  18. func NewUpdateFileMetaLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateFileMetaLogic {
  19. return &UpdateFileMetaLogic{
  20. ctx: ctx,
  21. svcCtx: svcCtx,
  22. Logger: logx.WithContext(ctx),
  23. }
  24. }
  25. // 更新文件元数据
  26. func (l *UpdateFileMetaLogic) UpdateFileMeta(in *filesystem.UpdateFileMetaReq) (*filesystem.FileOpResp, error) {
  27. // todo: add your logic here and delete this line
  28. // 获取存储空间。
  29. bucket, err := l.svcCtx.OssClient.Bucket(in.Domain)
  30. if err != nil {
  31. return &filesystem.FileOpResp{}, 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. // 一次修改多条元信息。
  42. options := []oss.Option{
  43. // oss.Meta("MyMeta", "MyMetaValue2"),
  44. // oss.Meta("MyObjectLocation", "HangZhou"),
  45. }
  46. for k, v := range in.Meta {
  47. options = append(options, oss.Meta(k, v))
  48. }
  49. err = bucket.SetObjectMeta(in.FileId, options...)
  50. if err != nil {
  51. return &filesystem.FileOpResp{State: false}, err
  52. }
  53. // 获取文件元信息。
  54. props, err := bucket.GetObjectDetailedMeta(in.FileId)
  55. if err != nil {
  56. return &filesystem.FileOpResp{}, err
  57. }
  58. fmt.Println("Object Meta:", props)
  59. return &filesystem.FileOpResp{State: true}, nil
  60. }