Skip to content

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

:art: https://github.com/siyuan-note/siyuan/pull/17647

Daniela7a6f2b0d118
diff --git a/app/src/protyle/util/compatibility.ts b/app/src/protyle/util/compatibility.ts
index 8845e9d155..37c640bcb2 100644
--- a/app/src/protyle/util/compatibility.ts
+++ b/app/src/protyle/util/compatibility.ts
@@ -3,7 +3,6 @@ import {fetchPost, fetchSyncPost} from "../../util/fetch";
import {Constants} from "../../constants";
/// #if !BROWSER
import {ipcRenderer} from "electron";
-import * as fs from "fs";
/// #endif
/// #if MOBILE
import {processSYLink} from "../../editor/openLink";
@@ -125,24 +124,16 @@ export const saveExportFile = async (uri: string) => {
if (result.canceled || !result.filePath) {
return;
}
- const response = await fetch(resolved.href);
- if (!response.ok) {
- throw new Error(
- `HTTP ${response.status} ${response.statusText}: ${response.url || resolved.href}`
- );
- }
- const reader = response.body.getReader();
- const fd = await fs.promises.open(result.filePath, "w");
- try {
- while (true) {
- const {done, value} = await reader.read();
- if (done) {
- break;
- }
- await fd.write(value);
- }
- } finally {
- await fd.close();
+ const copyResponse = await (await fetch("/api/export/copyExportFile", {
+ method: "POST",
+ headers: {"Content-Type": "application/json"},
+ body: JSON.stringify({
+ srcPath: resolved.pathname,
+ dest: result.filePath,
+ }),
+ })).json();
+ if (copyResponse.code !== 0) {
+ throw new Error(copyResponse.msg);
}
showMessage(window.siyuan.languages.exported);
return;
diff --git a/kernel/api/export.go b/kernel/api/export.go
index b3bdde386e..d8b95e28dc 100644
--- a/kernel/api/export.go
+++ b/kernel/api/export.go
@@ -928,3 +928,54 @@ func exportAsFile(c *gin.Context) {
"file": path.Join("/export/", name),
}
}
+
+func copyExportFile(c *gin.Context) {
+ ret := gulu.Ret.NewResult()
+ defer c.JSON(http.StatusOK, ret)
+
+ arg, ok := util.JsonArg(c, ret)
+ if !ok {
+ return
+ }
+
+ var srcPath, dest string
+ if !util.ParseJsonArgs(arg, ret,
+ util.BindJsonArg("srcPath", &srcPath, true, true),
+ util.BindJsonArg("dest", &dest, true, true),
+ ) {
+ return
+ }
+
+ if !filepath.IsAbs(dest) {
+ ret.Code = -1
+ ret.Msg = "dest must be an absolute path"
+ return
+ }
+
+ srcPath = filepath.Clean(srcPath)
+ if decoded, err := url.PathUnescape(srcPath); err == nil {
+ srcPath = decoded
+ }
+ srcFullPath := filepath.Join(util.TempDir, srcPath)
+ srcFullPath = filepath.Clean(srcFullPath)
+
+ exportBaseDir := filepath.Join(util.TempDir, "export")
+ if !gulu.File.IsSubPath(exportBaseDir, srcFullPath) && srcFullPath != exportBaseDir {
+ ret.Code = -1
+ ret.Msg = "invalid source path"
+ return
+ }
+
+ if util.IsSensitivePath(dest) {
+ ret.Code = -2
+ ret.Msg = "refuse to copy to sensitive path: " + dest
+ return
+ }
+
+ if err := filelock.Copy(srcFullPath, dest); err != nil {
+ logging.LogErrorf("copy export file [%s] to [%s] failed: %s", srcFullPath, dest, err)
+ ret.Code = -1
+ ret.Msg = err.Error()
+ return
+ }
+}
diff --git a/kernel/api/router.go b/kernel/api/router.go
index dc60e97acc..0c388b8349 100644
--- a/kernel/api/router.go
+++ b/kernel/api/router.go
@@ -368,6 +368,7 @@ func ServeAPI(ginServer *gin.Engine) {
ginServer.Handle("POST", "/api/export/exportEPUB", model.CheckAuth, model.CheckAdminRole, exportEPUB)
ginServer.Handle("POST", "/api/export/exportAttributeView", model.CheckAuth, model.CheckAdminRole, exportAttributeView)
ginServer.Handle("POST", "/api/export/exportCodeBlock", model.CheckAuth, model.CheckAdminRole, exportCodeBlock)
+ ginServer.Handle("POST", "/api/export/copyExportFile", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, copyExportFile)
ginServer.Handle("POST", "/api/import/importStdMd", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, importStdMd)
ginServer.Handle("POST", "/api/import/importZipMd", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, importZipMd)