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