gitea源码

12345678910111213141516171819202122232425262728293031323334353637
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package system
  4. import (
  5. "context"
  6. "code.gitea.io/gitea/models/system"
  7. "code.gitea.io/gitea/modules/json"
  8. "code.gitea.io/gitea/modules/util"
  9. )
  10. // DBStore can be used to store app state items in local filesystem
  11. type DBStore struct{}
  12. // Get reads the state item
  13. func (f *DBStore) Get(ctx context.Context, item StateItem) error {
  14. content, err := system.GetAppStateContent(ctx, item.Name())
  15. if err != nil {
  16. return err
  17. }
  18. if content == "" {
  19. return nil
  20. }
  21. return json.Unmarshal(util.UnsafeStringToBytes(content), item)
  22. }
  23. // Set saves the state item
  24. func (f *DBStore) Set(ctx context.Context, item StateItem) error {
  25. b, err := json.Marshal(item)
  26. if err != nil {
  27. return err
  28. }
  29. return system.SaveAppStateContent(ctx, item.Name(), util.UnsafeBytesToString(b))
  30. }