🐛 File names ending with .
will be considered as missing assets
🎨 Improve HTML clipping https://github.com/siyuan-note/siyuan/issues/13355
This commit is contained in:
parent
821a58d895
commit
22190b532d
8 changed files with 36 additions and 25 deletions
|
@ -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
|
||||
|
|
|
@ -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("<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)
|
||||
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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"
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue