From 22190b532db010509cf98955ded8ea0d3204bd24 Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Wed, 4 Dec 2024 22:37:22 +0800 Subject: [PATCH] :bug: File names ending with `.` will be considered as missing assets :art: Improve HTML clipping https://github.com/siyuan-note/siyuan/issues/13355 --- kernel/api/extension.go | 10 +++++----- kernel/model/assets.go | 18 +++++++++++------- kernel/model/caldav.go | 5 +++-- kernel/model/carddav.go | 4 ++-- kernel/model/import.go | 2 +- kernel/model/repository.go | 2 +- kernel/model/upload.go | 5 +---- kernel/util/file.go | 15 ++++++++++++--- 8 files changed, 36 insertions(+), 25 deletions(-) diff --git a/kernel/api/extension.go b/kernel/api/extension.go index 738a808e2b0..58cdba28d9e 100644 --- a/kernel/api/extension.go +++ b/kernel/api/extension.go @@ -97,21 +97,21 @@ func extensionCopy(c *gin.Context) { break } - ext := path.Ext(fName) - originalExt := ext + fName = util.FilterUploadFileName(fName) + ext := util.Ext(fName) if "" == ext || strings.Contains(ext, "!") { // 改进浏览器剪藏扩展转换本地图片后缀 https://github.com/siyuan-note/siyuan/issues/7467 if mtype := mimetype.Detect(data); nil != mtype { ext = mtype.Extension() + fName += ext } } if "" == ext && bytes.HasPrefix(data, []byte("")) { ext = ".svg" + fName += ext } - fName = fName[0 : len(fName)-len(originalExt)] - fName = util.FilterUploadFileName(fName) - fName = fName + "-" + ast.NewNodeID() + ext + fName = util.AssetName(fName) writePath := filepath.Join(assets, fName) if err = filelock.WriteFile(writePath, data); err != nil { ret.Code = -1 diff --git a/kernel/model/assets.go b/kernel/model/assets.go index 175ca67a870..15640fc253d 100644 --- a/kernel/model/assets.go +++ b/kernel/model/assets.go @@ -123,7 +123,6 @@ func NetAssets2LocalAssets(rootID string, onlyImg bool, originalURL string) (err name := filepath.Base(u) name = util.FilterUploadFileName(name) - name = util.TruncateLenFileName(name) name = "network-asset-" + name name = util.AssetName(name) writePath := filepath.Join(assetsDirPath, name) @@ -205,23 +204,28 @@ func NetAssets2LocalAssets(rootID string, onlyImg bool, originalURL string) (err name = name[:strings.Index(name, "#")] } name, _ = url.PathUnescape(name) - ext := path.Ext(name) + name = util.FilterUploadFileName(name) + ext := util.Ext(name) if "" == ext { if mtype := mimetype.Detect(data); nil != mtype { ext = mtype.Extension() + name += ext } } + if "" == ext && bytes.HasPrefix(data, []byte("")) { + ext = ".svg" + name += ext + } if "" == ext { contentType := resp.Header.Get("Content-Type") exts, _ := mime.ExtensionsByType(contentType) if 0 < len(exts) { ext = exts[0] + name += ext } } - name = strings.TrimSuffix(name, ext) - name = util.FilterUploadFileName(name) - name = util.TruncateLenFileName(name) - name = "network-asset-" + name + "-" + ast.NewNodeID() + ext + name = util.AssetName(name) + name = "network-asset-" + name writePath := filepath.Join(assetsDirPath, name) if err = filelock.WriteFile(writePath, data); err != nil { logging.LogErrorf("write downloaded network asset [%s] to local asset [%s] failed: %s", u, writePath, err) @@ -607,7 +611,7 @@ func RenameAsset(oldPath, newName string) (newPath string, err error) { defer util.PushClearProgress() newName = strings.TrimSpace(newName) - newName = util.RemoveInvalid(newName) + newName = util.FilterFileName(newName) if path.Base(oldPath) == newName { return } diff --git a/kernel/model/caldav.go b/kernel/model/caldav.go index 385ab3e105b..1df6826d70d 100644 --- a/kernel/model/caldav.go +++ b/kernel/model/caldav.go @@ -20,6 +20,7 @@ import ( "bytes" "context" "errors" + "github.com/siyuan-note/siyuan/kernel/util" "os" "path" "strings" @@ -100,7 +101,7 @@ func ParseCalendarObjectPath(objectPath string) (calendarPath string, objectID s calendarPath, objectFileName := path.Split(objectPath) calendarPath = PathCleanWithSlash(calendarPath) objectID = path.Base(objectFileName) - objectFileExt := path.Ext(objectFileName) + objectFileExt := util.Ext(objectFileName) if GetCalDavPathDepth(calendarPath) != calDavPathDepth_Calendar { err = ErrorCalDavCalendarPathInvalid @@ -520,7 +521,7 @@ func (c *Calendar) load() error { for _, entry := range entries { if !entry.IsDir() { filename := entry.Name() - ext := path.Ext(filename) + ext := util.Ext(filename) if ext == ICalendarFileExt { wg.Add(1) go func() { diff --git a/kernel/model/carddav.go b/kernel/model/carddav.go index acb940ab82b..a8e6c6d92ba 100644 --- a/kernel/model/carddav.go +++ b/kernel/model/carddav.go @@ -128,7 +128,7 @@ func GetCardDavPathDepth(urlPath string) CardDavPathDepth { func ParseAddressPath(addressPath string) (addressBookPath string, addressID string, err error) { addressBookPath, addressFileName := path.Split(addressPath) addressID = path.Base(addressFileName) - addressFileExt := path.Ext(addressFileName) + addressFileExt := util.Ext(addressFileName) if GetCardDavPathDepth(addressBookPath) != cardDavPathDepth_AddressBook { err = ErrorCardDavBookPathInvalid @@ -604,7 +604,7 @@ func (b *AddressBook) load() error { for _, entry := range entries { if !entry.IsDir() { filename := entry.Name() - ext := path.Ext(filename) + ext := util.Ext(filename) if ext == VCardFileExt { wg.Add(1) go func() { diff --git a/kernel/model/import.go b/kernel/model/import.go index 8efc7c7fb3d..4d69a3a1a56 100644 --- a/kernel/model/import.go +++ b/kernel/model/import.go @@ -737,7 +737,7 @@ func ImportFromLocalPath(boxID, localPath string, toPath string) (err error) { var ext string title := d.Name() if !d.IsDir() { - ext = path.Ext(d.Name()) + ext = util.Ext(d.Name()) title = strings.TrimSuffix(d.Name(), ext) } id := ast.NewNodeID() diff --git a/kernel/model/repository.go b/kernel/model/repository.go index 987ad3493d8..7aa01de5cee 100644 --- a/kernel/model/repository.go +++ b/kernel/model/repository.go @@ -500,7 +500,7 @@ func buildSnapshots(logs []*dejavu.Log) (ret []*Snapshot) { func statTypesByPath(files []*entity.File) (ret []*TypeCount) { for _, f := range files { - ext := path.Ext(f.Path) + ext := util.Ext(f.Path) if "" == ext { ext = "NoExt" } diff --git a/kernel/model/upload.go b/kernel/model/upload.go index 97bf73373db..c21a4aa8dfe 100644 --- a/kernel/model/upload.go +++ b/kernel/model/upload.go @@ -25,7 +25,6 @@ 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" @@ -87,9 +86,7 @@ func InsertLocalAssets(id string, assetPaths []string, isUpload bool) (succMap m // 已经存在同样数据的资源文件的话不重复保存 succMap[baseName] = existAsset.Path } else { - ext := path.Ext(fName) - fName = fName[0 : len(fName)-len(ext)] - fName = fName + "-" + ast.NewNodeID() + ext + fName = util.AssetName(fName) writePath := filepath.Join(assetsDirPath, fName) if _, err = f.Seek(0, io.SeekStart); err != nil { f.Close() diff --git a/kernel/util/file.go b/kernel/util/file.go index 112d0c2a6e6..79ed52c747a 100644 --- a/kernel/util/file.go +++ b/kernel/util/file.go @@ -132,7 +132,7 @@ func IsPathRegularDirOrSymlinkDir(path string) bool { } func RemoveID(name string) string { - ext := path.Ext(name) + ext := Ext(name) name = strings.TrimSuffix(name, ext) if 23 < len(name) { if id := name[len(name)-22:]; ast.IsNodeIDPattern(id) { @@ -142,9 +142,17 @@ func RemoveID(name string) string { return name + ext } +func Ext(name string) (ret string) { + ret = path.Ext(name) + if "." == ret { + ret = "" + } + return +} + func AssetName(name string) string { _, id := LastID(name) - ext := path.Ext(name) + ext := Ext(name) name = name[0 : len(name)-len(ext)] if !ast.IsNodeIDPattern(id) { id = ast.NewNodeID() @@ -161,7 +169,7 @@ func AssetName(name string) string { func LastID(p string) (name, id string) { name = path.Base(p) - ext := path.Ext(name) + ext := Ext(name) id = strings.TrimSuffix(name, ext) if 22 < len(id) { id = id[len(id)-22:] @@ -242,6 +250,7 @@ func FilterFileName(name string) string { name = strings.ReplaceAll(name, ">", "_") name = strings.ReplaceAll(name, "|", "_") name = strings.TrimSpace(name) + name = strings.TrimSuffix(name, ".") name = RemoveInvalid(name) // Remove invisible characters from file names when uploading assets https://github.com/siyuan-note/siyuan/issues/11683 return name }