mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-16 22:50:13 +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
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue