Skip to content

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

:bug: PDF files with too long file names cannot generate annotated images https://github.com/siyuan-note/siyuan/issues/15739 https://github.com/siyuan-note/siyuan/issues/10666

Danielcfb976eb8904
diff --git a/kernel/api/extension.go b/kernel/api/extension.go
index 602c7919b7..e8b2c29932 100644
--- a/kernel/api/extension.go
+++ b/kernel/api/extension.go
@@ -154,7 +154,7 @@ func extensionCopy(c *gin.Context) {
fName += ext
}
- fName = util.AssetName(fName)
+ fName = util.AssetName(fName, ast.NewNodeID())
writePath := filepath.Join(assets, fName)
if err = filelock.WriteFile(writePath, data); err != nil {
ret.Code = -1
diff --git a/kernel/mobile/kernel.go b/kernel/mobile/kernel.go
index 05b88cca65..98d81ec0f0 100644
--- a/kernel/mobile/kernel.go
+++ b/kernel/mobile/kernel.go
@@ -26,6 +26,7 @@ import (
"time"
"github.com/88250/gulu"
+ "github.com/88250/lute/ast"
"github.com/siyuan-note/filelock"
"github.com/siyuan-note/httpclient"
"github.com/siyuan-note/logging"
@@ -289,5 +290,5 @@ func FilterUploadFileName(name string) string {
}
func AssetName(name string) string {
- return util.AssetName(name)
+ return util.AssetName(name, ast.NewNodeID())
}
diff --git a/kernel/model/assets.go b/kernel/model/assets.go
index 980dac5a83..3e55a3a09c 100644
--- a/kernel/model/assets.go
+++ b/kernel/model/assets.go
@@ -201,7 +201,7 @@ func NetAssets2LocalAssets(rootID string, onlyImg bool, originalURL string) (err
name := filepath.Base(u)
name = util.FilterUploadFileName(name)
name = "network-asset-" + name
- name = util.AssetName(name)
+ name = util.AssetName(name, ast.NewNodeID())
writePath := filepath.Join(assetsDirPath, name)
if err = filelock.Copy(u, writePath); err != nil {
logging.LogErrorf("copy [%s] to [%s] failed: %s", u, writePath, err)
@@ -303,7 +303,7 @@ func NetAssets2LocalAssets(rootID string, onlyImg bool, originalURL string) (err
name += ext
}
}
- name = util.AssetName(name)
+ name = util.AssetName(name, ast.NewNodeID())
name = "network-asset-" + name
writePath := filepath.Join(assetsDirPath, name)
if err = filelock.WriteFile(writePath, data); err != nil {
@@ -737,7 +737,7 @@ func RenameAsset(oldPath, newName string) (newPath string, err error) {
return
}
- newName = util.AssetName(newName + filepath.Ext(oldPath))
+ newName = util.AssetName(newName+filepath.Ext(oldPath), ast.NewNodeID())
parentDir := path.Dir(oldPath)
newPath = path.Join(parentDir, newName)
oldAbsPath, getErr := GetAssetAbsPath(oldPath)
diff --git a/kernel/model/carddav.go b/kernel/model/carddav.go
index a8e6c6d92b..3dc936382b 100644
--- a/kernel/model/carddav.go
+++ b/kernel/model/carddav.go
@@ -28,6 +28,7 @@ import (
"sync"
"github.com/88250/gulu"
+ "github.com/88250/lute/ast"
"github.com/emersion/go-vcard"
"github.com/emersion/go-webdav/carddav"
"github.com/siyuan-note/logging"
@@ -640,7 +641,7 @@ func (b *AddressBook) load() error {
addressesWaitGroup.Add(1)
go func() {
defer addressesWaitGroup.Done()
- filename_ := util.AssetName(filename)
+ filename_ := util.AssetName(filename, ast.NewNodeID())
address := &AddressObject{
FilePath: path.Join(b.DirectoryPath, filename_),
BookPath: b.MetaData.Path,
diff --git a/kernel/model/import.go b/kernel/model/import.go
index a61002c92c..b5b670705b 100644
--- a/kernel/model/import.go
+++ b/kernel/model/import.go
@@ -981,7 +981,7 @@ func ImportFromLocalPath(boxID, localPath string, toPath string) (err error) {
if "" == existName {
name = filepath.Base(absolutePath)
name = util.FilterUploadFileName(name)
- name = util.AssetName(name)
+ name = util.AssetName(name, ast.NewNodeID())
assetTargetPath := filepath.Join(assetDirPath, name)
if err = filelock.Copy(absolutePath, assetTargetPath); err != nil {
logging.LogErrorf("copy asset from [%s] to [%s] failed: %s", absolutePath, assetTargetPath, err)
@@ -1104,7 +1104,7 @@ func ImportFromLocalPath(boxID, localPath string, toPath string) (err error) {
if "" == existName {
name = filepath.Base(absolutePath)
name = util.FilterUploadFileName(name)
- name = util.AssetName(name)
+ name = util.AssetName(name, ast.NewNodeID())
assetTargetPath := filepath.Join(assetDirPath, name)
if err = filelock.Copy(absolutePath, assetTargetPath); err != nil {
logging.LogErrorf("copy asset from [%s] to [%s] failed: %s", absolutePath, assetTargetPath, err)
@@ -1249,7 +1249,7 @@ func processBase64Img(n *ast.Node, dest string, assetDirPath string) {
name = alt.TokensStr() + ext
}
name = util.FilterUploadFileName(name)
- name = util.AssetName(name)
+ name = util.AssetName(name, ast.NewNodeID())
tmp := filepath.Join(base64TmpDir, name)
tmpFile, openErr := os.OpenFile(tmp, os.O_RDWR|os.O_CREATE, 0644)
diff --git a/kernel/model/upload.go b/kernel/model/upload.go
index daa4e5240a..53b1f3cb9a 100644
--- a/kernel/model/upload.go
+++ b/kernel/model/upload.go
@@ -25,6 +25,7 @@ import (
"strings"
"github.com/88250/gulu"
+ "github.com/88250/lute/ast"
"github.com/gin-gonic/gin"
"github.com/siyuan-note/filelock"
"github.com/siyuan-note/logging"
@@ -93,7 +94,7 @@ func InsertLocalAssets(id string, assetAbsPaths []string, isUpload bool) (succMa
// 已经存在同样数据的资源文件的话不重复保存
succMap[baseName] = existAsset.Path
} else {
- fName = util.AssetName(fName)
+ fName = util.AssetName(fName, ast.NewNodeID())
writePath := filepath.Join(assetsDirPath, fName)
if _, err = f.Seek(0, io.SeekStart); err != nil {
f.Close()
@@ -163,6 +164,10 @@ func Upload(c *gin.Context) {
for _, file := range files {
baseName := file.Filename
+ _, lastID := util.LastID(baseName)
+ if !ast.IsNodeIDPattern(lastID) {
+ lastID = ""
+ }
needUnzip2Dir := false
if gulu.OS.IsDarwin() {
@@ -198,7 +203,16 @@ func Upload(c *gin.Context) {
} else {
if skipIfDuplicated {
// 复制 PDF 矩形注解时不再重复插入图片 No longer upload image repeatedly when copying PDF rectangle annotation https://github.com/siyuan-note/siyuan/issues/10666
- matches, globErr := filepath.Glob(assetsDirPath + string(os.PathSeparator) + strings.TrimSuffix(fName, ext) + "*" + ext)
+ pattern := assetsDirPath + string(os.PathSeparator) + strings.TrimSuffix(fName, ext)
+ _, patternLastID := util.LastID(fName)
+ if lastID != "" && lastID != patternLastID {
+ // 文件名太长被截断了,通过之前的 lastID 来匹配 PDF files with too long file names cannot generate annotated images https://github.com/siyuan-note/siyuan/issues/15739
+ pattern = assetsDirPath + string(os.PathSeparator) + "*" + lastID + ext
+ } else {
+ pattern += "*" + ext
+ }
+
+ matches, globErr := filepath.Glob(pattern)
if nil != globErr {
logging.LogErrorf("glob failed: %s", globErr)
} else {
@@ -211,7 +225,10 @@ func Upload(c *gin.Context) {
}
}
- fName = util.AssetName(fName)
+ if "" == lastID {
+ lastID = ast.NewNodeID()
+ }
+ fName = util.AssetName(fName, lastID)
writePath := filepath.Join(assetsDirPath, fName)
tmpDir := filepath.Join(util.TempDir, "convert", "zip", gulu.Rand.String(7))
if needUnzip2Dir {
@@ -248,7 +265,7 @@ func Upload(c *gin.Context) {
fName = strings.TrimSuffix(fName, ext)
ext = strings.ToLower(ext)
fName += ext
- fName = util.AssetName(fName)
+ fName = util.AssetName(fName, ast.NewNodeID())
tmpDir2 := filepath.Join(util.TempDir, "convert", "zip", gulu.Rand.String(7))
if err = gulu.Zip.Unzip(writePath, tmpDir2); err != nil {
errFiles = append(errFiles, fName)
diff --git a/kernel/util/file.go b/kernel/util/file.go
index 169d003e4a..afe035dc7d 100644
--- a/kernel/util/file.go
+++ b/kernel/util/file.go
@@ -172,12 +172,12 @@ func Ext(name string) (ret string) {
return
}
-func AssetName(name string) string {
+func AssetName(name, newID string) string {
_, id := LastID(name)
ext := Ext(name)
name = name[0 : len(name)-len(ext)]
if !ast.IsNodeIDPattern(id) {
- id = ast.NewNodeID()
+ id = newID
name = name + "-" + id + ext
} else {
if !ast.IsNodeIDPattern(name) {