2020-06-26 14:26:36 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"image"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path"
|
2023-09-08 17:36:56 +02:00
|
|
|
"path/filepath"
|
2020-06-26 14:26:36 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/disintegration/imaging"
|
|
|
|
"github.com/gin-gonic/gin"
|
2022-09-28 09:01:17 +02:00
|
|
|
|
2020-06-26 14:26:36 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
2023-09-08 17:36:56 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/frame"
|
2022-10-15 21:54:11 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/get"
|
2020-06-26 14:26:36 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/photoprism"
|
2023-09-08 17:36:56 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/query"
|
2021-09-18 15:32:39 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/search"
|
2020-06-26 14:26:36 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/thumb"
|
2022-09-28 09:01:17 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2020-06-26 14:26:36 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
|
|
|
)
|
|
|
|
|
2023-09-08 17:36:56 +02:00
|
|
|
// SharePreview returns a preview image for the given share uid if the token is valid.
|
2021-09-05 13:48:53 +02:00
|
|
|
//
|
2023-09-08 17:36:56 +02:00
|
|
|
// GET /s/:token/:shared/preview
|
2020-06-26 14:26:36 +02:00
|
|
|
// TODO: Proof of concept, needs refactoring.
|
|
|
|
func SharePreview(router *gin.RouterGroup) {
|
2022-10-02 11:38:30 +02:00
|
|
|
router.GET("/:token/:shared/preview", func(c *gin.Context) {
|
2022-10-15 21:54:11 +02:00
|
|
|
conf := get.Config()
|
2020-06-26 14:26:36 +02:00
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
token := clean.Token(c.Param("token"))
|
2023-09-08 17:36:56 +02:00
|
|
|
shared := clean.UID(c.Param("shared"))
|
2022-10-02 11:38:30 +02:00
|
|
|
links := entity.FindLinks(token, shared)
|
2020-06-26 14:26:36 +02:00
|
|
|
|
|
|
|
if len(links) != 1 {
|
|
|
|
log.Warn("share: invalid token (preview)")
|
|
|
|
c.Redirect(http.StatusTemporaryRedirect, conf.SitePreview())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-18 13:55:17 +02:00
|
|
|
thumbPath := path.Join(conf.ThumbCachePath(), "share")
|
2020-06-26 14:26:36 +02:00
|
|
|
|
2022-10-31 15:01:48 +01:00
|
|
|
if err := os.MkdirAll(thumbPath, fs.ModeDir); err != nil {
|
2020-06-26 14:26:36 +02:00
|
|
|
log.Error(err)
|
|
|
|
c.Redirect(http.StatusTemporaryRedirect, conf.SitePreview())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-08 17:36:56 +02:00
|
|
|
previewFilename := filepath.Join(thumbPath, shared+fs.ExtJPEG)
|
|
|
|
|
|
|
|
expires := entity.TimeStamp().Add(-1 * time.Hour)
|
2020-06-26 14:26:36 +02:00
|
|
|
|
|
|
|
if info, err := os.Stat(previewFilename); err != nil {
|
2022-10-02 11:38:30 +02:00
|
|
|
log.Debugf("share: creating new preview for %s", clean.Log(shared))
|
2023-09-08 17:36:56 +02:00
|
|
|
} else if info.ModTime().After(expires) {
|
2022-10-02 11:38:30 +02:00
|
|
|
log.Debugf("share: using cached preview for %s", clean.Log(shared))
|
2020-06-26 14:26:36 +02:00
|
|
|
c.File(previewFilename)
|
|
|
|
return
|
|
|
|
} else if err := os.Remove(previewFilename); err != nil {
|
2022-10-02 11:38:30 +02:00
|
|
|
log.Errorf("share: could not remove old preview of %s", clean.Log(shared))
|
2020-06-26 14:26:36 +02:00
|
|
|
c.Redirect(http.StatusTemporaryRedirect, conf.SitePreview())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-08 17:36:56 +02:00
|
|
|
a, err := query.AlbumByUID(shared)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
c.Redirect(http.StatusTemporaryRedirect, conf.SitePreview())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-26 13:59:10 +01:00
|
|
|
var f form.SearchPhotos
|
2020-06-26 14:26:36 +02:00
|
|
|
|
2021-10-01 16:44:50 +02:00
|
|
|
// Covers may only contain public content in shared albums.
|
2022-10-02 11:38:30 +02:00
|
|
|
f.Album = shared
|
2020-06-26 14:26:36 +02:00
|
|
|
f.Public = true
|
|
|
|
f.Private = false
|
|
|
|
f.Hidden = false
|
|
|
|
f.Archived = false
|
|
|
|
f.Review = false
|
2020-06-28 15:23:15 +02:00
|
|
|
f.Primary = true
|
2020-06-26 14:26:36 +02:00
|
|
|
|
|
|
|
// Get first 12 album entries.
|
2023-09-08 17:36:56 +02:00
|
|
|
f.Count = 6
|
|
|
|
f.Order = a.AlbumOrder
|
2020-06-26 14:26:36 +02:00
|
|
|
|
2023-09-08 17:36:56 +02:00
|
|
|
if parseErr := f.ParseQueryString(); parseErr != nil {
|
|
|
|
log.Errorf("preview: %s", parseErr)
|
2022-08-01 15:57:19 +02:00
|
|
|
c.Redirect(http.StatusTemporaryRedirect, conf.SitePreview())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-18 15:32:39 +02:00
|
|
|
p, count, err := search.Photos(f)
|
2020-06-26 14:26:36 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
c.Redirect(http.StatusTemporaryRedirect, conf.SitePreview())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-28 15:23:15 +02:00
|
|
|
if count == 0 {
|
|
|
|
c.Redirect(http.StatusTemporaryRedirect, conf.SitePreview())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-08 17:36:56 +02:00
|
|
|
size, _ := thumb.Sizes[thumb.Tile500]
|
2020-06-26 14:26:36 +02:00
|
|
|
|
2023-09-08 17:36:56 +02:00
|
|
|
images := make([]image.Image, 0, len(p))
|
2020-06-26 14:26:36 +02:00
|
|
|
|
2023-09-08 17:36:56 +02:00
|
|
|
// Get thumbnail images to create album preview.
|
|
|
|
for _, file := range p {
|
|
|
|
fileName := photoprism.FileName(file.FileRoot, file.FileName)
|
2020-06-26 14:26:36 +02:00
|
|
|
|
|
|
|
if !fs.FileExists(fileName) {
|
2023-09-08 17:36:56 +02:00
|
|
|
log.Errorf("share: file %s is missing (preview)", clean.Log(file.FileName))
|
2020-06-26 14:26:36 +02:00
|
|
|
c.Redirect(http.StatusTemporaryRedirect, conf.SitePreview())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-08 17:36:56 +02:00
|
|
|
thumbnail, imgErr := thumb.FromFile(fileName, file.FileHash, conf.ThumbCachePath(), size.Width, size.Height, file.FileOrientation, size.Options...)
|
2020-06-26 14:26:36 +02:00
|
|
|
|
2023-09-08 17:36:56 +02:00
|
|
|
if imgErr != nil {
|
|
|
|
log.Warn(imgErr)
|
|
|
|
continue
|
2020-06-26 14:26:36 +02:00
|
|
|
}
|
|
|
|
|
2023-09-08 17:36:56 +02:00
|
|
|
img, imgErr := imaging.Open(thumbnail)
|
2020-06-26 14:26:36 +02:00
|
|
|
|
2023-09-08 17:36:56 +02:00
|
|
|
if imgErr != nil {
|
|
|
|
log.Warn(imgErr)
|
|
|
|
continue
|
2020-06-26 14:26:36 +02:00
|
|
|
}
|
|
|
|
|
2023-09-08 17:36:56 +02:00
|
|
|
images = append(images, img)
|
2020-06-26 14:26:36 +02:00
|
|
|
}
|
|
|
|
|
2023-09-08 17:36:56 +02:00
|
|
|
// Create album preview from thumbnail images.
|
|
|
|
preview, err := frame.Collage(frame.Polaroid, images)
|
|
|
|
|
2023-09-09 10:40:14 +02:00
|
|
|
// Downsize from 1600x900 to 1200x675.
|
|
|
|
preview = imaging.Resize(preview, 1200, 0, imaging.Lanczos)
|
|
|
|
|
2023-09-08 17:36:56 +02:00
|
|
|
// Save the resulting album preview as JPEG.
|
2023-09-09 10:40:14 +02:00
|
|
|
err = imaging.Save(preview, previewFilename, thumb.JpegQualitySmall.EncodeOption())
|
2020-06-26 14:26:36 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
c.Redirect(http.StatusTemporaryRedirect, conf.SitePreview())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.File(previewFilename)
|
|
|
|
})
|
|
|
|
}
|