code.gitea.io/gitea@v1.19.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  	"code.gitea.io/gitea/models/system"
     8  	"code.gitea.io/gitea/modules/json"
     9  
    10  	"github.com/yuin/goldmark/util"
    11  )
    12  
    13  // DBStore can be used to store app state items in local filesystem
    14  type DBStore struct{}
    15  
    16  // Get reads the state item
    17  func (f *DBStore) Get(item StateItem) error {
    18  	content, err := system.GetAppStateContent(item.Name())
    19  	if err != nil {
    20  		return err
    21  	}
    22  	if content == "" {
    23  		return nil
    24  	}
    25  	return json.Unmarshal(util.StringToReadOnlyBytes(content), item)
    26  }
    27  
    28  // Set saves the state item
    29  func (f *DBStore) Set(item StateItem) error {
    30  	b, err := json.Marshal(item)
    31  	if err != nil {
    32  		return err
    33  	}
    34  	return system.SaveAppStateContent(item.Name(), util.BytesToReadOnlyString(b))
    35  }