code.gitea.io/gitea@v1.22.3/modules/system/db.go (about)

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