/
main.go
283 lines (237 loc) · 6.17 KB
/
main.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
//go:generate esc -o static.go -prefix static static
package main
import (
"bytes"
"encoding/json"
"flag"
"github.com/fatih/structs"
"github.com/nfnt/resize"
"gopkg.in/yaml.v2"
"image/jpeg"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"sort"
"sync"
"time"
)
var photosMutex = &sync.Mutex{}
var photos = NewSyncMap()
var dirToMetadata = NewSyncMap()
var cache *Cache
var photoDir string
var cacheDir string
var bind string
var devel bool
type metadata struct {
Event string
Photographer string
Date string
Location string
Directory string `json:"-" structs:"directory"`
}
func handleFilters(w http.ResponseWriter, r *http.Request) {
keys := []string{"Event", "Photographer", "Date", "Location"}
filters := make(map[string]map[string]struct{})
for _, k := range keys {
filters[k] = make(map[string]struct{})
}
for meta := range dirToMetadata.Values() {
m := structs.Map(meta)
for _, k := range keys {
if _, ok := m[k]; ok {
filters[k][m[k].(string)] = struct{}{}
}
}
}
jFilters := make(map[string][]string)
for _, k := range keys {
jFilters[k] = make([]string, 0)
}
for i := range filters {
for j := range filters[i] {
jFilters[i] = append(jFilters[i], j)
}
sort.Strings(jFilters[i])
if len(jFilters[i]) == 0 {
delete(jFilters, i)
}
}
w.Header().Set("Content-Type", "application/json")
enc := json.NewEncoder(w)
if err := enc.Encode(jFilters); err != nil {
log.Print(err)
return
}
}
func handleFilter(w http.ResponseWriter, r *http.Request) {
filterToEligible := make(map[string][]string)
filters := make(map[string][]string)
for filterKey := range r.URL.Query() {
filters[filterKey] = r.URL.Query()[filterKey]
}
// find all directories with matching filters
for filterKey, filterValues := range filters {
for dir := range dirToMetadata.Keys() {
meta := dirToMetadata.Get(dir)
m := structs.Map(meta)
for k, v := range m {
if filterKey == k {
for _, filterValue := range filterValues {
if filterValue == v {
filterToEligible[filterKey] = append(filterToEligible[filterKey], dir)
}
}
}
}
}
}
// find intersection of all filter matches
eligible := make(map[string]bool)
for _, dirs := range filterToEligible {
for _, dir := range dirs {
count := 0
for _, dirTests := range filterToEligible {
for _, dirTest := range dirTests {
if dir == dirTest {
count += 1
}
}
}
if count == len(filterToEligible) {
eligible[dir] = true
}
}
}
eligiblePhotos := make([]string, 0)
for photo := range photos.Keys() {
dir := filepath.Dir(photo)
if _, ok := eligible[dir]; ok {
eligiblePhotos = append(eligiblePhotos, photo)
}
}
sort.Strings(eligiblePhotos)
w.Header().Set("Content-Type", "application/json")
enc := json.NewEncoder(w)
if err := enc.Encode(eligiblePhotos); err != nil {
log.Print(err)
return
}
}
func handleMetadata(w http.ResponseWriter, r *http.Request) {
parentDir := filepath.Dir(r.URL.Query().Get("photo"))
meta := dirToMetadata.Get(parentDir)
w.Header().Set("Content-Type", "application/json")
enc := json.NewEncoder(w)
if err := enc.Encode(meta); err != nil {
log.Print(err)
return
}
}
func handlePhoto(w http.ResponseWriter, r *http.Request) {
if filepath.Ext(r.URL.Path) != ".jpg" {
http.Error(w, http.StatusText(404), 404)
return
}
// serve photo
photoFilepath := filepath.Join(photoDir, r.URL.Path[len("/photo/"):])
http.ServeFile(w, r, photoFilepath)
}
func handleThumb(w http.ResponseWriter, r *http.Request) {
if filepath.Ext(r.URL.Path) != ".jpg" {
http.Error(w, http.StatusText(404), 404)
return
}
key := r.URL.Path[len("/cache/"):]
thumb := cache.Get(key)
// serve thumbnail
http.ServeContent(w, r, key, time.Time{}, bytes.NewReader(thumb))
}
func handleIndex(w http.ResponseWriter, r *http.Request) {
// pull index.html from esc http.FileSystem
file, err := FS(devel).Open("/index.html")
if err != nil {
log.Print(err)
return
}
stat, err := file.Stat()
if err != nil {
log.Print(err)
return
}
modTime := stat.ModTime()
http.ServeContent(w, r, "/index.html", modTime, file)
}
func cacheFillFunc(key string) []byte {
// create thumbnail
photoFilepath := filepath.Join(photoDir, key)
file, err := os.Open(photoFilepath)
if err != nil {
log.Print(err)
return nil
}
img, err := jpeg.Decode(file)
if err != nil {
log.Print(err)
return nil
}
file.Close()
m := resize.Thumbnail(500, 1000, img, resize.Bicubic)
out := &bytes.Buffer{}
jpeg.Encode(out, m, nil)
return out.Bytes()
}
func walkFunc(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
dir, filename := filepath.Split(path)
dir, err = filepath.Rel(photoDir, dir)
if err != nil {
return err
}
if filename == "metadata.yaml" {
m := metadata{Directory: dir}
data, err := ioutil.ReadFile(path)
if err != nil {
return err
}
err = yaml.Unmarshal(data, &m)
dirToMetadata.Put(dir, m)
} else if filepath.Ext(filename) == ".jpg" {
filename = filepath.Join(dir, filename)
photos.Put(filename, struct{}{})
}
return nil
}
func walk() {
for {
filepath.Walk(photoDir, walkFunc)
time.Sleep(5 * time.Second)
}
}
func main() {
flag.StringVar(&photoDir, "photoDir", "photos", "path to photos")
flag.StringVar(&cacheDir, "cacheDir", "cache", "path to photo cache directory")
var cacheSize = flag.Float64("cacheSize", 100, "photo cache size limit in megabytes")
flag.StringVar(&bind, "bind", "127.0.0.1:8000", "interface and port to bind to")
flag.BoolVar(&devel, "devel", false, "enable development mode")
flag.Parse()
go walk()
cache = NewCache(cacheDir, *cacheSize, cacheFillFunc)
go cache.PeriodicallyPrune(time.Second * 30)
// esc for static content. true uses local files, false uses embedded
http.HandleFunc("/", handleIndex)
http.Handle("/static/", http.StripPrefix("/static", http.FileServer(FS(devel))))
http.HandleFunc("/photo/", handlePhoto)
http.HandleFunc("/thumb/", handleThumb)
http.HandleFunc("/api/filters", handleFilters)
http.HandleFunc("/api/filter", handleFilter)
http.HandleFunc("/api/metadata", handleMetadata)
log.Print("listening on ", bind)
if err := http.ListenAndServe(bind, nil); err != nil {
log.Print(err)
}
}