diff --git a/internal/api/share_preview.go b/internal/api/share_preview.go index 04e718d85..42d244fc2 100644 --- a/internal/api/share_preview.go +++ b/internal/api/share_preview.go @@ -69,13 +69,13 @@ func SharePreview(router *gin.RouterGroup) { f.Hidden = false f.Archived = false f.Review = false - f.Merged = true + f.Primary = true // Get first 12 album entries. f.Count = 12 f.Order = "relevance" - p, _, err := query.PhotoSearch(f) + p, count, err := query.PhotoSearch(f) if err != nil { log.Error(err) @@ -83,6 +83,34 @@ func SharePreview(router *gin.RouterGroup) { return } + if count == 0 { + c.Redirect(http.StatusTemporaryRedirect, conf.SitePreview()) + return + } else if count < 12 { + f := p[0] + thumbType, _ := thumb.Types["fit_720"] + + fileName := photoprism.FileName(f.FileRoot, f.FileName) + + if !fs.FileExists(fileName) { + log.Errorf("share: file %s is missing (preview)", txt.Quote(f.FileName)) + c.Redirect(http.StatusTemporaryRedirect, conf.SitePreview()) + return + } + + thumbnail, err := thumb.FromFile(fileName, f.FileHash, conf.ThumbPath(), thumbType.Width, thumbType.Height, thumbType.Options...) + + if err != nil { + log.Error(err) + c.Redirect(http.StatusTemporaryRedirect, conf.SitePreview()) + return + } + + c.File(thumbnail) + + return + } + width := 908 height := 680 x := 0 @@ -105,6 +133,7 @@ func SharePreview(router *gin.RouterGroup) { if err != nil { log.Error(err) c.Redirect(http.StatusTemporaryRedirect, conf.SitePreview()) + return } src, err := imaging.Open(thumbnail) diff --git a/internal/form/photo_search.go b/internal/form/photo_search.go index f6fd7d67d..85ed87004 100644 --- a/internal/form/photo_search.go +++ b/internal/form/photo_search.go @@ -16,6 +16,7 @@ type PhotoSearch struct { Original string `form:"original"` Title string `form:"title"` Hash string `form:"hash"` + Primary bool `form:"primary"` Video bool `form:"video"` Photo bool `form:"photo"` Duplicate bool `form:"duplicate"` diff --git a/internal/query/photo_search.go b/internal/query/photo_search.go index b158d86a7..f39da3e20 100644 --- a/internal/query/photo_search.go +++ b/internal/query/photo_search.go @@ -49,6 +49,11 @@ func PhotoSearch(f form.PhotoSearch) (results PhotoResults, count int, err error } } + // Return primary files only. + if f.Primary { + s = s.Where("files.file_primary = 1") + } + // Shortcut for known photo ids. if f.ID != "" { s = s.Where("photos.photo_uid IN (?)", strings.Split(f.ID, ","))