🧑‍💻 Improve kernel API /api/convert/pandoc Fix https://github.com/siyuan-note/siyuan/issues/8619

This commit is contained in:
Daniel 2023-06-26 18:31:30 +08:00
parent b3d3048924
commit fc818c6511
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
4 changed files with 46 additions and 24 deletions

View file

@ -33,17 +33,27 @@ func pandoc(c *gin.Context) {
return
}
dir := gulu.Rand.String(7)
dirArg := arg["dir"]
if nil != dirArg {
dir = dirArg.(string)
}
pandocArgs := arg["args"].([]interface{})
var args []string
for _, v := range pandocArgs {
args = append(args, v.(string))
}
err := util.ConvertPandoc(args...)
path, err := util.ConvertPandoc(dir, args...)
if nil != err {
ret.Code = -1
ret.Msg = err.Error()
return
}
ret.Data = map[string]interface{}{
"path": path,
}
return
}

View file

@ -29,24 +29,28 @@ import (
"github.com/siyuan-note/logging"
)
func ConvertPandoc(args ...string) (err error) {
func ConvertPandoc(dir string, args ...string) (path string, err error) {
if "" == PandocBinPath || ContainerStd != Container {
return errors.New("not found executable pandoc")
err = errors.New("not found executable pandoc")
return
}
pandoc := exec.Command(PandocBinPath, args...)
gulu.CmdAttr(pandoc)
dir := filepath.Join(WorkspaceDir, "temp", "convert", "pandoc", gulu.Rand.String(7))
if err = os.MkdirAll(dir, 0755); nil != err {
logging.LogErrorf("mkdir [%s] failed: [%s]", dir, err)
path = filepath.Join("temp", "convert", "pandoc", dir)
absPath := filepath.Join(WorkspaceDir, path)
if err = os.MkdirAll(absPath, 0755); nil != err {
logging.LogErrorf("mkdir [%s] failed: [%s]", absPath, err)
return
}
pandoc.Dir = dir
pandoc.Dir = absPath
output, err := pandoc.CombinedOutput()
if nil != err {
logging.LogErrorf("pandoc convert output [%s]", string(output))
err = errors.Join(err, errors.New(string(output)))
logging.LogErrorf("pandoc convert output failed: %s", err)
return
}
path = "/" + filepath.ToSlash(path)
return
}