mirror of
https://github.com/photoprism/photoprism.git
synced 2025-12-12 00:34:13 +01:00
Improve thumbnail rendering and indexing performance
This commit is contained in:
@@ -2,9 +2,9 @@ package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/photoprism/photoprism/internal/config"
|
||||
"github.com/photoprism/photoprism/internal/util"
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -17,19 +17,20 @@ var photoIconSvg = []byte(`
|
||||
<path d="M21 19V5c0-1.1-.9-2-2-2H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2zM8.5 13.5l2.5 3.01L14.5 12l4.5 6H5l3.5-4.5z"/>
|
||||
</svg>`)
|
||||
|
||||
// GET /api/v1/thumbnails/:type/:size/:hash
|
||||
// GET /api/v1/thumbnails/:hash/:type
|
||||
//
|
||||
// Parameters:
|
||||
// type: string Format, either "fit" or "square"
|
||||
// size: int Size in pixels
|
||||
// hash: string The file hash as returned by the search API
|
||||
// type: string Thumbnail type, see photoprism.ThumbnailTypes
|
||||
func GetThumbnail(router *gin.RouterGroup, conf *config.Config) {
|
||||
router.GET("/thumbnails/:type/:size/:hash", func(c *gin.Context) {
|
||||
router.GET("/thumbnails/:hash/:type", func(c *gin.Context) {
|
||||
fileHash := c.Param("hash")
|
||||
thumbnailType := c.Param("type")
|
||||
size, err := strconv.Atoi(c.Param("size"))
|
||||
if err != nil {
|
||||
log.Errorf("invalid size: %s", c.Param("size"))
|
||||
typeName := c.Param("type")
|
||||
|
||||
thumbType, ok := photoprism.ThumbnailTypes[typeName]
|
||||
|
||||
if !ok {
|
||||
log.Errorf("invalid type: %s", typeName)
|
||||
c.Data(400, "image/svg+xml", photoIconSvg)
|
||||
return
|
||||
}
|
||||
@@ -44,9 +45,8 @@ func GetThumbnail(router *gin.RouterGroup, conf *config.Config) {
|
||||
|
||||
fileName := fmt.Sprintf("%s/%s", conf.OriginalsPath(), file.FileName)
|
||||
|
||||
mediaFile, err := photoprism.NewMediaFile(fileName)
|
||||
if err != nil {
|
||||
log.Errorf("could not find image for thumbnail: %s", err.Error())
|
||||
if !util.Exists(fileName) {
|
||||
log.Errorf("could not find original for thumbnail: %s", fileName)
|
||||
c.Data(404, "image/svg+xml", photoIconSvg)
|
||||
|
||||
// Set missing flag so that the file doesn't show up in search results anymore
|
||||
@@ -55,23 +55,10 @@ func GetThumbnail(router *gin.RouterGroup, conf *config.Config) {
|
||||
return
|
||||
}
|
||||
|
||||
switch thumbnailType {
|
||||
case "fit":
|
||||
if thumbnail, err := mediaFile.Thumbnail(conf.ThumbnailsPath(), size); err == nil {
|
||||
c.File(thumbnail.Filename())
|
||||
} else {
|
||||
log.Errorf("could not create thumbnail: %s", err.Error())
|
||||
c.Data(400, "image/svg+xml", photoIconSvg)
|
||||
}
|
||||
case "square":
|
||||
if thumbnail, err := mediaFile.SquareThumbnail(conf.ThumbnailsPath(), size); err == nil {
|
||||
c.File(thumbnail.Filename())
|
||||
} else {
|
||||
log.Errorf("could not create square thumbnail: %s", err.Error())
|
||||
c.Data(400, "image/svg+xml", photoIconSvg)
|
||||
}
|
||||
default:
|
||||
log.Errorf("unknown thumbnail type: %s", thumbnailType)
|
||||
if thumbnail, err := photoprism.ThumbnailFromFile(fileName, file.FileHash, conf.ThumbnailsPath(), thumbType.Width, thumbType.Height, thumbType.Options...); err == nil {
|
||||
c.File(thumbnail)
|
||||
} else {
|
||||
log.Errorf("could not create thumbnail: %s", err)
|
||||
c.Data(400, "image/svg+xml", photoIconSvg)
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user