2021-02-07 19:04:17 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
2021-12-14 18:34:52 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/sanitize"
|
|
|
|
|
2021-02-07 19:04:17 +01:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/photoprism/photoprism/internal/photoprism"
|
|
|
|
"github.com/photoprism/photoprism/internal/query"
|
|
|
|
"github.com/photoprism/photoprism/internal/service"
|
|
|
|
"github.com/photoprism/photoprism/internal/thumb"
|
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
folderCover = "folder-cover"
|
|
|
|
)
|
|
|
|
|
2021-09-05 13:48:53 +02:00
|
|
|
// FolderCover returns a folder cover image.
|
|
|
|
//
|
|
|
|
// GET /api/v1/folders/t/:hash/:token/:size
|
2021-02-07 19:04:17 +01:00
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// uid: string folder uid
|
|
|
|
// token: string url security token, see config
|
2021-09-05 13:48:53 +02:00
|
|
|
// size: string thumb type, see thumb.Sizes
|
|
|
|
func FolderCover(router *gin.RouterGroup) {
|
|
|
|
router.GET("/folders/t/:uid/:token/:size", func(c *gin.Context) {
|
2021-02-07 19:04:17 +01:00
|
|
|
if InvalidPreviewToken(c) {
|
|
|
|
c.Data(http.StatusForbidden, "image/svg+xml", folderIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
conf := service.Config()
|
|
|
|
uid := c.Param("uid")
|
2021-12-14 18:34:52 +01:00
|
|
|
thumbName := thumb.Name(sanitize.Token(c.Param("size")))
|
2021-02-07 19:04:17 +01:00
|
|
|
download := c.Query("download") != ""
|
|
|
|
|
2021-09-05 13:48:53 +02:00
|
|
|
size, ok := thumb.Sizes[thumbName]
|
2021-02-07 19:04:17 +01:00
|
|
|
|
|
|
|
if !ok {
|
2021-09-05 13:48:53 +02:00
|
|
|
log.Errorf("%s: invalid size %s", folderCover, thumbName)
|
2021-02-07 19:04:17 +01:00
|
|
|
c.Data(http.StatusOK, "image/svg+xml", folderIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-05 12:32:08 +02:00
|
|
|
if size.Uncached() && !conf.ThumbUncached() {
|
2021-09-05 13:48:53 +02:00
|
|
|
thumbName, size = thumb.Find(conf.ThumbSizePrecached())
|
2021-02-07 19:04:17 +01:00
|
|
|
|
2021-09-05 13:48:53 +02:00
|
|
|
if thumbName == "" {
|
2021-09-05 12:32:08 +02:00
|
|
|
log.Errorf("folder: invalid thumb size %d", conf.ThumbSizePrecached())
|
2021-02-07 19:04:17 +01:00
|
|
|
c.Data(http.StatusOK, "image/svg+xml", folderIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cache := service.CoverCache()
|
2021-09-05 13:48:53 +02:00
|
|
|
cacheKey := CacheKey(folderCover, uid, string(thumbName))
|
2021-02-07 19:04:17 +01:00
|
|
|
|
|
|
|
if cacheData, ok := cache.Get(cacheKey); ok {
|
2021-11-27 18:41:10 +01:00
|
|
|
log.Tracef("api: cache hit for %s [%s]", cacheKey, time.Since(start))
|
2021-02-07 19:04:17 +01:00
|
|
|
|
|
|
|
cached := cacheData.(ThumbCache)
|
|
|
|
|
|
|
|
if !fs.FileExists(cached.FileName) {
|
|
|
|
log.Errorf("%s: %s not found", folderCover, uid)
|
|
|
|
c.Data(http.StatusOK, "image/svg+xml", folderIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
AddCoverCacheHeader(c)
|
|
|
|
|
|
|
|
if download {
|
|
|
|
c.FileAttachment(cached.FileName, cached.ShareName)
|
|
|
|
} else {
|
|
|
|
c.File(cached.FileName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := query.FolderCoverByUID(uid)
|
|
|
|
|
|
|
|
if err != nil {
|
2021-10-05 18:42:39 +02:00
|
|
|
log.Debugf("%s: %s contains no photos, using generic cover", folderCover, uid)
|
2021-02-07 19:04:17 +01:00
|
|
|
c.Data(http.StatusOK, "image/svg+xml", folderIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fileName := photoprism.FileName(f.FileRoot, f.FileName)
|
|
|
|
|
|
|
|
if !fs.FileExists(fileName) {
|
|
|
|
log.Errorf("%s: could not find original for %s", folderCover, fileName)
|
|
|
|
c.Data(http.StatusOK, "image/svg+xml", folderIconSvg)
|
|
|
|
|
|
|
|
// Set missing flag so that the file doesn't show up in search results anymore.
|
2021-12-14 20:01:39 +01:00
|
|
|
log.Warnf("%s: %s is missing", folderCover, sanitize.Log(f.FileName))
|
2021-02-07 19:04:17 +01:00
|
|
|
logError(folderCover, 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() && !download {
|
|
|
|
log.Debugf("%s: using original, size exceeds limit (width %d, height %d)", folderCover, size.Width, size.Height)
|
2021-02-07 19:04:17 +01:00
|
|
|
AddCoverCacheHeader(c)
|
|
|
|
c.File(fileName)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var thumbnail string
|
|
|
|
|
2021-09-05 12:32:08 +02:00
|
|
|
if conf.ThumbUncached() || size.Uncached() {
|
|
|
|
thumbnail, err = thumb.FromFile(fileName, f.FileHash, conf.ThumbPath(), size.Width, size.Height, f.FileOrientation, size.Options...)
|
2021-02-07 19:04:17 +01:00
|
|
|
} else {
|
2021-09-05 12:32:08 +02:00
|
|
|
thumbnail, err = thumb.FromCache(fileName, f.FileHash, conf.ThumbPath(), size.Width, size.Height, size.Options...)
|
2021-02-07 19:04:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("%s: %s", folderCover, err)
|
|
|
|
c.Data(http.StatusOK, "image/svg+xml", folderIconSvg)
|
|
|
|
return
|
|
|
|
} else if thumbnail == "" {
|
|
|
|
log.Errorf("%s: %s has empty thumb name - bug?", folderCover, filepath.Base(fileName))
|
|
|
|
c.Data(http.StatusOK, "image/svg+xml", folderIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cache.SetDefault(cacheKey, ThumbCache{thumbnail, f.ShareBase(0)})
|
|
|
|
log.Debugf("cached %s [%s]", cacheKey, time.Since(start))
|
|
|
|
|
|
|
|
AddCoverCacheHeader(c)
|
|
|
|
|
|
|
|
if download {
|
|
|
|
c.FileAttachment(thumbnail, f.DownloadName(DownloadName(c), 0))
|
|
|
|
} else {
|
|
|
|
c.File(thumbnail)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|