2021-01-08 12:20:41 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-09-08 17:36:56 +02:00
|
|
|
"os"
|
|
|
|
"path"
|
2021-01-08 12:20:41 +01:00
|
|
|
|
2023-03-20 11:40:46 +01:00
|
|
|
"github.com/gin-gonic/gin"
|
2021-08-30 18:58:27 +02:00
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/get"
|
2023-03-20 11:40:46 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/query"
|
2021-01-08 12:20:41 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/thumb"
|
2023-08-15 11:06:43 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/ttl"
|
2023-09-08 17:36:56 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
|
|
|
"github.com/photoprism/photoprism/pkg/rnd"
|
2021-01-08 12:20:41 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type ThumbCache struct {
|
|
|
|
FileName string
|
|
|
|
ShareName string
|
|
|
|
}
|
|
|
|
|
|
|
|
type ByteCache struct {
|
|
|
|
Data []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// CacheKey returns a cache key string based on namespace, uid and name.
|
|
|
|
func CacheKey(ns, uid, name string) string {
|
|
|
|
return fmt.Sprintf("%s:%s:%s", ns, uid, name)
|
|
|
|
}
|
|
|
|
|
2021-01-08 13:29:01 +01:00
|
|
|
// RemoveFromFolderCache removes an item from the folder cache e.g. after indexing.
|
|
|
|
func RemoveFromFolderCache(rootName string) {
|
2022-10-15 21:54:11 +02:00
|
|
|
cache := get.FolderCache()
|
2021-01-08 13:29:01 +01:00
|
|
|
|
|
|
|
cacheKey := fmt.Sprintf("folder:%s:%t:%t", rootName, true, false)
|
|
|
|
|
|
|
|
cache.Delete(cacheKey)
|
|
|
|
|
2021-10-01 16:44:50 +02:00
|
|
|
if err := query.UpdateAlbumFolderCovers(); err != nil {
|
2021-09-23 23:46:17 +02:00
|
|
|
log.Error(err)
|
2021-08-30 18:58:27 +02:00
|
|
|
}
|
|
|
|
|
2021-01-08 13:29:01 +01:00
|
|
|
log.Debugf("removed %s from cache", cacheKey)
|
|
|
|
}
|
|
|
|
|
2021-01-08 12:20:41 +01:00
|
|
|
// RemoveFromAlbumCoverCache removes covers by album UID e.g. after adding or removing photos.
|
|
|
|
func RemoveFromAlbumCoverCache(uid string) {
|
2023-09-08 17:36:56 +02:00
|
|
|
if !rnd.IsAlnum(uid) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
cache := get.CoverCache()
|
2021-01-08 12:20:41 +01:00
|
|
|
|
2023-09-08 17:36:56 +02:00
|
|
|
// Flush album cover cache.
|
2021-09-05 13:48:53 +02:00
|
|
|
for thumbName := range thumb.Sizes {
|
|
|
|
cacheKey := CacheKey(albumCover, uid, string(thumbName))
|
2021-01-08 12:20:41 +01:00
|
|
|
|
|
|
|
cache.Delete(cacheKey)
|
|
|
|
|
|
|
|
log.Debugf("removed %s from cache", cacheKey)
|
|
|
|
}
|
2021-08-30 18:58:27 +02:00
|
|
|
|
2023-09-08 17:36:56 +02:00
|
|
|
// Delete share preview, if exists.
|
|
|
|
if sharePreview := path.Join(get.Config().ThumbCachePath(), "share", uid+fs.ExtJPEG); fs.FileExists(sharePreview) {
|
|
|
|
_ = os.Remove(sharePreview)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update album cover images.
|
2021-10-01 16:44:50 +02:00
|
|
|
if err := query.UpdateAlbumCovers(); err != nil {
|
2021-09-23 23:46:17 +02:00
|
|
|
log.Error(err)
|
2021-08-30 18:58:27 +02:00
|
|
|
}
|
2021-01-08 12:20:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// FlushCoverCache clears the complete cover cache.
|
|
|
|
func FlushCoverCache() {
|
2022-10-15 21:54:11 +02:00
|
|
|
get.CoverCache().Flush()
|
2021-01-08 12:20:41 +01:00
|
|
|
|
2021-10-01 16:44:50 +02:00
|
|
|
if err := query.UpdateCovers(); err != nil {
|
2021-09-23 23:46:17 +02:00
|
|
|
log.Error(err)
|
2021-08-30 18:58:27 +02:00
|
|
|
}
|
|
|
|
|
2021-01-08 12:20:41 +01:00
|
|
|
log.Debugf("albums: flushed cover cache")
|
|
|
|
}
|
2023-03-20 11:40:46 +01:00
|
|
|
|
|
|
|
// AddCacheHeader adds a cache control header to the response.
|
2023-08-15 11:06:43 +02:00
|
|
|
func AddCacheHeader(c *gin.Context, maxAge ttl.Duration, public bool) {
|
|
|
|
if c == nil {
|
|
|
|
return
|
|
|
|
} else if maxAge <= 0 {
|
|
|
|
c.Header("Cache-Control", "no-cache")
|
|
|
|
} else if public {
|
|
|
|
c.Header("Cache-Control", fmt.Sprintf("public, max-age=%s", maxAge.String()))
|
2023-03-20 11:40:46 +01:00
|
|
|
} else {
|
2023-08-15 11:06:43 +02:00
|
|
|
c.Header("Cache-Control", fmt.Sprintf("private, max-age=%s", maxAge.String()))
|
2023-03-20 11:40:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddCoverCacheHeader adds cover image cache control headers to the response.
|
|
|
|
func AddCoverCacheHeader(c *gin.Context) {
|
2023-08-15 11:06:43 +02:00
|
|
|
AddCacheHeader(c, ttl.Cover, thumb.CachePublic)
|
2023-03-20 11:40:46 +01:00
|
|
|
}
|
|
|
|
|
2023-04-14 14:46:56 +02:00
|
|
|
// AddImmutableCacheHeader adds cache control headers to the response for immutable content like thumbnails.
|
|
|
|
func AddImmutableCacheHeader(c *gin.Context) {
|
2023-08-15 11:06:43 +02:00
|
|
|
if c == nil {
|
|
|
|
return
|
|
|
|
} else if thumb.CachePublic {
|
|
|
|
c.Header("Cache-Control", fmt.Sprintf("public, max-age=%s, immutable", ttl.Default.String()))
|
|
|
|
} else {
|
|
|
|
c.Header("Cache-Control", fmt.Sprintf("private, max-age=%s, immutable", ttl.Default.String()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddVideoCacheHeader adds video cache control headers to the response.
|
|
|
|
func AddVideoCacheHeader(c *gin.Context, cdn bool) {
|
|
|
|
if c == nil {
|
|
|
|
return
|
|
|
|
} else if cdn || thumb.CachePublic {
|
|
|
|
c.Header("Cache-Control", fmt.Sprintf("public, max-age=%s, immutable", ttl.Video.String()))
|
2023-03-20 11:40:46 +01:00
|
|
|
} else {
|
2023-08-15 11:06:43 +02:00
|
|
|
c.Header("Cache-Control", fmt.Sprintf("private, max-age=%s, immutable", ttl.Video.String()))
|
2023-03-20 11:40:46 +01:00
|
|
|
}
|
|
|
|
}
|