2021-10-21 04:22:43 -05:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 12:20:29 -06:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-10-21 04:22:43 -05:00
|
|
|
|
2022-10-16 18:29:26 -05:00
|
|
|
package system
|
2021-10-21 04:22:43 -05:00
|
|
|
|
|
|
|
import (
|
2023-10-15 10:46:06 -05:00
|
|
|
"context"
|
|
|
|
|
2022-10-16 18:29:26 -05:00
|
|
|
"code.gitea.io/gitea/models/system"
|
2021-10-21 04:22:43 -05:00
|
|
|
"code.gitea.io/gitea/modules/json"
|
|
|
|
|
|
|
|
"github.com/yuin/goldmark/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DBStore can be used to store app state items in local filesystem
|
2022-01-20 11:46:10 -06:00
|
|
|
type DBStore struct{}
|
2021-10-21 04:22:43 -05:00
|
|
|
|
|
|
|
// Get reads the state item
|
2023-10-15 10:46:06 -05:00
|
|
|
func (f *DBStore) Get(ctx context.Context, item StateItem) error {
|
|
|
|
content, err := system.GetAppStateContent(ctx, item.Name())
|
2021-10-21 04:22:43 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if content == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return json.Unmarshal(util.StringToReadOnlyBytes(content), item)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set saves the state item
|
2023-10-15 10:46:06 -05:00
|
|
|
func (f *DBStore) Set(ctx context.Context, item StateItem) error {
|
2021-10-21 04:22:43 -05:00
|
|
|
b, err := json.Marshal(item)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-15 10:46:06 -05:00
|
|
|
return system.SaveAppStateContent(ctx, item.Name(), util.BytesToReadOnlyString(b))
|
2021-10-21 04:22:43 -05:00
|
|
|
}
|