2021-10-21 17:22:43 +08:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-28 02:20:29 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-10-21 17:22:43 +08:00
|
|
|
|
2022-10-17 07:29:26 +08:00
|
|
|
package system
|
2021-10-21 17:22:43 +08:00
|
|
|
|
|
|
|
import (
|
2023-10-15 23:46:06 +08:00
|
|
|
"context"
|
|
|
|
|
2022-10-17 07:29:26 +08:00
|
|
|
"code.gitea.io/gitea/models/system"
|
2021-10-21 17:22:43 +08:00
|
|
|
"code.gitea.io/gitea/modules/json"
|
2024-06-14 09:26:33 +08:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2021-10-21 17:22:43 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// DBStore can be used to store app state items in local filesystem
|
2022-01-21 01:46:10 +08:00
|
|
|
type DBStore struct{}
|
2021-10-21 17:22:43 +08:00
|
|
|
|
|
|
|
// Get reads the state item
|
2023-10-15 23:46:06 +08:00
|
|
|
func (f *DBStore) Get(ctx context.Context, item StateItem) error {
|
|
|
|
content, err := system.GetAppStateContent(ctx, item.Name())
|
2021-10-21 17:22:43 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if content == "" {
|
|
|
|
return nil
|
|
|
|
}
|
2024-06-14 09:26:33 +08:00
|
|
|
return json.Unmarshal(util.UnsafeStringToBytes(content), item)
|
2021-10-21 17:22:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set saves the state item
|
2023-10-15 23:46:06 +08:00
|
|
|
func (f *DBStore) Set(ctx context.Context, item StateItem) error {
|
2021-10-21 17:22:43 +08:00
|
|
|
b, err := json.Marshal(item)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-06-14 09:26:33 +08:00
|
|
|
return system.SaveAppStateContent(ctx, item.Name(), util.UnsafeBytesToString(b))
|
2021-10-21 17:22:43 +08:00
|
|
|
}
|