diff --git a/script/bql.go b/script/bql.go index 8e10ae5..c83db83 100644 --- a/script/bql.go +++ b/script/bql.go @@ -17,6 +17,7 @@ type QueryParams struct { AccountType string `bql:"account ~"` OrderBy string `bql:"order by"` Limit int `bql:"limit"` + Path string } func GetQueryParams(c *gin.Context) QueryParams { @@ -41,6 +42,9 @@ func GetQueryParams(c *gin.Context) QueryParams { hasWhere = true } queryParams.Where = hasWhere + if c.Query("path") != "" { + queryParams.Path = c.Query("path") + } return queryParams } diff --git a/script/file.go b/script/file.go index 78c49f8..10127d1 100644 --- a/script/file.go +++ b/script/file.go @@ -47,6 +47,27 @@ func CreateFile(filePath string) error { 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 { err := os.MkdirAll(dirPath, os.ModePerm) if nil != err { diff --git a/server.go b/server.go index 615d3ae..0679b68 100644 --- a/server.go +++ b/server.go @@ -58,6 +58,9 @@ func RegisterRouter(router *gin.Engine) { authorized.GET("/transactions/payee", service.QueryTransactionsPayee) authorized.GET("/transactions/template", service.QueryTransactionsTemplate) 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) diff --git a/service/source_file.go b/service/source_file.go new file mode 100644 index 0000000..8cba8ab --- /dev/null +++ b/service/source_file.go @@ -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) +}