mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-22 01:20:12 +01:00
⚡ Improve the image loading performance in the database https://github.com/siyuan-note/siyuan/issues/15245
This commit is contained in:
parent
9bb91c3a97
commit
c852f6f51a
8 changed files with 113 additions and 8 deletions
|
|
@ -292,7 +292,7 @@ func buildAssetContentOrderBy(orderBy int) string {
|
|||
|
||||
var assetContentSearcher = NewAssetsSearcher()
|
||||
|
||||
func RemoveIndexAssetContent(absPath string) {
|
||||
func removeIndexAssetContent(absPath string) {
|
||||
defer logging.Recover()
|
||||
|
||||
assetsDir := util.GetDataAssetsAbsPath()
|
||||
|
|
@ -300,7 +300,7 @@ func RemoveIndexAssetContent(absPath string) {
|
|||
sql.DeleteAssetContentsByPathQueue(p)
|
||||
}
|
||||
|
||||
func IndexAssetContent(absPath string) {
|
||||
func indexAssetContent(absPath string) {
|
||||
defer logging.Recover()
|
||||
|
||||
ext := filepath.Ext(absPath)
|
||||
|
|
|
|||
|
|
@ -37,6 +37,7 @@ import (
|
|||
"github.com/88250/lute/editor"
|
||||
"github.com/88250/lute/html"
|
||||
"github.com/88250/lute/parse"
|
||||
"github.com/disintegration/imaging"
|
||||
"github.com/gabriel-vasile/mimetype"
|
||||
"github.com/siyuan-note/filelock"
|
||||
"github.com/siyuan-note/httpclient"
|
||||
|
|
@ -50,6 +51,74 @@ import (
|
|||
"github.com/siyuan-note/siyuan/kernel/util"
|
||||
)
|
||||
|
||||
func HandleAssetsRemoveEvent(assetAbsPath string) {
|
||||
removeIndexAssetContent(assetAbsPath)
|
||||
removeAssetThumbnail(assetAbsPath)
|
||||
}
|
||||
|
||||
func HandleAssetsChangeEvent(assetAbsPath string) {
|
||||
indexAssetContent(assetAbsPath)
|
||||
removeAssetThumbnail(assetAbsPath)
|
||||
}
|
||||
|
||||
func removeAssetThumbnail(assetAbsPath string) {
|
||||
if util.IsCompressibleAssetImage(assetAbsPath) {
|
||||
p := filepath.ToSlash(assetAbsPath)
|
||||
idx := strings.Index(p, "assets/")
|
||||
if -1 == idx {
|
||||
return
|
||||
}
|
||||
thumbnailPath := filepath.Join(util.TempDir, "thumbnails", "assets", p[idx+7:])
|
||||
os.RemoveAll(thumbnailPath)
|
||||
}
|
||||
}
|
||||
|
||||
func NeedGenerateAssetsThumbnail(sourceImgPath string) bool {
|
||||
info, err := os.Stat(sourceImgPath)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if info.IsDir() {
|
||||
return false
|
||||
}
|
||||
return info.Size() > 1024*10
|
||||
}
|
||||
|
||||
func GenerateAssetsThumbnail(sourceImgPath, resizedImgPath string) (err error) {
|
||||
start := time.Now()
|
||||
img, err := imaging.Open(sourceImgPath)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 获取原图宽高
|
||||
originalWidth := img.Bounds().Dx()
|
||||
originalHeight := img.Bounds().Dy()
|
||||
|
||||
// 固定最大宽度为 520,计算缩放比例
|
||||
maxWidth := 520
|
||||
scale := float64(maxWidth) / float64(originalWidth)
|
||||
|
||||
// 按比例计算新的宽高
|
||||
newWidth := maxWidth
|
||||
newHeight := int(float64(originalHeight) * scale)
|
||||
|
||||
// 缩放图片
|
||||
resizedImg := imaging.Resize(img, newWidth, newHeight, imaging.Lanczos)
|
||||
|
||||
// 保存缩放后的图片
|
||||
err = os.MkdirAll(filepath.Dir(resizedImgPath), 0755)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = imaging.Save(resizedImg, resizedImgPath)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
logging.LogDebugf("generated thumbnail image [%s] to [%s], cost [%d]ms", sourceImgPath, resizedImgPath, time.Since(start).Milliseconds())
|
||||
return
|
||||
}
|
||||
|
||||
func DocImageAssets(rootID string) (ret []string, err error) {
|
||||
tree, err := LoadTreeByBlockID(rootID)
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -75,9 +75,9 @@ func watchAssets() {
|
|||
timer.Reset(time.Millisecond * 100)
|
||||
|
||||
if lastEvent.Op&fsnotify.Rename == fsnotify.Rename || lastEvent.Op&fsnotify.Write == fsnotify.Write {
|
||||
IndexAssetContent(lastEvent.Name)
|
||||
HandleAssetsChangeEvent(lastEvent.Name)
|
||||
} else if lastEvent.Op&fsnotify.Remove == fsnotify.Remove {
|
||||
RemoveIndexAssetContent(lastEvent.Name)
|
||||
HandleAssetsRemoveEvent(lastEvent.Name)
|
||||
}
|
||||
case err, ok := <-assetsWatcher.Errors:
|
||||
if !ok {
|
||||
|
|
@ -94,9 +94,9 @@ func watchAssets() {
|
|||
go cache.LoadAssets()
|
||||
|
||||
if lastEvent.Op&fsnotify.Remove == fsnotify.Remove {
|
||||
RemoveIndexAssetContent(lastEvent.Name)
|
||||
HandleAssetsRemoveEvent(lastEvent.Name)
|
||||
} else {
|
||||
IndexAssetContent(lastEvent.Name)
|
||||
HandleAssetsChangeEvent(lastEvent.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -61,9 +61,9 @@ func watchAssets() {
|
|||
go cache.LoadAssets()
|
||||
|
||||
if watcher.Remove == event.Op {
|
||||
RemoveIndexAssetContent(event.Path)
|
||||
HandleAssetsRemoveEvent(event.Path)
|
||||
} else {
|
||||
IndexAssetContent(event.Path)
|
||||
HandleAssetsChangeEvent(event.Path)
|
||||
}
|
||||
case err, ok := <-assetsWatcher.Error:
|
||||
if !ok {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue