/
album.go
240 lines (202 loc) · 5.63 KB
/
album.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
// Copyright 2015 Daniel Pupius
package dbps
import (
"bytes"
"errors"
"expvar"
"fmt"
"io/ioutil"
"log"
"path"
"sort"
"sync"
"time"
"github.com/dpup/dbps/internal/dropbox"
"github.com/dpup/dbps/internal/goexif/exif"
"github.com/dpup/rcache"
)
// Album queries dropbox and keeps a list of photos in date order.
type Album struct {
folder string
dropbox *dropbox.Client
cache rcache.Cache
photoList photoList
photoMap map[string]Photo
loading bool
mu sync.RWMutex
}
// NewAlbum returns a new Album
func NewAlbum(folder string, dropbox *dropbox.Client) *Album {
a := &Album{folder: folder, dropbox: dropbox, cache: rcache.New(folder)}
a.cache.RegisterFetcher(a.fetchOriginal)
a.cache.RegisterFetcher(a.fetchThumbnail)
expvar.Publish(fmt.Sprintf("photos (%s)", folder), expvar.Func(func() interface{} {
return a.photoMap
}))
return a
}
// Monitor starts a go routine which calls Load() every interval to pick up new
// changes
func (a *Album) Monitor(interval time.Duration) {
c := interval
go func() {
for {
time.Sleep(c)
err := a.Load()
if err != nil {
log.Printf("album: failed to refresh after %s: %s", c, err)
c = c * 2
} else {
c = interval
}
}
}()
}
// Load fetches metadata about the photos in a folder. If the folder hasn't
// changed since Load was last called then no work wil be done.
func (a *Album) Load() error {
a.mu.Lock()
if a.loading {
a.mu.Unlock()
return errors.New("album: load already in progress")
}
a.loading = true
defer func() { a.loading = false }()
a.mu.Unlock()
log.Println("album: loading image metadata")
f, err := a.dropbox.Files.ListFolder(&dropbox.ListFolderInput{
Path: a.folder,
Limit: 2000,
IncludeMediaInfo: true,
})
if err != nil {
return fmt.Errorf("album: failed to list files: %s", err)
}
files := f.Entries
var wg sync.WaitGroup
photos := make(photoList, len(files))
c := 0
for i, e := range files {
name := path.Base(e.PathLower)
// If no entry exists, or the entry is stale, then load the photo to get its
// exif data. Loads are done in parallel.
if old, ok := a.photoMap[name]; !ok || old.Hash != e.ContentHash {
photos[i] = Photo{
Filename: name,
Size: int(e.Size),
Hash: e.ContentHash,
DropboxModified: e.ServerModified,
ExifCreated: e.ClientModified, // Default to the last modified time.
}
c++
wg.Add(1)
a.cache.Invalidate(originalCacheKey{name}, true)
go a.loadExifInfo(&photos[i], &wg)
} else {
photos[i] = old
}
}
if c > 0 {
log.Printf("album: waiting for new images to load")
} else {
log.Printf("album: no new images")
}
wg.Wait()
sort.Sort(photos)
// TODO(dan): Currently we are not clearing the cache of deleted images, for
// the existing usecase that is a rare scenario. Can easily be added by
// asking for deleted items and checking entry.IsDeleted
a.mu.Lock()
a.photoList = photos
a.photoMap = make(map[string]Photo)
for _, p := range photos {
a.photoMap[p.Filename] = p
}
a.mu.Unlock()
log.Println("album: metadata load complete")
return nil
}
// FirstPhoto returns the ... first photo.
func (a *Album) FirstPhoto() Photo {
return a.photoList[0]
}
// Photo returns the metadata for a photo and the image data, or an error if it doesn't exist.
func (a *Album) Photo(name string) (Photo, []byte, error) {
if photo, ok := a.photoMap[name]; ok {
data, err := a.cache.Get(originalCacheKey{name})
return photo, data, err
}
return Photo{}, nil, fmt.Errorf("album: no photo with name: %s", name)
}
// Thumbnail returns the metadata for a photo and a thumbnail, or an error if it doesn't exist.
func (a *Album) Thumbnail(name string, width, height uint) (Photo, []byte, error) {
if photo, ok := a.photoMap[name]; ok {
data, err := a.cache.Get(thumbCacheKey{name, width, height})
return photo, data, err
}
return Photo{}, nil, fmt.Errorf("album: no photo with name: %s", name)
}
// Photos returns a copy of the PhotoList.
func (a *Album) Photos() []Photo {
a.mu.RLock()
defer a.mu.RUnlock()
c := make(photoList, len(a.photoList))
copy(c, a.photoList)
return c
}
func (a *Album) loadExifInfo(p *Photo, wg *sync.WaitGroup) {
defer func() { wg.Done() }()
data, err := a.cache.Get(originalCacheKey{p.Filename})
if err != nil {
log.Printf("album: error renewing cache for %s: %s", p, err)
return
}
x, err := exif.Decode(bytes.NewReader(data))
if err != nil {
log.Printf("album: error reading exif for %s: %s", p, err)
return
}
t, err := x.DateTime()
if err != nil {
log.Printf("album: error reading exif datetime for %s: %s", p, err)
return
}
p.ExifCreated = t
}
func (a *Album) fetchOriginal(key originalCacheKey) ([]byte, error) {
// TODO(dan): Add timeout, Download gets stuck.
filename := key.Filename
log.Printf("album: fetching %s", filename)
resp, err := a.dropbox.Files.Download(&dropbox.DownloadInput{
Path: path.Join(a.folder, filename),
})
if err != nil {
return []byte{}, err
}
return ioutil.ReadAll(resp.Body)
}
func (a *Album) fetchThumbnail(key thumbCacheKey) ([]byte, error) {
data, err := a.cache.Get(originalCacheKey{key.Filename})
if err != nil {
return []byte{}, err
}
log.Printf("album: resizing %s", key.Filename)
return Resize(data, key.Width, key.Height)
}
type originalCacheKey struct {
Filename string
}
func (o originalCacheKey) String() string {
return o.Filename
}
type thumbCacheKey struct {
Filename string
Width uint
Height uint
}
func (t thumbCacheKey) Dependencies() []interface{} {
return []interface{}{originalCacheKey{t.Filename}}
}
func (t thumbCacheKey) String() string {
return fmt.Sprintf("%s@%dx%d", t.Filename, t.Width, t.Height)
}