2021-01-08 12:20:41 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2021-09-05 11:05:33 +02:00
|
|
|
|
2021-12-14 18:34:52 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/crop"
|
2022-10-15 21:54:11 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/get"
|
2021-01-08 12:20:41 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/photoprism"
|
|
|
|
"github.com/photoprism/photoprism/internal/query"
|
|
|
|
"github.com/photoprism/photoprism/internal/thumb"
|
2022-04-15 09:42:07 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2021-01-08 12:20:41 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
|
|
|
)
|
|
|
|
|
2021-09-20 16:17:10 +02:00
|
|
|
// GetThumb returns a thumbnail image matching the file hash, crop area, and type.
|
2021-09-02 23:47:37 +02:00
|
|
|
//
|
2021-09-20 16:17:10 +02:00
|
|
|
// GET /api/v1/t/:thumb/:token/:size
|
2021-01-08 12:20:41 +01:00
|
|
|
//
|
|
|
|
// Parameters:
|
2022-08-10 16:09:21 +02:00
|
|
|
//
|
|
|
|
// thumb: string sha1 file hash plus optional crop area
|
|
|
|
// token: string url security token, see config
|
|
|
|
// size: string thumb type, see thumb.Sizes
|
2021-01-08 12:20:41 +01:00
|
|
|
func GetThumb(router *gin.RouterGroup) {
|
2021-09-20 16:17:10 +02:00
|
|
|
router.GET("/t/:thumb/:token/:size", func(c *gin.Context) {
|
2021-01-08 12:20:41 +01:00
|
|
|
if InvalidPreviewToken(c) {
|
|
|
|
c.Data(http.StatusForbidden, "image/svg+xml", brokenIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-05 17:10:52 +02:00
|
|
|
logPrefix := "thumb"
|
|
|
|
|
2021-01-08 12:20:41 +01:00
|
|
|
start := time.Now()
|
2022-10-15 21:54:11 +02:00
|
|
|
conf := get.Config()
|
2021-01-08 12:52:26 +01:00
|
|
|
download := c.Query("download") != ""
|
2022-04-15 09:42:07 +02:00
|
|
|
fileHash, cropArea := crop.ParseThumb(clean.Token(c.Param("thumb")))
|
2021-09-20 16:17:10 +02:00
|
|
|
|
|
|
|
// Is cropped thumbnail?
|
|
|
|
if cropArea != "" {
|
2022-04-15 09:42:07 +02:00
|
|
|
cropName := crop.Name(clean.Token(c.Param("size")))
|
2021-09-20 16:17:10 +02:00
|
|
|
|
|
|
|
cropSize, ok := crop.Sizes[cropName]
|
|
|
|
|
|
|
|
if !ok {
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Errorf("%s: invalid size %s", logPrefix, clean.Log(string(cropName)))
|
2021-09-20 16:17:10 +02:00
|
|
|
c.Data(http.StatusOK, "image/svg+xml", photoIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-18 13:55:17 +02:00
|
|
|
fileName, err := crop.FromRequest(fileHash, cropArea, cropSize, conf.ThumbCachePath())
|
2021-09-20 16:17:10 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("%s: %s", logPrefix, err)
|
|
|
|
c.Data(http.StatusOK, "image/svg+xml", brokenIconSvg)
|
|
|
|
return
|
|
|
|
} else if fileName == "" {
|
|
|
|
log.Errorf("%s: empty file name, potential bug", logPrefix)
|
|
|
|
c.Data(http.StatusOK, "image/svg+xml", brokenIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if download {
|
|
|
|
c.FileAttachment(fileName, cropName.Jpeg())
|
|
|
|
} else {
|
2022-02-20 17:36:51 +01:00
|
|
|
AddThumbCacheHeader(c)
|
2021-09-20 16:17:10 +02:00
|
|
|
c.File(fileName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-06 23:01:54 +02:00
|
|
|
sizeName := thumb.Name(clean.Token(c.Param("size")))
|
2021-01-08 12:20:41 +01:00
|
|
|
|
2022-07-06 23:01:54 +02:00
|
|
|
size, ok := thumb.Sizes[sizeName]
|
2021-01-08 12:20:41 +01:00
|
|
|
|
|
|
|
if !ok {
|
2022-07-06 23:01:54 +02:00
|
|
|
log.Errorf("%s: invalid size %s", logPrefix, clean.Log(sizeName.String()))
|
2021-01-08 12:20:41 +01:00
|
|
|
c.Data(http.StatusOK, "image/svg+xml", photoIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-05 12:32:08 +02:00
|
|
|
if size.Uncached() && !conf.ThumbUncached() {
|
2022-07-06 23:01:54 +02:00
|
|
|
sizeName, size = thumb.Find(conf.ThumbSizePrecached())
|
2021-01-08 12:20:41 +01:00
|
|
|
|
2022-07-06 23:01:54 +02:00
|
|
|
if sizeName == "" {
|
2021-09-05 17:10:52 +02:00
|
|
|
log.Errorf("%s: invalid size %d", logPrefix, conf.ThumbSizePrecached())
|
2021-01-08 12:20:41 +01:00
|
|
|
c.Data(http.StatusOK, "image/svg+xml", photoIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
cache := get.ThumbCache()
|
2022-07-06 23:01:54 +02:00
|
|
|
cacheKey := CacheKey("thumbs", fileHash, string(sizeName))
|
2021-01-08 12:20:41 +01:00
|
|
|
|
2021-01-08 12:52:26 +01:00
|
|
|
if cacheData, ok := cache.Get(cacheKey); ok {
|
2022-07-19 16:58:43 +02:00
|
|
|
log.Tracef("api-v1: cache hit for %s [%s]", cacheKey, time.Since(start))
|
2021-01-08 12:20:41 +01:00
|
|
|
|
2021-01-08 12:52:26 +01:00
|
|
|
cached := cacheData.(ThumbCache)
|
2021-01-08 12:20:41 +01:00
|
|
|
|
|
|
|
if !fs.FileExists(cached.FileName) {
|
2021-09-05 17:10:52 +02:00
|
|
|
log.Errorf("%s: %s not found", logPrefix, fileHash)
|
2021-01-08 12:20:41 +01:00
|
|
|
c.Data(http.StatusOK, "image/svg+xml", brokenIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Query("download") != "" {
|
|
|
|
c.FileAttachment(cached.FileName, cached.ShareName)
|
|
|
|
} else {
|
2022-02-20 17:36:51 +01:00
|
|
|
AddThumbCacheHeader(c)
|
2021-01-08 12:20:41 +01:00
|
|
|
c.File(cached.FileName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-08 12:52:26 +01:00
|
|
|
// Return existing thumbs straight away.
|
|
|
|
if !download {
|
2022-07-06 23:01:54 +02:00
|
|
|
if fileName, err := size.ResolvedName(fileHash, conf.ThumbCachePath()); err == nil {
|
2022-02-20 17:36:51 +01:00
|
|
|
AddThumbCacheHeader(c)
|
2021-01-08 12:52:26 +01:00
|
|
|
c.File(fileName)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query index for file infos.
|
2021-01-08 12:20:41 +01:00
|
|
|
f, err := query.FileByHash(fileHash)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.Data(http.StatusOK, "image/svg+xml", photoIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-11 20:18:04 +01:00
|
|
|
// Find supported preview image if media file is not a JPEG or PNG.
|
|
|
|
if f.NoJPEG() && f.NoPNG() {
|
2021-01-08 12:20:41 +01:00
|
|
|
f, err = query.FileByPhotoUID(f.PhotoUID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.Data(http.StatusOK, "image/svg+xml", fileIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return SVG icon as placeholder if file has errors.
|
|
|
|
if f.FileError != "" {
|
|
|
|
c.Data(http.StatusOK, "image/svg+xml", brokenIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fileName := photoprism.FileName(f.FileRoot, f.FileName)
|
|
|
|
|
2022-07-06 23:01:54 +02:00
|
|
|
if fileName, err = fs.Resolve(fileName); err != nil {
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Errorf("%s: file %s is missing", logPrefix, clean.Log(f.FileName))
|
2021-01-08 12:20:41 +01:00
|
|
|
c.Data(http.StatusOK, "image/svg+xml", brokenIconSvg)
|
|
|
|
|
|
|
|
// Set missing flag so that the file doesn't show up in search results anymore.
|
2021-09-05 17:10:52 +02:00
|
|
|
logError(logPrefix, f.Update("FileMissing", true))
|
2021-01-08 12:20:41 +01:00
|
|
|
|
|
|
|
if f.AllFilesMissing() {
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Infof("%s: deleting photo, all files missing for %s", logPrefix, clean.Log(f.FileName))
|
2021-01-08 12:20:41 +01:00
|
|
|
|
2021-09-24 13:13:59 +02:00
|
|
|
if _, err := f.RelatedPhoto().Delete(false); err != nil {
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Errorf("%s: %s while deleting %s", logPrefix, err, clean.Log(f.FileName))
|
2021-09-24 13:13:59 +02:00
|
|
|
}
|
2021-01-08 12:20:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-06 23:01:54 +02:00
|
|
|
// Choose the smallest fitting size if the original image is smaller.
|
|
|
|
if size.Fit && f.Bounds().In(size.Bounds()) {
|
|
|
|
size = thumb.FitBounds(f.Bounds())
|
|
|
|
log.Tracef("%s: smallest fitting size for %s is %s (width %d, height %d)", logPrefix, clean.Log(f.FileName), size.Name, size.Width, size.Height)
|
|
|
|
}
|
|
|
|
|
2021-01-08 12:20:41 +01:00
|
|
|
// Use original file if thumb size exceeds limit, see https://github.com/photoprism/photoprism/issues/157
|
2021-09-05 12:32:08 +02:00
|
|
|
if size.ExceedsLimit() && c.Query("download") == "" {
|
2021-09-05 17:10:52 +02:00
|
|
|
log.Debugf("%s: using original, size exceeds limit (width %d, height %d)", logPrefix, size.Width, size.Height)
|
2021-01-08 12:20:41 +01:00
|
|
|
|
|
|
|
AddThumbCacheHeader(c)
|
|
|
|
c.File(fileName)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-06 23:01:54 +02:00
|
|
|
// thumbName is the thumbnail filename.
|
|
|
|
var thumbName string
|
2021-01-08 12:20:41 +01:00
|
|
|
|
2022-07-06 23:01:54 +02:00
|
|
|
// Try to find or create thumbnail image.
|
2021-09-05 12:32:08 +02:00
|
|
|
if conf.ThumbUncached() || size.Uncached() {
|
2022-07-06 23:01:54 +02:00
|
|
|
thumbName, err = size.FromFile(fileName, f.FileHash, conf.ThumbCachePath(), f.FileOrientation)
|
2021-01-08 12:20:41 +01:00
|
|
|
} else {
|
2022-07-06 23:01:54 +02:00
|
|
|
thumbName, err = size.FromCache(fileName, f.FileHash, conf.ThumbCachePath())
|
2021-01-08 12:20:41 +01:00
|
|
|
}
|
|
|
|
|
2022-10-31 13:25:02 +01:00
|
|
|
// Failed?
|
2021-01-08 12:20:41 +01:00
|
|
|
if err != nil {
|
2021-09-05 17:10:52 +02:00
|
|
|
log.Errorf("%s: %s", logPrefix, err)
|
2021-01-08 12:20:41 +01:00
|
|
|
c.Data(http.StatusOK, "image/svg+xml", brokenIconSvg)
|
|
|
|
return
|
2022-07-06 23:01:54 +02:00
|
|
|
} else if thumbName == "" {
|
2022-04-06 17:46:41 +02:00
|
|
|
log.Errorf("%s: %s has empty thumb name - possible bug", logPrefix, filepath.Base(fileName))
|
2021-01-08 12:20:41 +01:00
|
|
|
c.Data(http.StatusOK, "image/svg+xml", brokenIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-06 23:01:54 +02:00
|
|
|
// Cache thumbnail filename to reduce the number of index queries.
|
|
|
|
cache.SetDefault(cacheKey, ThumbCache{thumbName, f.ShareBase(0)})
|
2021-01-08 12:52:26 +01:00
|
|
|
log.Debugf("cached %s [%s]", cacheKey, time.Since(start))
|
2021-01-08 12:20:41 +01:00
|
|
|
|
2022-07-06 23:01:54 +02:00
|
|
|
// Set the download or cache header and return the thumbnail.
|
2021-01-08 12:52:26 +01:00
|
|
|
if download {
|
2022-07-06 23:01:54 +02:00
|
|
|
c.FileAttachment(thumbName, f.DownloadName(DownloadName(c), 0))
|
2021-01-08 12:20:41 +01:00
|
|
|
} else {
|
2022-02-20 17:36:51 +01:00
|
|
|
AddThumbCacheHeader(c)
|
2022-07-06 23:01:54 +02:00
|
|
|
c.File(thumbName)
|
2021-01-08 12:20:41 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|