mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-16 22:50:13 +01:00
🐛 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
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
ext := path.Ext(fName)
|
fName = util.FilterUploadFileName(fName)
|
||||||
originalExt := ext
|
ext := util.Ext(fName)
|
||||||
if "" == ext || strings.Contains(ext, "!") {
|
if "" == ext || strings.Contains(ext, "!") {
|
||||||
// 改进浏览器剪藏扩展转换本地图片后缀 https://github.com/siyuan-note/siyuan/issues/7467
|
// 改进浏览器剪藏扩展转换本地图片后缀 https://github.com/siyuan-note/siyuan/issues/7467
|
||||||
if mtype := mimetype.Detect(data); nil != mtype {
|
if mtype := mimetype.Detect(data); nil != mtype {
|
||||||
ext = mtype.Extension()
|
ext = mtype.Extension()
|
||||||
|
fName += ext
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if "" == ext && bytes.HasPrefix(data, []byte("<svg ")) && bytes.HasSuffix(data, []byte("</svg>")) {
|
if "" == ext && bytes.HasPrefix(data, []byte("<svg ")) && bytes.HasSuffix(data, []byte("</svg>")) {
|
||||||
ext = ".svg"
|
ext = ".svg"
|
||||||
|
fName += ext
|
||||||
}
|
}
|
||||||
|
|
||||||
fName = fName[0 : len(fName)-len(originalExt)]
|
fName = util.AssetName(fName)
|
||||||
fName = util.FilterUploadFileName(fName)
|
|
||||||
fName = fName + "-" + ast.NewNodeID() + ext
|
|
||||||
writePath := filepath.Join(assets, fName)
|
writePath := filepath.Join(assets, fName)
|
||||||
if err = filelock.WriteFile(writePath, data); err != nil {
|
if err = filelock.WriteFile(writePath, data); err != nil {
|
||||||
ret.Code = -1
|
ret.Code = -1
|
||||||
|
|
|
||||||
|
|
@ -123,7 +123,6 @@ func NetAssets2LocalAssets(rootID string, onlyImg bool, originalURL string) (err
|
||||||
|
|
||||||
name := filepath.Base(u)
|
name := filepath.Base(u)
|
||||||
name = util.FilterUploadFileName(name)
|
name = util.FilterUploadFileName(name)
|
||||||
name = util.TruncateLenFileName(name)
|
|
||||||
name = "network-asset-" + name
|
name = "network-asset-" + name
|
||||||
name = util.AssetName(name)
|
name = util.AssetName(name)
|
||||||
writePath := filepath.Join(assetsDirPath, 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 = name[:strings.Index(name, "#")]
|
||||||
}
|
}
|
||||||
name, _ = url.PathUnescape(name)
|
name, _ = url.PathUnescape(name)
|
||||||
ext := path.Ext(name)
|
name = util.FilterUploadFileName(name)
|
||||||
|
ext := util.Ext(name)
|
||||||
if "" == ext {
|
if "" == ext {
|
||||||
if mtype := mimetype.Detect(data); nil != mtype {
|
if mtype := mimetype.Detect(data); nil != mtype {
|
||||||
ext = mtype.Extension()
|
ext = mtype.Extension()
|
||||||
|
name += ext
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if "" == ext && bytes.HasPrefix(data, []byte("<svg ")) && bytes.HasSuffix(data, []byte("</svg>")) {
|
||||||
|
ext = ".svg"
|
||||||
|
name += ext
|
||||||
|
}
|
||||||
if "" == ext {
|
if "" == ext {
|
||||||
contentType := resp.Header.Get("Content-Type")
|
contentType := resp.Header.Get("Content-Type")
|
||||||
exts, _ := mime.ExtensionsByType(contentType)
|
exts, _ := mime.ExtensionsByType(contentType)
|
||||||
if 0 < len(exts) {
|
if 0 < len(exts) {
|
||||||
ext = exts[0]
|
ext = exts[0]
|
||||||
|
name += ext
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
name = strings.TrimSuffix(name, ext)
|
name = util.AssetName(name)
|
||||||
name = util.FilterUploadFileName(name)
|
name = "network-asset-" + name
|
||||||
name = util.TruncateLenFileName(name)
|
|
||||||
name = "network-asset-" + name + "-" + ast.NewNodeID() + ext
|
|
||||||
writePath := filepath.Join(assetsDirPath, name)
|
writePath := filepath.Join(assetsDirPath, name)
|
||||||
if err = filelock.WriteFile(writePath, data); err != nil {
|
if err = filelock.WriteFile(writePath, data); err != nil {
|
||||||
logging.LogErrorf("write downloaded network asset [%s] to local asset [%s] failed: %s", u, writePath, err)
|
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()
|
defer util.PushClearProgress()
|
||||||
|
|
||||||
newName = strings.TrimSpace(newName)
|
newName = strings.TrimSpace(newName)
|
||||||
newName = util.RemoveInvalid(newName)
|
newName = util.FilterFileName(newName)
|
||||||
if path.Base(oldPath) == newName {
|
if path.Base(oldPath) == newName {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"github.com/siyuan-note/siyuan/kernel/util"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
@ -100,7 +101,7 @@ func ParseCalendarObjectPath(objectPath string) (calendarPath string, objectID s
|
||||||
calendarPath, objectFileName := path.Split(objectPath)
|
calendarPath, objectFileName := path.Split(objectPath)
|
||||||
calendarPath = PathCleanWithSlash(calendarPath)
|
calendarPath = PathCleanWithSlash(calendarPath)
|
||||||
objectID = path.Base(objectFileName)
|
objectID = path.Base(objectFileName)
|
||||||
objectFileExt := path.Ext(objectFileName)
|
objectFileExt := util.Ext(objectFileName)
|
||||||
|
|
||||||
if GetCalDavPathDepth(calendarPath) != calDavPathDepth_Calendar {
|
if GetCalDavPathDepth(calendarPath) != calDavPathDepth_Calendar {
|
||||||
err = ErrorCalDavCalendarPathInvalid
|
err = ErrorCalDavCalendarPathInvalid
|
||||||
|
|
@ -520,7 +521,7 @@ func (c *Calendar) load() error {
|
||||||
for _, entry := range entries {
|
for _, entry := range entries {
|
||||||
if !entry.IsDir() {
|
if !entry.IsDir() {
|
||||||
filename := entry.Name()
|
filename := entry.Name()
|
||||||
ext := path.Ext(filename)
|
ext := util.Ext(filename)
|
||||||
if ext == ICalendarFileExt {
|
if ext == ICalendarFileExt {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
|
|
|
||||||
|
|
@ -128,7 +128,7 @@ func GetCardDavPathDepth(urlPath string) CardDavPathDepth {
|
||||||
func ParseAddressPath(addressPath string) (addressBookPath string, addressID string, err error) {
|
func ParseAddressPath(addressPath string) (addressBookPath string, addressID string, err error) {
|
||||||
addressBookPath, addressFileName := path.Split(addressPath)
|
addressBookPath, addressFileName := path.Split(addressPath)
|
||||||
addressID = path.Base(addressFileName)
|
addressID = path.Base(addressFileName)
|
||||||
addressFileExt := path.Ext(addressFileName)
|
addressFileExt := util.Ext(addressFileName)
|
||||||
|
|
||||||
if GetCardDavPathDepth(addressBookPath) != cardDavPathDepth_AddressBook {
|
if GetCardDavPathDepth(addressBookPath) != cardDavPathDepth_AddressBook {
|
||||||
err = ErrorCardDavBookPathInvalid
|
err = ErrorCardDavBookPathInvalid
|
||||||
|
|
@ -604,7 +604,7 @@ func (b *AddressBook) load() error {
|
||||||
for _, entry := range entries {
|
for _, entry := range entries {
|
||||||
if !entry.IsDir() {
|
if !entry.IsDir() {
|
||||||
filename := entry.Name()
|
filename := entry.Name()
|
||||||
ext := path.Ext(filename)
|
ext := util.Ext(filename)
|
||||||
if ext == VCardFileExt {
|
if ext == VCardFileExt {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
|
|
|
||||||
|
|
@ -737,7 +737,7 @@ func ImportFromLocalPath(boxID, localPath string, toPath string) (err error) {
|
||||||
var ext string
|
var ext string
|
||||||
title := d.Name()
|
title := d.Name()
|
||||||
if !d.IsDir() {
|
if !d.IsDir() {
|
||||||
ext = path.Ext(d.Name())
|
ext = util.Ext(d.Name())
|
||||||
title = strings.TrimSuffix(d.Name(), ext)
|
title = strings.TrimSuffix(d.Name(), ext)
|
||||||
}
|
}
|
||||||
id := ast.NewNodeID()
|
id := ast.NewNodeID()
|
||||||
|
|
|
||||||
|
|
@ -500,7 +500,7 @@ func buildSnapshots(logs []*dejavu.Log) (ret []*Snapshot) {
|
||||||
|
|
||||||
func statTypesByPath(files []*entity.File) (ret []*TypeCount) {
|
func statTypesByPath(files []*entity.File) (ret []*TypeCount) {
|
||||||
for _, f := range files {
|
for _, f := range files {
|
||||||
ext := path.Ext(f.Path)
|
ext := util.Ext(f.Path)
|
||||||
if "" == ext {
|
if "" == ext {
|
||||||
ext = "NoExt"
|
ext = "NoExt"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,6 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/88250/gulu"
|
"github.com/88250/gulu"
|
||||||
"github.com/88250/lute/ast"
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/siyuan-note/filelock"
|
"github.com/siyuan-note/filelock"
|
||||||
"github.com/siyuan-note/logging"
|
"github.com/siyuan-note/logging"
|
||||||
|
|
@ -87,9 +86,7 @@ func InsertLocalAssets(id string, assetPaths []string, isUpload bool) (succMap m
|
||||||
// 已经存在同样数据的资源文件的话不重复保存
|
// 已经存在同样数据的资源文件的话不重复保存
|
||||||
succMap[baseName] = existAsset.Path
|
succMap[baseName] = existAsset.Path
|
||||||
} else {
|
} else {
|
||||||
ext := path.Ext(fName)
|
fName = util.AssetName(fName)
|
||||||
fName = fName[0 : len(fName)-len(ext)]
|
|
||||||
fName = fName + "-" + ast.NewNodeID() + ext
|
|
||||||
writePath := filepath.Join(assetsDirPath, fName)
|
writePath := filepath.Join(assetsDirPath, fName)
|
||||||
if _, err = f.Seek(0, io.SeekStart); err != nil {
|
if _, err = f.Seek(0, io.SeekStart); err != nil {
|
||||||
f.Close()
|
f.Close()
|
||||||
|
|
|
||||||
|
|
@ -132,7 +132,7 @@ func IsPathRegularDirOrSymlinkDir(path string) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func RemoveID(name string) string {
|
func RemoveID(name string) string {
|
||||||
ext := path.Ext(name)
|
ext := Ext(name)
|
||||||
name = strings.TrimSuffix(name, ext)
|
name = strings.TrimSuffix(name, ext)
|
||||||
if 23 < len(name) {
|
if 23 < len(name) {
|
||||||
if id := name[len(name)-22:]; ast.IsNodeIDPattern(id) {
|
if id := name[len(name)-22:]; ast.IsNodeIDPattern(id) {
|
||||||
|
|
@ -142,9 +142,17 @@ func RemoveID(name string) string {
|
||||||
return name + ext
|
return name + ext
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Ext(name string) (ret string) {
|
||||||
|
ret = path.Ext(name)
|
||||||
|
if "." == ret {
|
||||||
|
ret = ""
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func AssetName(name string) string {
|
func AssetName(name string) string {
|
||||||
_, id := LastID(name)
|
_, id := LastID(name)
|
||||||
ext := path.Ext(name)
|
ext := Ext(name)
|
||||||
name = name[0 : len(name)-len(ext)]
|
name = name[0 : len(name)-len(ext)]
|
||||||
if !ast.IsNodeIDPattern(id) {
|
if !ast.IsNodeIDPattern(id) {
|
||||||
id = ast.NewNodeID()
|
id = ast.NewNodeID()
|
||||||
|
|
@ -161,7 +169,7 @@ func AssetName(name string) string {
|
||||||
|
|
||||||
func LastID(p string) (name, id string) {
|
func LastID(p string) (name, id string) {
|
||||||
name = path.Base(p)
|
name = path.Base(p)
|
||||||
ext := path.Ext(name)
|
ext := Ext(name)
|
||||||
id = strings.TrimSuffix(name, ext)
|
id = strings.TrimSuffix(name, ext)
|
||||||
if 22 < len(id) {
|
if 22 < len(id) {
|
||||||
id = id[len(id)-22:]
|
id = id[len(id)-22:]
|
||||||
|
|
@ -242,6 +250,7 @@ func FilterFileName(name string) string {
|
||||||
name = strings.ReplaceAll(name, ">", "_")
|
name = strings.ReplaceAll(name, ">", "_")
|
||||||
name = strings.ReplaceAll(name, "|", "_")
|
name = strings.ReplaceAll(name, "|", "_")
|
||||||
name = strings.TrimSpace(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
|
name = RemoveInvalid(name) // Remove invisible characters from file names when uploading assets https://github.com/siyuan-note/siyuan/issues/11683
|
||||||
return name
|
return name
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue