2021-01-08 12:20:41 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2022-07-19 16:58:43 +02:00
|
|
|
|
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/service"
|
|
|
|
"github.com/photoprism/photoprism/internal/thumb"
|
2022-07-19 16:58:43 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2021-01-08 12:20:41 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Namespaces for caching and logs.
|
|
|
|
const (
|
|
|
|
albumCover = "album-cover"
|
|
|
|
labelCover = "label-cover"
|
|
|
|
)
|
|
|
|
|
2021-09-05 13:48:53 +02:00
|
|
|
// AlbumCover returns an album cover image.
|
|
|
|
//
|
|
|
|
// GET /api/v1/albums/:uid/t/:token/:size
|
2021-01-08 12:20:41 +01:00
|
|
|
//
|
|
|
|
// Parameters:
|
2022-08-10 16:09:21 +02:00
|
|
|
//
|
|
|
|
// uid: string album uid
|
|
|
|
// token: string security token (see config)
|
|
|
|
// size: string thumb type, see photoprism.ThumbnailTypes
|
2021-01-08 12:20:41 +01:00
|
|
|
func AlbumCover(router *gin.RouterGroup) {
|
2021-09-05 13:48:53 +02:00
|
|
|
router.GET("/albums/:uid/t/:token/:size", func(c *gin.Context) {
|
2021-01-08 12:20:41 +01:00
|
|
|
if InvalidPreviewToken(c) {
|
|
|
|
c.Data(http.StatusForbidden, "image/svg+xml", albumIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
conf := service.Config()
|
2022-04-15 09:42:07 +02:00
|
|
|
thumbName := thumb.Name(clean.Token(c.Param("size")))
|
2022-09-28 09:01:17 +02:00
|
|
|
uid := clean.UID(c.Param("uid"))
|
2021-01-08 12:20:41 +01:00
|
|
|
|
2021-09-05 13:48:53 +02:00
|
|
|
size, ok := thumb.Sizes[thumbName]
|
2021-01-08 12:20:41 +01:00
|
|
|
|
|
|
|
if !ok {
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Errorf("%s: invalid size %s", albumCover, clean.Log(thumbName.String()))
|
2021-01-08 12:20:41 +01:00
|
|
|
c.Data(http.StatusOK, "image/svg+xml", albumIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cache := service.CoverCache()
|
2021-09-05 13:48:53 +02:00
|
|
|
cacheKey := CacheKey(albumCover, uid, string(thumbName))
|
2021-01-08 12:20:41 +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
|
|
|
|
|
|
|
cached := cacheData.(ThumbCache)
|
|
|
|
|
|
|
|
if !fs.FileExists(cached.FileName) {
|
|
|
|
log.Errorf("%s: %s not found", albumCover, uid)
|
|
|
|
c.Data(http.StatusOK, "image/svg+xml", albumIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
AddCoverCacheHeader(c)
|
|
|
|
|
|
|
|
if c.Query("download") != "" {
|
|
|
|
c.FileAttachment(cached.FileName, cached.ShareName)
|
|
|
|
} else {
|
|
|
|
c.File(cached.FileName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-31 17:42:57 +02:00
|
|
|
f, err := query.AlbumCoverByUID(uid, conf.Settings().Features.Private)
|
2021-01-08 12:20:41 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2021-10-05 18:42:39 +02:00
|
|
|
log.Debugf("%s: %s contains no photos, using generic cover", albumCover, uid)
|
2021-01-08 12:20:41 +01:00
|
|
|
c.Data(http.StatusOK, "image/svg+xml", albumIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fileName := photoprism.FileName(f.FileRoot, f.FileName)
|
|
|
|
|
|
|
|
if !fs.FileExists(fileName) {
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Errorf("%s: found no original for %s", albumCover, clean.Log(fileName))
|
2021-01-08 12:20:41 +01:00
|
|
|
c.Data(http.StatusOK, "image/svg+xml", albumIconSvg)
|
|
|
|
|
|
|
|
// Set missing flag so that the file doesn't show up in search results anymore.
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Warnf("%s: %s is missing", albumCover, clean.Log(f.FileName))
|
2021-01-08 12:20:41 +01:00
|
|
|
logError(albumCover, f.Update("FileMissing", true))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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") == "" {
|
|
|
|
log.Debugf("%s: using original, size exceeds limit (width %d, height %d)", albumCover, size.Width, size.Height)
|
2021-01-08 12:20:41 +01:00
|
|
|
AddCoverCacheHeader(c)
|
|
|
|
c.File(fileName)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var thumbnail string
|
|
|
|
|
2021-09-05 12:32:08 +02:00
|
|
|
if conf.ThumbUncached() || size.Uncached() {
|
2022-04-18 13:55:17 +02:00
|
|
|
thumbnail, err = thumb.FromFile(fileName, f.FileHash, conf.ThumbCachePath(), size.Width, size.Height, f.FileOrientation, size.Options...)
|
2021-01-08 12:20:41 +01:00
|
|
|
} else {
|
2022-04-18 13:55:17 +02:00
|
|
|
thumbnail, err = thumb.FromCache(fileName, f.FileHash, conf.ThumbCachePath(), size.Width, size.Height, size.Options...)
|
2021-01-08 12:20:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2021-02-07 19:04:17 +01:00
|
|
|
log.Errorf("%s: %s", albumCover, err)
|
|
|
|
c.Data(http.StatusOK, "image/svg+xml", albumIconSvg)
|
2021-01-08 12:20:41 +01:00
|
|
|
return
|
|
|
|
} else if thumbnail == "" {
|
2022-04-06 17:46:41 +02:00
|
|
|
log.Errorf("%s: %s has empty thumb name - possible bug", albumCover, filepath.Base(fileName))
|
2021-01-08 12:20:41 +01:00
|
|
|
c.Data(http.StatusOK, "image/svg+xml", albumIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-27 21:30:10 +01:00
|
|
|
cache.SetDefault(cacheKey, ThumbCache{thumbnail, f.ShareBase(0)})
|
2021-01-08 12:20:41 +01:00
|
|
|
log.Debugf("cached %s [%s]", cacheKey, time.Since(start))
|
|
|
|
|
|
|
|
AddCoverCacheHeader(c)
|
|
|
|
|
|
|
|
if c.Query("download") != "" {
|
2021-01-27 21:30:10 +01:00
|
|
|
c.FileAttachment(thumbnail, f.DownloadName(DownloadName(c), 0))
|
2021-01-08 12:20:41 +01:00
|
|
|
} else {
|
|
|
|
c.File(thumbnail)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-05 13:48:53 +02:00
|
|
|
// LabelCover returns a label cover image.
|
|
|
|
//
|
|
|
|
// GET /api/v1/labels/:uid/t/:token/:size
|
2021-01-08 12:20:41 +01:00
|
|
|
//
|
|
|
|
// Parameters:
|
2022-08-10 16:09:21 +02:00
|
|
|
//
|
|
|
|
// uid: string label uid
|
|
|
|
// token: string security token (see config)
|
|
|
|
// size: string thumb type, see photoprism.ThumbnailTypes
|
2021-01-08 12:20:41 +01:00
|
|
|
func LabelCover(router *gin.RouterGroup) {
|
2021-09-05 13:48:53 +02:00
|
|
|
router.GET("/labels/:uid/t/:token/:size", func(c *gin.Context) {
|
2021-01-08 12:20:41 +01:00
|
|
|
if InvalidPreviewToken(c) {
|
|
|
|
c.Data(http.StatusForbidden, "image/svg+xml", labelIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
conf := service.Config()
|
2022-04-15 09:42:07 +02:00
|
|
|
thumbName := thumb.Name(clean.Token(c.Param("size")))
|
2022-09-28 09:01:17 +02:00
|
|
|
uid := clean.UID(c.Param("uid"))
|
2021-01-08 12:20:41 +01:00
|
|
|
|
2021-09-05 13:48:53 +02:00
|
|
|
size, ok := thumb.Sizes[thumbName]
|
2021-01-08 12:20:41 +01:00
|
|
|
|
|
|
|
if !ok {
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Errorf("%s: invalid size %s", labelCover, clean.Log(thumbName.String()))
|
2021-01-08 12:20:41 +01:00
|
|
|
c.Data(http.StatusOK, "image/svg+xml", labelIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cache := service.CoverCache()
|
2021-09-05 13:48:53 +02:00
|
|
|
cacheKey := CacheKey(labelCover, uid, string(thumbName))
|
2021-01-08 12:20:41 +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
|
|
|
|
|
|
|
cached := cacheData.(ThumbCache)
|
|
|
|
|
|
|
|
if !fs.FileExists(cached.FileName) {
|
|
|
|
log.Errorf("%s: %s not found", labelCover, uid)
|
|
|
|
c.Data(http.StatusOK, "image/svg+xml", labelIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
AddCoverCacheHeader(c)
|
|
|
|
|
|
|
|
if c.Query("download") != "" {
|
|
|
|
c.FileAttachment(cached.FileName, cached.ShareName)
|
|
|
|
} else {
|
|
|
|
c.File(cached.FileName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := query.LabelThumbByUID(uid)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf(err.Error())
|
|
|
|
c.Data(http.StatusOK, "image/svg+xml", labelIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fileName := photoprism.FileName(f.FileRoot, f.FileName)
|
|
|
|
|
|
|
|
if !fs.FileExists(fileName) {
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Errorf("%s: file %s is missing", labelCover, clean.Log(f.FileName))
|
2021-01-08 12:20:41 +01:00
|
|
|
c.Data(http.StatusOK, "image/svg+xml", labelIconSvg)
|
|
|
|
|
|
|
|
// Set missing flag so that the file doesn't show up in search results anymore.
|
|
|
|
logError(labelCover, f.Update("FileMissing", true))
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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() {
|
|
|
|
log.Debugf("%s: using original, size exceeds limit (width %d, height %d)", labelCover, size.Width, size.Height)
|
2021-01-08 12:20:41 +01:00
|
|
|
|
|
|
|
AddCoverCacheHeader(c)
|
|
|
|
c.File(fileName)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var thumbnail string
|
|
|
|
|
2021-09-05 12:32:08 +02:00
|
|
|
if conf.ThumbUncached() || size.Uncached() {
|
2022-04-18 13:55:17 +02:00
|
|
|
thumbnail, err = thumb.FromFile(fileName, f.FileHash, conf.ThumbCachePath(), size.Width, size.Height, f.FileOrientation, size.Options...)
|
2021-01-08 12:20:41 +01:00
|
|
|
} else {
|
2022-04-18 13:55:17 +02:00
|
|
|
thumbnail, err = thumb.FromCache(fileName, f.FileHash, conf.ThumbCachePath(), size.Width, size.Height, size.Options...)
|
2021-01-08 12:20:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("%s: %s", labelCover, err)
|
|
|
|
c.Data(http.StatusOK, "image/svg+xml", labelIconSvg)
|
|
|
|
return
|
|
|
|
} else if thumbnail == "" {
|
2022-04-06 17:46:41 +02:00
|
|
|
log.Errorf("%s: %s has empty thumb name - possible bug", labelCover, filepath.Base(fileName))
|
2021-01-08 12:20:41 +01:00
|
|
|
c.Data(http.StatusOK, "image/svg+xml", labelIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-27 21:30:10 +01:00
|
|
|
cache.SetDefault(cacheKey, ThumbCache{thumbnail, f.ShareBase(0)})
|
2021-01-08 12:20:41 +01:00
|
|
|
log.Debugf("cached %s [%s]", cacheKey, time.Since(start))
|
|
|
|
|
|
|
|
AddCoverCacheHeader(c)
|
|
|
|
|
|
|
|
if c.Query("download") != "" {
|
2021-01-27 21:30:10 +01:00
|
|
|
c.FileAttachment(thumbnail, f.DownloadName(DownloadName(c), 0))
|
2021-01-08 12:20:41 +01:00
|
|
|
} else {
|
|
|
|
c.File(thumbnail)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|