/
share_preview.go
158 lines (124 loc) Β· 4.09 KB
/
share_preview.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package api
import (
"image"
"net/http"
"os"
"path"
"path/filepath"
"time"
"github.com/disintegration/imaging"
"github.com/gin-gonic/gin"
"github.com/photoprism/photoprism/internal/entity"
"github.com/photoprism/photoprism/internal/form"
"github.com/photoprism/photoprism/internal/frame"
"github.com/photoprism/photoprism/internal/get"
"github.com/photoprism/photoprism/internal/photoprism"
"github.com/photoprism/photoprism/internal/query"
"github.com/photoprism/photoprism/internal/search"
"github.com/photoprism/photoprism/internal/thumb"
"github.com/photoprism/photoprism/pkg/clean"
"github.com/photoprism/photoprism/pkg/fs"
)
// SharePreview returns a preview image for the given share uid if the token is valid.
//
// GET /s/:token/:shared/preview
// TODO: Proof of concept, needs refactoring.
func SharePreview(router *gin.RouterGroup) {
router.GET("/:token/:shared/preview", func(c *gin.Context) {
conf := get.Config()
token := clean.Token(c.Param("token"))
shared := clean.UID(c.Param("shared"))
links := entity.FindLinks(token, shared)
if len(links) != 1 {
log.Warn("share: invalid token (preview)")
c.Redirect(http.StatusTemporaryRedirect, conf.SitePreview())
return
}
thumbPath := path.Join(conf.ThumbCachePath(), "share")
if err := fs.MkdirAll(thumbPath); err != nil {
log.Error(err)
c.Redirect(http.StatusTemporaryRedirect, conf.SitePreview())
return
}
previewFilename := filepath.Join(thumbPath, shared+fs.ExtJPEG)
expires := entity.TimeStamp().Add(-1 * time.Hour)
if info, err := os.Stat(previewFilename); err != nil {
log.Debugf("share: creating new preview for %s", clean.Log(shared))
} else if info.ModTime().After(expires) {
log.Debugf("share: using cached preview for %s", clean.Log(shared))
c.File(previewFilename)
return
} else if err := os.Remove(previewFilename); err != nil {
log.Errorf("share: could not remove old preview of %s", clean.Log(shared))
c.Redirect(http.StatusTemporaryRedirect, conf.SitePreview())
return
}
a, err := query.AlbumByUID(shared)
if err != nil {
log.Error(err)
c.Redirect(http.StatusTemporaryRedirect, conf.SitePreview())
return
}
var f form.SearchPhotos
// Covers may only contain public content in shared albums.
f.Album = shared
f.Public = true
f.Private = false
f.Hidden = false
f.Archived = false
f.Review = false
f.Primary = true
// Get first 12 album entries.
f.Count = 6
f.Order = a.AlbumOrder
if parseErr := f.ParseQueryString(); parseErr != nil {
log.Errorf("preview: %s", parseErr)
c.Redirect(http.StatusTemporaryRedirect, conf.SitePreview())
return
}
p, count, err := search.Photos(f)
if err != nil {
log.Error(err)
c.Redirect(http.StatusTemporaryRedirect, conf.SitePreview())
return
}
if count == 0 {
c.Redirect(http.StatusTemporaryRedirect, conf.SitePreview())
return
}
size, _ := thumb.Sizes[thumb.Tile500]
images := make([]image.Image, 0, len(p))
// Get thumbnail images to create album preview.
for _, file := range p {
fileName := photoprism.FileName(file.FileRoot, file.FileName)
if !fs.FileExists(fileName) {
log.Errorf("share: file %s is missing (preview)", clean.Log(file.FileName))
c.Redirect(http.StatusTemporaryRedirect, conf.SitePreview())
return
}
thumbnail, imgErr := thumb.FromFile(fileName, file.FileHash, conf.ThumbCachePath(), size.Width, size.Height, file.FileOrientation, size.Options...)
if imgErr != nil {
log.Warn(imgErr)
continue
}
img, imgErr := imaging.Open(thumbnail)
if imgErr != nil {
log.Warn(imgErr)
continue
}
images = append(images, img)
}
// Create album preview from thumbnail images.
preview, err := frame.Collage(frame.Polaroid, images)
// Downsize from 1600x900 to 1200x675.
preview = imaging.Resize(preview, 1200, 0, imaging.Lanczos)
// Save the resulting album preview as JPEG.
err = imaging.Save(preview, previewFilename, thumb.JpegQualitySmall.EncodeOption())
if err != nil {
log.Error(err)
c.Redirect(http.StatusTemporaryRedirect, conf.SitePreview())
return
}
c.File(previewFilename)
})
}