🧑‍💻 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

21
API.md
View File

@ -470,7 +470,8 @@ View API token in <kbd>Settings - About</kbd>, request header: `Authorization: T
* `"/assets/sub/"`: workspace/data/assets/sub/ folder * `"/assets/sub/"`: workspace/data/assets/sub/ folder
Under normal circumstances, it is recommended to use the first method, which is stored in the assets folder Under normal circumstances, it is recommended to use the first method, which is stored in the assets folder
of the workspace, putting in a subdirectory has some side effects, please refer to the assets chapter of the user guide. of the workspace, putting in a subdirectory has some side effects, please refer to the assets chapter of the user
guide.
* `file[]`: Uploaded file list * `file[]`: Uploaded file list
* Return value * Return value
@ -829,9 +830,9 @@ View API token in <kbd>Settings - About</kbd>, request header: `Authorization: T
} }
``` ```
* `fromID`: Def block ID * `fromID`: Def block ID
* `toID`: Target block ID * `toID`: Target block ID
* `refIDs`: Ref block IDs point to def block ID, optional, if not specified, all ref block IDs will be transferred * `refIDs`: Ref block IDs point to def block ID, optional, if not specified, all ref block IDs will be transferred
* Return value * Return value
```json ```json
@ -1041,8 +1042,8 @@ View API token in <kbd>Settings - About</kbd>, request header: `Authorization: T
"newPath": "/data/assets/test-20230523085812-k3o9t32.png" "newPath": "/data/assets/test-20230523085812-k3o9t32.png"
} }
``` ```
* `path`: the file path under the workspace path * `path`: the file path under the workspace path
* `newPath`: the new file path under the workspace path * `newPath`: the new file path under the workspace path
* Return value * Return value
```json ```json
@ -1119,7 +1120,7 @@ View API token in <kbd>Settings - About</kbd>, request header: `Authorization: T
* `/api/convert/pandoc` * `/api/convert/pandoc`
* Working directory * Working directory
* Executing the pandoc command will set the working directory to `workspace/temp/convert/pandoc/` * Executing the pandoc command will set the working directory to `workspace/temp/convert/pandoc/${dir}`
* API [`Put file`](#put-file) can be used to write the file to be converted to this directory first * API [`Put file`](#put-file) can be used to write the file to be converted to this directory first
* Then call the API for conversion, and the converted file will also be written to this directory * Then call the API for conversion, and the converted file will also be written to this directory
* Finally, call the API [`Get file`](#get-file) to get the converted file * Finally, call the API [`Get file`](#get-file) to get the converted file
@ -1129,6 +1130,7 @@ View API token in <kbd>Settings - About</kbd>, request header: `Authorization: T
```json ```json
{ {
"dir": "test",
"args": [ "args": [
"--to", "markdown_strict-raw_html", "--to", "markdown_strict-raw_html",
"foo.epub", "foo.epub",
@ -1144,9 +1146,12 @@ View API token in <kbd>Settings - About</kbd>, request header: `Authorization: T
{ {
"code": 0, "code": 0,
"msg": "", "msg": "",
"data": null "data": {
"path": "/temp/convert/pandoc/test"
}
} }
``` ```
* `path`: the path under the workspace
## Notification ## Notification

View File

@ -822,9 +822,9 @@
} }
``` ```
* `fromID`:定义块 ID * `fromID`:定义块 ID
* `toID`:目标块 ID * `toID`:目标块 ID
* `refIDs`:指向定义块 ID 的引用所在块 ID可选如果不指定所有指向定义块 ID 的引用块 ID 都会被转移 * `refIDs`:指向定义块 ID 的引用所在块 ID可选如果不指定所有指向定义块 ID 的引用块 ID 都会被转移
* 返回值 * 返回值
```json ```json
@ -835,7 +835,6 @@
} }
``` ```
## 属性 ## 属性
### 设置块属性 ### 设置块属性
@ -1035,8 +1034,8 @@
"newPath": "/data/assets/test-20230523085812-k3o9t32.png" "newPath": "/data/assets/test-20230523085812-k3o9t32.png"
} }
``` ```
* `path`:工作空间路径下的文件路径 * `path`:工作空间路径下的文件路径
* `newPath`:新的文件路径 * `newPath`:新的文件路径
* 返回值 * 返回值
```json ```json
@ -1113,7 +1112,7 @@
* `/api/convert/pandoc` * `/api/convert/pandoc`
* 工作目录 * 工作目录
* 执行调用 pandoc 命令时工作目录会被设置在 `工作空间/temp/convert/pandoc/` 下 * 执行调用 pandoc 命令时工作目录会被设置在 `工作空间/temp/convert/pandoc/${test}` 下
* 可先通过 API [`写入文件`](#写入文件) 将待转换文件写入该目录 * 可先通过 API [`写入文件`](#写入文件) 将待转换文件写入该目录
* 然后再调用该 API 进行转换,转换后的文件也会被写入该目录 * 然后再调用该 API 进行转换,转换后的文件也会被写入该目录
* 最后调用 API [`获取文件`](#获取文件) 获取转换后的文件内容 * 最后调用 API [`获取文件`](#获取文件) 获取转换后的文件内容
@ -1123,6 +1122,7 @@
```json ```json
{ {
"dir": "test",
"args": [ "args": [
"--to", "markdown_strict-raw_html", "--to", "markdown_strict-raw_html",
"foo.epub", "foo.epub",
@ -1138,9 +1138,12 @@
{ {
"code": 0, "code": 0,
"msg": "", "msg": "",
"data": null "data": {
"path": "/temp/convert/pandoc/test"
}
} }
``` ```
* `path`:工作空间下的路径
## 通知 ## 通知

View File

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

View File

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