Skip to content

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

:art: If a file with the same name exists during export PDF/Docx, it will be automatically renamed https://github.com/siyuan-note/siyuan/issues/11357

Daniel749ed8a4acfc
diff --git a/kernel/api/file.go b/kernel/api/file.go
index a842eb7115..e9453e8998 100644
--- a/kernel/api/file.go
+++ b/kernel/api/file.go
@@ -36,6 +36,21 @@ import (
"github.com/siyuan-note/siyuan/kernel/util"
)
+func getUniqueFilename(c *gin.Context) {
+ ret := gulu.Ret.NewResult()
+ defer c.JSON(http.StatusOK, ret)
+
+ arg, ok := util.JsonArg(c, ret)
+ if !ok {
+ return
+ }
+
+ filePath := arg["path"].(string)
+ ret.Data = map[string]interface{}{
+ "path": util.GetUniqueFilename(filePath),
+ }
+}
+
func globalCopyFiles(c *gin.Context) {
ret := gulu.Ret.NewResult()
defer c.JSON(http.StatusOK, ret)
diff --git a/kernel/api/router.go b/kernel/api/router.go
index b0b3d07b58..4801ecc5a6 100644
--- a/kernel/api/router.go
+++ b/kernel/api/router.go
@@ -208,6 +208,7 @@ func ServeAPI(ginServer *gin.Engine) {
ginServer.Handle("POST", "/api/file/removeFile", model.CheckAuth, model.CheckReadonly, removeFile)
ginServer.Handle("POST", "/api/file/renameFile", model.CheckAuth, model.CheckReadonly, renameFile)
ginServer.Handle("POST", "/api/file/readDir", model.CheckAuth, readDir)
+ ginServer.Handle("POST", "/api/file/getUniqueFilename", model.CheckAuth, getUniqueFilename)
ginServer.Handle("POST", "/api/ref/refreshBacklink", model.CheckAuth, refreshBacklink)
ginServer.Handle("POST", "/api/ref/getBacklink", model.CheckAuth, getBacklink)
diff --git a/kernel/model/export.go b/kernel/model/export.go
index 795aedffa4..58c31d7aae 100644
--- a/kernel/model/export.go
+++ b/kernel/model/export.go
@@ -180,7 +180,7 @@ func ExportAv2CSV(avID, blockID string) (zipPath string, err error) {
}
writer.Flush()
- zipPath = getUniqueFilename(exportFolder + ".db.zip")
+ zipPath = exportFolder + ".db.zip"
zip, err := gulu.Zip.Create(zipPath)
if nil != err {
logging.LogErrorf("create export .db.zip [%s] failed: %s", exportFolder, err)
@@ -209,24 +209,6 @@ func ExportAv2CSV(avID, blockID string) (zipPath string, err error) {
return
}
-func getUniqueFilename(filePath string) string {
- if !gulu.File.IsExist(filePath) {
- return filePath
- }
-
- ext := filepath.Ext(filePath)
- base := strings.TrimSuffix(filepath.Base(filePath), ext)
- dir := filepath.Dir(filePath)
- i := 1
- for {
- newPath := filepath.Join(dir, base+" ("+strconv.Itoa(i)+ext) + ")"
- if !gulu.File.IsExist(newPath) {
- return newPath
- }
- i++
- }
-}
-
func Export2Liandi(id string) (err error) {
tree, err := LoadTreeByBlockID(id)
if nil != err {
diff --git a/kernel/util/file.go b/kernel/util/file.go
index 4af6e40b06..75c19ed829 100644
--- a/kernel/util/file.go
+++ b/kernel/util/file.go
@@ -24,6 +24,7 @@ import (
"os"
"path"
"path/filepath"
+ "strconv"
"strings"
"unicode/utf8"
@@ -34,6 +35,24 @@ import (
"github.com/siyuan-note/logging"
)
+func GetUniqueFilename(filePath string) string {
+ if !gulu.File.IsExist(filePath) {
+ return filePath
+ }
+
+ ext := filepath.Ext(filePath)
+ base := strings.TrimSuffix(filepath.Base(filePath), ext)
+ dir := filepath.Dir(filePath)
+ i := 1
+ for {
+ newPath := filepath.Join(dir, base+" ("+strconv.Itoa(i)+")"+ext)
+ if !gulu.File.IsExist(newPath) {
+ return newPath
+ }
+ i++
+ }
+}
+
func GetMimeTypeByExt(filePath string) (ret string) {
ret = mime.TypeByExtension(filepath.Ext(filePath))
if "" == ret {