Improve the image loading performance in the database https://github.com/siyuan-note/siyuan/issues/15245

This commit is contained in:
Daniel 2025-07-09 16:18:53 +08:00
parent 9bb91c3a97
commit c852f6f51a
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
8 changed files with 113 additions and 8 deletions

View file

@ -491,9 +491,17 @@ func serveAssets(ginServer *gin.Engine) {
return
}
}
if serveThumbnail(context, p, requestPath) {
// 如果请求缩略图服务成功则返回
return
}
// 返回原始文件
http.ServeFile(context.Writer, context.Request, p)
return
})
ginServer.GET("/history/*path", model.CheckAuth, model.CheckAdminRole, func(context *gin.Context) {
p := filepath.Join(util.HistoryDir, context.Param("path"))
http.ServeFile(context.Writer, context.Request, p)
@ -501,6 +509,24 @@ func serveAssets(ginServer *gin.Engine) {
})
}
func serveThumbnail(context *gin.Context, assetAbsPath, requestPath string) bool {
if style := context.Query("style"); style == "thumb" && model.NeedGenerateAssetsThumbnail(assetAbsPath) { // 请求缩略图
thumbnailPath := filepath.Join(util.TempDir, "thumbnails", "assets", requestPath)
if !gulu.File.IsExist(thumbnailPath) {
// 如果缩略图不存在,则生成缩略图
err := model.GenerateAssetsThumbnail(assetAbsPath, thumbnailPath)
if err != nil {
logging.LogErrorf("generate thumbnail failed: %s", err)
return false
}
}
http.ServeFile(context.Writer, context.Request, thumbnailPath)
return true
}
return false
}
func serveRepoDiff(ginServer *gin.Engine) {
ginServer.GET("/repo/diff/*path", model.CheckAuth, model.CheckAdminRole, func(context *gin.Context) {
requestPath := context.Param("path")