Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

♻️ Migrate archive API contracts https://github.com/siyuan-note/siyuan/issues/19378

Daniel24fe3d9839c8
diff --git a/app/src/types/api/index.d.ts b/app/src/types/api/index.d.ts
index 5680b7393a..471b949e4f 100644
--- a/app/src/types/api/index.d.ts
+++ b/app/src/types/api/index.d.ts
@@ -256,6 +256,8 @@ export type UnfoldedParentData = { "parentID": string; };
export type UnlockNotebookRequestInput = { "notebook": string; "password": string; };
+export type UnzipRequestInput = { "path": string; "zipPath": string; };
+
export type UpdateBlockRequestInput = { "data": string; "dataType": string; "id": string; "lockType"?: boolean | null; };
export type UpdateChannelRequestInput = { "updateChannel": string; };
@@ -268,6 +270,8 @@ export type WordCountData = { "reqId": JSONValue; "stat": BlockStat | null; };
export type WorkspaceInfoData = { "siyuanVer": string; "workspaceDir": string; };
+export type ZipRequestInput = { "path": string; "zipPath": string; };
+
export type APILegacyGETPath =
"/api/ai/mcp/oauth/callback/:flowID" |
"/api/icon/getDynamicIcon" |
@@ -342,8 +346,6 @@ export type APILegacyPOSTPath =
"/api/ai/testEmbeddingModel" |
"/api/ai/testModel" |
"/api/ai/testRerankModel" |
- "/api/archive/unzip" |
- "/api/archive/zip" |
"/api/asset/fullReindexAssetContent" |
"/api/asset/getDocAssets" |
"/api/asset/getDocImageAssets" |
@@ -790,6 +792,16 @@ export type APILegacyPOSTPath =
"/plugin/private/:name/*path";
export interface APIPOSTRoutes {
+ "/api/archive/unzip": {
+ request: UnzipRequestInput;
+ response: { "code": 0; "data": null; "msg": string; } | { "code": -1; "data": { "closeTimeout": number; } | null; "msg": string; };
+ body: "json";
+ };
+ "/api/archive/zip": {
+ request: ZipRequestInput;
+ response: { "code": 0; "data": null; "msg": string; } | { "code": -1; "data": { "closeTimeout": number; } | null; "msg": string; };
+ body: "json";
+ };
"/api/attr/batchGetBlockAttrs": {
request: BlockIDsRequestInput;
response: { "code": 0; "data": Record<string, Record<string, string> | null>; "msg": string; } | { "code": -1; "data": { "closeTimeout": number; } | null; "msg": string; };
diff --git a/kernel/api/archive.go b/kernel/api/archive.go
index 1731c92add..740495d152 100644
--- a/kernel/api/archive.go
+++ b/kernel/api/archive.go
@@ -19,9 +19,9 @@ package api
import (
archivezip "archive/zip"
"fmt"
+ "github.com/siyuan-note/siyuan/kernel/apicontract"
"io"
"io/fs"
- "net/http"
"os"
"path/filepath"
"strings"
@@ -83,50 +83,39 @@ func resolveArchivePath(path string) (string, error) {
return filepath.Join(resolved, rel), nil
}
-func zip(c *gin.Context) {
+var zip = contractHandler(apicontract.Zip, func(c *gin.Context, request apicontract.ZipRequest) apicontract.Response[apicontract.Null] {
ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
+ entryPath, zipFilePath := request.Path, request.ZipPath
- var entryPath, zipFilePath string
- if !util.ParseJsonArgs(arg, ret,
- util.BindJsonArg("path", &entryPath, true, true), // 相对于工作空间的路径(待打包目录或文件)
- util.BindJsonArg("zipPath", &zipFilePath, true, true), // 相对于工作空间的路径(生成的 zip)
- ) {
- return
- }
entryAbsPath, err := util.GetAbsPathInWorkspace(entryPath)
if err != nil {
ret.Code = -1
ret.Msg = err.Error()
- return
+ return contractFailure[apicontract.Null](ret)
}
if err = rejectEncryptedArchivePath(entryAbsPath); err != nil {
ret.Code = -1
ret.Msg = err.Error()
- return
+ return contractFailure[apicontract.Null](ret)
}
zipAbsFilePath, err := util.GetAbsPathInWorkspace(zipFilePath)
if err != nil {
ret.Code = -1
ret.Msg = err.Error()
- return
+ return contractFailure[apicontract.Null](ret)
}
if err = rejectEncryptedArchivePath(zipAbsFilePath); err != nil {
ret.Code = -1
ret.Msg = err.Error()
- return
+ return contractFailure[apicontract.Null](ret)
}
// 在创建归档前检查全部源条目,避免通过父目录打包加密笔记本。
resolvedEntryPath, err := resolveArchivePath(entryAbsPath)
if err != nil {
ret.Code = -1
ret.Msg = err.Error()
- return
+ return contractFailure[apicontract.Null](ret)
}
if err = filepath.WalkDir(resolvedEntryPath, func(path string, _ fs.DirEntry, walkErr error) error {
if walkErr != nil {
@@ -136,26 +125,26 @@ func zip(c *gin.Context) {
}); err != nil {
ret.Code = -1
ret.Msg = err.Error()
- return
+ return contractFailure[apicontract.Null](ret)
}
resolvedZipPath, err := resolveArchivePath(zipAbsFilePath)
if err != nil {
ret.Code = -1
ret.Msg = err.Error()
- return
+ return contractFailure[apicontract.Null](ret)
}
if err = rejectEncryptedArchivePath(resolvedZipPath); err != nil {
ret.Code = -1
ret.Msg = err.Error()
- return
+ return contractFailure[apicontract.Null](ret)
}
zipFile, err := gulu.Zip.Create(resolvedZipPath)
if err != nil {
logging.LogErrorf("create zip [%s] failed: %s", zipAbsFilePath, err)
ret.Code = -1
ret.Msg = "create zip file failed" + errMsgSeeKernelLog
- return
+ return contractFailure[apicontract.Null](ret)
}
base := filepath.Base(entryAbsPath)
@@ -168,69 +157,62 @@ func zip(c *gin.Context) {
logging.LogErrorf("zip add entry [%s] failed: %s", entryAbsPath, err)
ret.Code = -1
ret.Msg = "zip failed" + errMsgSeeKernelLog
- return
+ return contractFailure[apicontract.Null](ret)
}
if err = zipFile.Close(); err != nil {
logging.LogErrorf("close zip [%s] failed: %s", zipAbsFilePath, err)
ret.Code = -1
ret.Msg = "close zip file failed" + errMsgSeeKernelLog
- return
+ return contractFailure[apicontract.Null](ret)
}
-}
-func unzip(c *gin.Context) {
+ return apicontract.Success(apicontract.Null{})
+})
+
+var unzip = contractHandler(apicontract.Unzip, func(c *gin.Context, request apicontract.UnzipRequest) apicontract.Response[apicontract.Null] {
ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
+ entryPath, zipFilePath := request.Path, request.ZipPath
- var zipFilePath, entryPath string
- if !util.ParseJsonArgs(arg, ret,
- util.BindJsonArg("zipPath", &zipFilePath, true, true), // 相对于工作空间的路径
- util.BindJsonArg("path", &entryPath, true, false), // 相对于工作空间的路径(解压目标目录)
- ) {
- return
- }
zipAbsFilePath, err := util.GetAbsPathInWorkspace(zipFilePath)
if err != nil {
ret.Code = -1
ret.Msg = err.Error()
- return
+ return contractFailure[apicontract.Null](ret)
}
if err = rejectEncryptedArchivePath(zipAbsFilePath); err != nil {
ret.Code = -1
ret.Msg = err.Error()
- return
+ return contractFailure[apicontract.Null](ret)
}
entryAbsPath, err := util.GetAbsPathInWorkspace(entryPath)
if err != nil {
ret.Code = -1
ret.Msg = err.Error()
- return
+ return contractFailure[apicontract.Null](ret)
}
if err = rejectEncryptedArchivePath(entryAbsPath); err != nil {
ret.Code = -1
ret.Msg = err.Error()
- return
+ return contractFailure[apicontract.Null](ret)
}
if !gulu.File.IsExist(zipAbsFilePath) {
ret.Code = -1
ret.Msg = "zip file does not exist"
- return
+ return contractFailure[apicontract.Null](ret)
}
if err := unzipWorkspaceArchive(zipAbsFilePath, entryAbsPath); err != nil {
logging.LogErrorf("unzip [%s] -> [%s] failed: %s", zipAbsFilePath, entryAbsPath, err)
ret.Code = -1
ret.Msg = "unzip failed" + errMsgSeeKernelLog
- return
+ return contractFailure[apicontract.Null](ret)
}
-}
+
+ return apicontract.Success(apicontract.Null{})
+})
// unzipWorkspaceArchive 先校验全部条目,阻止已知非法路径导致部分写入,再从同一个归档句柄解压。
func unzipWorkspaceArchive(zipPath, destination string) error {
diff --git a/kernel/apicontract/archive.go b/kernel/apicontract/archive.go
new file mode 100644
index 0000000000..d4f084a2ef
--- /dev/null
+++ b/kernel/apicontract/archive.go
@@ -0,0 +1,11 @@
+package apicontract
+
+type ZipRequest struct {
+ Path string `json:"path" api:"trim"`
+ ZipPath string `json:"zipPath" api:"trim"`
+}
+
+type UnzipRequest struct {
+ ZipPath string `json:"zipPath" api:"trim"`
+ Path string `json:"path"`
+}
diff --git a/kernel/apicontract/contracts.go b/kernel/apicontract/contracts.go
index 88dc270732..34d6fbb4a3 100644
--- a/kernel/apicontract/contracts.go
+++ b/kernel/apicontract/contracts.go
@@ -354,3 +354,6 @@ var GetHeadingLevelTransaction = define[HeadingLevelRequest, *BlockTransaction](
var GetDocHeadingLevelTransaction = define[DocHeadingLevelRequest, *DocHeadingLevelData]("getDocHeadingLevelTransaction", "/api/block/getDocHeadingLevelTransaction", StructJSONBody, ResponseOptions{}, "POST")
var GetRecentUpdatedBlocks = define[EmptyRequest, []*SearchBlock]("getRecentUpdatedBlocks", "/api/block/getRecentUpdatedBlocks", NoBody, ResponseOptions{}, "POST")
+
+var Zip = define[ZipRequest, Null]("zip", "/api/archive/zip", JSONBody, ResponseOptions{}, "POST")
+var Unzip = define[UnzipRequest, Null]("unzip", "/api/archive/unzip", JSONBody, ResponseOptions{}, "POST")
diff --git a/kernel/apicontract/legacy_routes.json b/kernel/apicontract/legacy_routes.json
index a59b7a88fa..364172efd9 100644
--- a/kernel/apicontract/legacy_routes.json
+++ b/kernel/apicontract/legacy_routes.json
@@ -57,8 +57,6 @@
{"method":"POST","path":"/api/ai/testEmbeddingModel","handler":"testEmbeddingModel"},
{"method":"POST","path":"/api/ai/testModel","handler":"testModel"},
{"method":"POST","path":"/api/ai/testRerankModel","handler":"testRerankModel"},
- {"method":"POST","path":"/api/archive/unzip","handler":"unzip"},
- {"method":"POST","path":"/api/archive/zip","handler":"zip"},
{"method":"POST","path":"/api/asset/fullReindexAssetContent","handler":"fullReindexAssetContent"},
{"method":"POST","path":"/api/asset/getDocAssets","handler":"getDocAssets"},
{"method":"POST","path":"/api/asset/getDocImageAssets","handler":"getDocImageAssets"},
diff --git a/kernel/apicontract/schema.json b/kernel/apicontract/schema.json
index 622859e6fd..238f18a804 100644
--- a/kernel/apicontract/schema.json
+++ b/kernel/apicontract/schema.json
@@ -3872,6 +3872,22 @@
],
"additionalProperties": false
},
+ "UnzipRequestInput": {
+ "type": "object",
+ "properties": {
+ "path": {
+ "type": "string"
+ },
+ "zipPath": {
+ "type": "string"
+ }
+ },
+ "required": [
+ "path",
+ "zipPath"
+ ],
+ "additionalProperties": false
+ },
"UpdateBlockRequestInput": {
"type": "object",
"properties": {
@@ -3992,6 +4008,22 @@
"workspaceDir"
],
"additionalProperties": false
+ },
+ "ZipRequestInput": {
+ "type": "object",
+ "properties": {
+ "path": {
+ "type": "string"
+ },
+ "zipPath": {
+ "type": "string"
+ }
+ },
+ "required": [
+ "path",
+ "zipPath"
+ ],
+ "additionalProperties": false
}
},
"endpoints": [
@@ -4145,6 +4177,156 @@
]
}
},
+ {
+ "method": "POST",
+ "path": "/api/archive/unzip",
+ "handler": "unzip",
+ "body": "json",
+ "request": {
+ "$ref": "#/$defs/UnzipRequestInput"
+ },
+ "response": {
+ "anyOf": [
+ {
+ "type": "object",
+ "properties": {
+ "code": {
+ "type": "integer",
+ "enum": [
+ 0
+ ]
+ },
+ "data": {
+ "type": "null"
+ },
+ "msg": {
+ "type": "string"
+ }
+ },
+ "required": [
+ "code",
+ "msg",
+ "data"
+ ],
+ "additionalProperties": false
+ },
+ {
+ "type": "object",
+ "properties": {
+ "code": {
+ "type": "integer",
+ "enum": [
+ -1
+ ]
+ },
+ "data": {
+ "anyOf": [
+ {
+ "type": "object",
+ "properties": {
+ "closeTimeout": {
+ "type": "number"
+ }
+ },
+ "required": [
+ "closeTimeout"
+ ],
+ "additionalProperties": false
+ },
+ {
+ "type": "null"
+ }
+ ]
+ },
+ "msg": {
+ "type": "string"
+ }
+ },
+ "required": [
+ "code",
+ "msg",
+ "data"
+ ],
+ "additionalProperties": true
+ }
+ ]
+ }
+ },
+ {
+ "method": "POST",
+ "path": "/api/archive/zip",
+ "handler": "zip",
+ "body": "json",
+ "request": {
+ "$ref": "#/$defs/ZipRequestInput"
+ },
+ "response": {
+ "anyOf": [
+ {
+ "type": "object",
+ "properties": {
+ "code": {
+ "type": "integer",
+ "enum": [
+ 0
+ ]
+ },
+ "data": {
+ "type": "null"
+ },
+ "msg": {
+ "type": "string"
+ }
+ },
+ "required": [
+ "code",
+ "msg",
+ "data"
+ ],
+ "additionalProperties": false
+ },
+ {
+ "type": "object",
+ "properties": {
+ "code": {
+ "type": "integer",
+ "enum": [
+ -1
+ ]
+ },
+ "data": {
+ "anyOf": [
+ {
+ "type": "object",
+ "properties": {
+ "closeTimeout": {
+ "type": "number"
+ }
+ },
+ "required": [
+ "closeTimeout"
+ ],
+ "additionalProperties": false
+ },
+ {
+ "type": "null"
+ }
+ ]
+ },
+ "msg": {
+ "type": "string"
+ }
+ },
+ "required": [
+ "code",
+ "msg",
+ "data"
+ ],
+ "additionalProperties": true
+ }
+ ]
+ }
+ },
{
"method": "POST",
"path": "/api/attr/batchGetBlockAttrs",