-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
moments.go
284 lines (242 loc) Β· 9.01 KB
/
moments.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
284
package photoprism
import (
"fmt"
"math"
"path/filepath"
"runtime/debug"
"strconv"
"github.com/dustin/go-humanize/english"
"github.com/photoprism/photoprism/internal/config"
"github.com/photoprism/photoprism/internal/entity"
"github.com/photoprism/photoprism/internal/form"
"github.com/photoprism/photoprism/internal/mutex"
"github.com/photoprism/photoprism/internal/query"
"github.com/photoprism/photoprism/pkg/clean"
"github.com/photoprism/photoprism/pkg/fs"
)
// Moments represents a worker that creates albums based on popular locations, dates and labels.
type Moments struct {
conf *config.Config
}
// NewMoments returns a new Moments worker.
func NewMoments(conf *config.Config) *Moments {
instance := &Moments{
conf: conf,
}
return instance
}
// MigrateSlug updates deprecated moment slugs if needed.
func (w *Moments) MigrateSlug(m query.Moment, albumType string) {
if m.Slug() == m.TitleSlug() {
return
}
// Find and update matching album.
if a := entity.FindAlbumBySlug(m.TitleSlug(), albumType); a != nil {
logWarn("moments", a.Update("album_slug", m.Slug()))
}
}
// Start creates albums based on popular locations, dates and categories.
func (w *Moments) Start() (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("%s (panic)\nstack: %s", r, debug.Stack())
log.Errorf("moments: %s", err)
}
}()
if err = mutex.MainWorker.Start(); err != nil {
return err
}
defer mutex.MainWorker.Stop()
// Remove duplicate moments.
if removed, err := query.RemoveDuplicateMoments(); err != nil {
log.Warnf("moments: %s (remove duplicates)", err)
} else if removed > 0 {
log.Infof("moments: removed %s", english.Plural(removed, "duplicate", "duplicates"))
}
counts := query.Counts{}
counts.Refresh()
indexSize := counts.Photos + counts.Videos
threshold := 3
if indexSize > 4 {
threshold = int(math.Log2(float64(indexSize))) + 1
}
log.Debugf("moments: analyzing %d photos and %d videos, with threshold %d", counts.Photos, counts.Videos, threshold)
if indexSize < threshold {
log.Debugf("moments: not enough files")
return nil
}
// Create an album for each folder that contains originals.
if results, err := query.AlbumFolders(1); err != nil {
log.Errorf("moments: %s", err.Error())
} else {
for _, mom := range results {
f := form.SearchPhotos{
Path: mom.Path,
Public: true,
}
if a := entity.FindFolderAlbum(mom.Path); a != nil {
if a.DeletedAt != nil {
// Nothing to do.
log.Tracef("moments: %s was deleted (%s)", clean.Log(a.AlbumTitle), a.AlbumFilter)
} else if err := a.UpdateFolder(mom.Path, f.Serialize()); err != nil {
log.Errorf("moments: %s (update folder)", err.Error())
} else {
log.Tracef("moments: %s already exists (%s)", clean.Log(a.AlbumTitle), a.AlbumFilter)
}
} else if a := entity.NewFolderAlbum(mom.Title(), mom.Path, f.Serialize()); a != nil {
a.AlbumYear = mom.FolderYear
a.AlbumMonth = mom.FolderMonth
a.AlbumDay = mom.FolderDay
a.AlbumCountry = mom.FolderCountry
if err := a.Create(); err != nil {
log.Errorf("moments: %s (create folder)", err)
} else {
log.Infof("moments: added %s (%s)", clean.Log(a.AlbumTitle), a.AlbumFilter)
}
}
}
}
// Create an album for each month and year.
if results, err := query.MomentsTime(1, w.conf.Settings().Features.Private); err != nil {
log.Errorf("moments: %s", err.Error())
} else {
for _, mom := range results {
if a := entity.FindMonthAlbum(mom.Year, mom.Month); a != nil {
if err := a.UpdateTitleAndLocation(mom.Title(), "", "", "", mom.Slug()); err != nil {
log.Errorf("moments: %s (update slug)", err.Error())
}
if !a.Deleted() {
log.Tracef("moments: %s already exists (%s)", clean.Log(a.AlbumTitle), a.AlbumFilter)
} else if err := a.Restore(); err != nil {
log.Errorf("moments: %s (restore month)", err.Error())
} else {
log.Infof("moments: %s restored", clean.Log(a.AlbumTitle))
}
} else if a := entity.NewMonthAlbum(mom.Title(), mom.Slug(), mom.Year, mom.Month); a != nil {
if err := a.Create(); err != nil {
log.Errorf("moments: %s", err)
} else {
log.Infof("moments: added %s (%s)", clean.Log(a.AlbumTitle), a.AlbumFilter)
}
}
}
}
// Create moments based on country and year.
if results, err := query.MomentsCountries(threshold, w.conf.Settings().Features.Private); err != nil {
log.Errorf("moments: %s", err.Error())
} else {
for _, mom := range results {
f := form.SearchPhotos{
Country: mom.Country,
Year: strconv.Itoa(mom.Year),
Public: true,
}
if a := entity.FindAlbumByAttr(S{mom.Slug(), mom.TitleSlug()}, S{f.Serialize()}, entity.AlbumMoment); a != nil {
if err := a.UpdateTitleAndLocation(mom.Title(), "", mom.State, mom.Country, mom.Slug()); err != nil {
log.Errorf("moments: %s (update slug)", err.Error())
}
if a.DeletedAt != nil {
// Nothing to do.
log.Tracef("moments: %s was deleted (%s)", clean.Log(a.AlbumTitle), a.AlbumFilter)
} else {
log.Tracef("moments: %s already exists (%s)", clean.Log(a.AlbumTitle), a.AlbumFilter)
}
} else if a := entity.NewMomentsAlbum(mom.Title(), mom.Slug(), f.Serialize()); a != nil {
a.AlbumYear = mom.Year
a.SetLocation("", mom.State, mom.Country)
if err := a.Create(); err != nil {
log.Errorf("moments: %s", err)
} else {
log.Infof("moments: added %s (%s)", clean.Log(a.AlbumTitle), a.AlbumFilter)
}
}
}
}
// Create moments based on states and countries.
if results, err := query.MomentsStates(1, w.conf.Settings().Features.Private); err != nil {
log.Errorf("moments: %s", err.Error())
} else {
for _, mom := range results {
f := form.SearchPhotos{
Country: mom.Country,
State: mom.State,
Public: true,
}
if a := entity.FindAlbumByAttr(S{mom.Slug(), mom.TitleSlug()}, S{f.Serialize()}, entity.AlbumState); a != nil {
if err := a.UpdateTitleAndState(mom.Title(), mom.Slug(), mom.State, mom.Country); err != nil {
log.Errorf("moments: %s (update state)", err.Error())
}
if !a.Deleted() {
log.Tracef("moments: %s already exists (%s)", clean.Log(a.AlbumTitle), a.AlbumFilter)
} else if err := a.Restore(); err != nil {
log.Errorf("moments: %s (restore state)", err.Error())
} else {
log.Infof("moments: %s restored", clean.Log(a.AlbumTitle))
}
} else if a := entity.NewStateAlbum(mom.Title(), mom.Slug(), f.Serialize()); a != nil {
a.SetLocation(mom.CountryName(), mom.State, mom.Country)
if err := a.Create(); err != nil {
log.Errorf("moments: %s", err)
} else {
log.Infof("moments: added %s (%s)", clean.Log(a.AlbumTitle), a.AlbumFilter)
}
}
}
}
// Create moments based on related image classifications.
if results, err := query.MomentsLabels(threshold, w.conf.Settings().Features.Private); err != nil {
log.Errorf("moments: %s", err.Error())
} else {
for _, mom := range results {
w.MigrateSlug(mom, entity.AlbumMoment)
f := form.SearchPhotos{
Label: mom.Label,
Public: true,
}
if a := entity.FindAlbumByAttr(S{mom.Slug(), mom.TitleSlug()}, S{f.Serialize()}, entity.AlbumMoment); a != nil {
if err := a.UpdateTitleAndLocation(mom.Title(), "", "", "", mom.Slug()); err != nil {
log.Errorf("moments: %s (update slug)", err.Error())
}
if a.DeletedAt != nil || f.Serialize() == a.AlbumFilter {
log.Tracef("moments: %s already exists (%s)", clean.Log(a.AlbumTitle), a.AlbumFilter)
continue
}
if err := a.Update("AlbumFilter", f.Serialize()); err != nil {
log.Errorf("moments: %s", err.Error())
} else {
log.Debugf("moments: updated %s (%s)", clean.Log(a.AlbumTitle), f.Serialize())
}
} else if a := entity.NewMomentsAlbum(mom.Title(), mom.Slug(), f.Serialize()); a != nil {
if err := a.Create(); err != nil {
log.Errorf("moments: %s", err.Error())
} else {
log.Infof("moments: added %s (%s)", clean.Log(a.AlbumTitle), a.AlbumFilter)
}
} else {
log.Errorf("moments: failed to create new moment %s (%s)", mom.Title(), f.Serialize())
}
}
}
// UpdateFolderDates updates folder year, month and day based on indexed photo metadata.
if err := query.UpdateFolderDates(); err != nil {
log.Errorf("moments: %s (update folder dates)", err.Error())
}
// UpdateAlbumDates updates the year, month and day of the album based on the indexed photo metadata.
if err := query.UpdateAlbumDates(); err != nil {
log.Errorf("moments: %s (update album dates)", err.Error())
}
// Make sure that the albums have been backed up before, otherwise back up all albums.
if fs.PathExists(filepath.Join(w.conf.AlbumsPath(), entity.AlbumManual)) &&
fs.PathExists(filepath.Join(w.conf.AlbumsPath(), entity.AlbumMonth)) {
// Skip.
} else if count, err := BackupAlbums(w.conf.AlbumsPath(), false); err != nil {
log.Errorf("moments: %s (backup albums)", err.Error())
} else if count > 0 {
log.Debugf("moments: %d albums saved as yaml files", count)
}
return nil
}
// Cancel stops the current operation.
func (w *Moments) Cancel() {
mutex.MainWorker.Cancel()
}