photoprism/internal/api/photo_thumbnail.go
Michael Mayer abfd2fa79d Videos: Improve indexing and searching #312
Signed-off-by: Michael Mayer <michael@liquidbytes.net>
2020-05-16 17:07:44 +02:00

104 lines
2.8 KiB
Go

package api
import (
"net/http"
"path"
"github.com/gin-gonic/gin"
"github.com/photoprism/photoprism/internal/config"
"github.com/photoprism/photoprism/internal/query"
"github.com/photoprism/photoprism/internal/thumb"
"github.com/photoprism/photoprism/pkg/fs"
"github.com/photoprism/photoprism/pkg/txt"
)
// GET /api/v1/thumbnails/:hash/:type
//
// Parameters:
// 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/:hash/:type", func(c *gin.Context) {
fileHash := c.Param("hash")
typeName := c.Param("type")
thumbType, ok := thumb.Types[typeName]
if !ok {
log.Errorf("photo: invalid thumb type %s", txt.Quote(typeName))
c.Data(http.StatusOK, "image/svg+xml", photoIconSvg)
return
}
f, err := query.FileByHash(fileHash)
if err != nil {
c.Data(http.StatusOK, "image/svg+xml", photoIconSvg)
return
}
if f.FileVideo {
f, err = query.FileByPhotoUUID(f.PhotoUUID)
if err != nil {
c.Data(http.StatusOK, "image/svg+xml", videoIconSvg)
return
}
}
if f.FileError != "" {
c.Data(http.StatusOK, "image/svg+xml", brokenIconSvg)
return
}
fileName := path.Join(conf.OriginalsPath(), f.FileName)
if !fs.FileExists(fileName) {
log.Errorf("photo: could not find original for %s", txt.Quote(f.FileName))
c.Data(http.StatusOK, "image/svg+xml", photoIconSvg)
// Set missing flag so that the file doesn't show up in search results anymore
f.FileMissing = true
if err := f.Save(); err != nil {
log.Errorf("photo: %s", err)
} else if f.AllFilesMissing() {
log.Infof("photo: deleting photo, all files missing for %s", txt.Quote(f.FileName))
if err := f.Photo.Delete(false); err != nil {
log.Errorf("photo: %s", err)
}
}
return
}
// Use original file if thumb size exceeds limit, see https://github.com/photoprism/photoprism/issues/157
if thumbType.ExceedsLimit() && c.Query("download") == "" {
log.Debugf("photo: using original, thumbnail size exceeds limit (width %d, height %d)", thumbType.Width, thumbType.Height)
c.File(fileName)
return
}
var thumbnail string
if conf.ThumbUncached() || thumbType.OnDemand() {
thumbnail, err = thumb.FromFile(fileName, f.FileHash, conf.ThumbPath(), thumbType.Width, thumbType.Height, thumbType.Options...)
} else {
thumbnail, err = thumb.FromCache(fileName, f.FileHash, conf.ThumbPath(), thumbType.Width, thumbType.Height, thumbType.Options...)
}
if err != nil {
log.Errorf("photo: %s", err)
c.Data(http.StatusOK, "image/svg+xml", photoIconSvg)
return
}
if c.Query("download") != "" {
c.FileAttachment(thumbnail, f.ShareFileName())
} else {
c.File(thumbnail)
}
})
}