Skip to content

Commit

Permalink
🐛 File names ending with . will be considered as missing assets
Browse files Browse the repository at this point in the history
🎨 Improve HTML clipping #13355
  • Loading branch information
88250 committed Dec 4, 2024
1 parent 821a58d commit 22190b5
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 25 deletions.
10 changes: 5 additions & 5 deletions kernel/api/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("<svg ")) && bytes.HasSuffix(data, []byte("</svg>")) {
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
Expand Down
18 changes: 11 additions & 7 deletions kernel/model/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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("<svg ")) && bytes.HasSuffix(data, []byte("</svg>")) {
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)
Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 3 additions & 2 deletions kernel/model/caldav.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bytes"
"context"
"errors"
"github.com/siyuan-note/siyuan/kernel/util"
"os"
"path"
"strings"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions kernel/model/carddav.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion kernel/model/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion kernel/model/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
5 changes: 1 addition & 4 deletions kernel/model/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand Down
15 changes: 12 additions & 3 deletions kernel/util/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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()
Expand All @@ -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:]
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 22190b5

Please sign in to comment.