add: file api

This commit is contained in:
BaoXuebin 2021-11-23 23:33:14 +08:00
parent 6cb9156349
commit a69f8e1091
4 changed files with 124 additions and 0 deletions

View File

@ -17,6 +17,7 @@ type QueryParams struct {
AccountType string `bql:"account ~"` AccountType string `bql:"account ~"`
OrderBy string `bql:"order by"` OrderBy string `bql:"order by"`
Limit int `bql:"limit"` Limit int `bql:"limit"`
Path string
} }
func GetQueryParams(c *gin.Context) QueryParams { func GetQueryParams(c *gin.Context) QueryParams {
@ -41,6 +42,9 @@ func GetQueryParams(c *gin.Context) QueryParams {
hasWhere = true hasWhere = true
} }
queryParams.Where = hasWhere queryParams.Where = hasWhere
if c.Query("path") != "" {
queryParams.Path = c.Query("path")
}
return queryParams return queryParams
} }

View File

@ -47,6 +47,27 @@ func CreateFile(filePath string) error {
return nil return nil
} }
func CopyFile(sourceFilePath string, targetFilePath string) error {
if !FileIfExist(sourceFilePath) {
panic("File is not found, " + sourceFilePath)
}
if !FileIfExist(targetFilePath) {
err := CreateFile(targetFilePath)
if err != nil {
return err
}
}
bytes, err := ReadFile(sourceFilePath)
if err != nil {
return err
}
err = WriteFile(targetFilePath, string(bytes))
if err != nil {
return err
}
return nil
}
func MkDir(dirPath string) error { func MkDir(dirPath string) error {
err := os.MkdirAll(dirPath, os.ModePerm) err := os.MkdirAll(dirPath, os.ModePerm)
if nil != err { if nil != err {

View File

@ -58,6 +58,9 @@ func RegisterRouter(router *gin.Engine) {
authorized.GET("/transactions/payee", service.QueryTransactionsPayee) authorized.GET("/transactions/payee", service.QueryTransactionsPayee)
authorized.GET("/transactions/template", service.QueryTransactionsTemplate) authorized.GET("/transactions/template", service.QueryTransactionsTemplate)
authorized.GET("/tags", service.QueryTags) authorized.GET("/tags", service.QueryTags)
authorized.GET("/file/dir", service.QueryLedgerSourceFileDir)
authorized.GET("/file/content", service.QueryLedgerSourceFileContent)
authorized.POST("/file", service.UpdateLedgerSourceFileContent)
// 兼容旧版本 // 兼容旧版本
authorized.GET("/entry", service.QueryTransactions) authorized.GET("/entry", service.QueryTransactions)

96
service/source_file.go Normal file
View File

@ -0,0 +1,96 @@
package service
import (
"fmt"
"github.com/beancount-gs/script"
"github.com/gin-gonic/gin"
"os"
"strings"
"time"
)
func QueryLedgerSourceFileDir(c *gin.Context) {
ledgerConfig := script.GetLedgerConfigFromContext(c)
result, err := dirs(ledgerConfig.DataPath, ledgerConfig.DataPath)
if err != nil {
InternalError(c, err.Error())
return
}
OK(c, result)
}
func dirs(parent string, dirPath string) ([]string, error) {
result := make([]string, 0)
rd, err := os.ReadDir(dirPath)
if err != nil {
return nil, err
}
for _, dir := range rd {
parentDir := dirPath + "/" + dir.Name()
if dir.IsDir() {
// 跳过备份文件夹
if dir.Name() == "bak" {
continue
}
files, err := dirs(parent, parentDir)
if err != nil {
return nil, err
}
result = append(result, files...)
} else {
fmt.Println(parentDir)
result = append(result, strings.ReplaceAll(parentDir, parent+"/", ""))
}
}
return result, nil
}
func QueryLedgerSourceFileContent(c *gin.Context) {
ledgerConfig := script.GetLedgerConfigFromContext(c)
queryParams := script.GetQueryParams(c)
if queryParams.Path == "" {
BadRequest(c, "params must not be blank")
return
}
bytes, err := script.ReadFile(ledgerConfig.DataPath + "/" + queryParams.Path)
if err != nil {
InternalError(c, err.Error())
return
}
OK(c, string(bytes))
}
type UpdateSourceFileForm struct {
Path string `form:"path" binding:"required"`
Content string `form:"content" binding:"required"`
}
func UpdateLedgerSourceFileContent(c *gin.Context) {
ledgerConfig := script.GetLedgerConfigFromContext(c)
var updateSourceFileForm UpdateSourceFileForm
if err := c.ShouldBindJSON(&updateSourceFileForm); err != nil {
BadRequest(c, err.Error())
return
}
sourceFilePath := ledgerConfig.DataPath + "/" + updateSourceFileForm.Path
targetFilePath := ledgerConfig.DataPath + "/bak/" + time.Now().Format("20060102150405") + "_" + updateSourceFileForm.Path
// 备份数据
if ledgerConfig.IsBak {
err := script.CopyFile(sourceFilePath, targetFilePath)
if err != nil {
InternalError(c, err.Error())
return
}
}
err := script.WriteFile(sourceFilePath, updateSourceFileForm.Content)
if err != nil {
InternalError(c, err.Error())
return
}
OK(c, nil)
}