Skip to content

Commit 1d83b30

Browse files
committed
Full scan
1 parent 0171c3e commit 1d83b30

File tree

2 files changed

+124
-13
lines changed

2 files changed

+124
-13
lines changed

server/main.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -235,16 +235,14 @@ func main() {
235235
log.Println("Scanning for photos in background...")
236236
// First cache all albums
237237
for _, collection := range config.collections {
238-
collection.QuickScan()
238+
collection.Scan(config.fullScan)
239+
}
240+
// Clean thumbnails of deleted photos0
241+
if config.fullScan {
242+
CleanupThumbnails(config.collections)
239243
}
240-
// Validate data in cache
241-
// if !config.fullScan {
242-
// // for _, collection := range config.collections {
243-
// // //collection.Validate()
244-
// // }
245-
// }
246244
// Then create thumbnails
247-
if !config.cacheThumbnails {
245+
if config.cacheThumbnails {
248246
for _, collection := range config.collections {
249247
collection.CreateThumbnails()
250248
}

server/scan.go

Lines changed: 118 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,82 @@ package main
22

33
import (
44
"log"
5+
"os"
6+
"path/filepath"
57

68
"github.com/timshannon/bolthold"
9+
"golang.org/x/exp/slices"
710
)
811

9-
func (collection *Collection) QuickScan() error {
12+
func (collection *Collection) Scan(fullScan bool) error {
1013
albums, err := collection.GetAlbums()
1114
if err != nil {
1215
return err
1316
}
17+
1418
for _, album := range albums {
15-
if collection.cache.WasAlbumSaved(album) {
19+
// Skip album if it was already scanned and it is a quick scan
20+
if !fullScan && collection.cache.WasAlbumSaved(album) {
1621
continue
1722
}
18-
collection.GetAlbumWithPhotos(album.Name, false)
23+
24+
// Load album
25+
album, err = collection.GetAlbumWithPhotos(album.Name, fullScan)
26+
if err != nil {
27+
log.Println(err)
28+
}
29+
30+
// Skip the rest if it's a quick scan
31+
if !fullScan {
32+
continue
33+
}
34+
35+
// Validate if photos have thumbnails
36+
for _, photo := range album.photosMap {
37+
thumbPath := photo.ThumbnailPath(collection)
38+
39+
// If the file doesn't exist
40+
_, err := os.Stat(thumbPath)
41+
hasThumb := !os.IsNotExist(err)
42+
43+
// Update flag if it is different than stored
44+
if photo.HasThumb != hasThumb {
45+
photo.HasThumb = hasThumb
46+
collection.cache.AddPhotoInfo(photo)
47+
}
48+
}
49+
50+
// Validate if all entries in the cacheDB are still valid
51+
var photos []*Photo
52+
err = collection.cache.store.Find(&photos, bolthold.Where("Album").Eq(album.Name).And("Id").MatchFunc(
53+
func(id string) (bool, error) {
54+
p, e := album.GetPhoto(id)
55+
if e == nil && p != nil {
56+
return false, nil
57+
}
58+
return true, nil
59+
}))
60+
if err == nil {
61+
collection.cache.DeletePhotoInfo(photos...)
62+
}
1963
}
20-
return nil
64+
65+
// Clean entries in the cacheDB of deleted albums
66+
var photos []*Photo
67+
err = collection.cache.store.Find(&photos, bolthold.Where("Album").MatchFunc(
68+
func(album string) (bool, error) {
69+
return !collection.cache.IsAlbum(album), nil
70+
}))
71+
if err == nil {
72+
collection.cache.DeletePhotoInfo(photos...)
73+
}
74+
75+
return err
2176
}
2277

2378
func (collection *Collection) CreateThumbnails() error {
24-
result, err := collection.cache.store.FindAggregate(Photo{}, bolthold.Where("HasThumb").Not().Eq(true).Index("hasthumb").SortBy("Title"), "Album")
79+
result, err := collection.cache.store.FindAggregate(Photo{},
80+
bolthold.Where("HasThumb").Not().Eq(true).Index("hasthumb").SortBy("Title"), "Album")
2581
if err != nil {
2682
return err
2783
}
@@ -46,3 +102,60 @@ func (collection *Collection) CreateThumbnails() error {
46102
}
47103
return nil
48104
}
105+
106+
func CleanupThumbnails(collections map[string]*Collection) error {
107+
var thumbPaths []string
108+
109+
// Get thumbs paths for all collections
110+
for _, collection := range collections {
111+
// Thumbnails filenames are exactly 64 chars long followed by .jpg
112+
thumbPath, err := filepath.Abs(filepath.Join(collection.ThumbsPath,
113+
"????????????????????????????????????????????????????????????????.jpg"))
114+
if err != nil {
115+
return err
116+
}
117+
if !slices.Contains(thumbPaths, thumbPath) {
118+
thumbPaths = append(thumbPaths, thumbPath)
119+
}
120+
}
121+
122+
// Gather all files from thumbs folders
123+
var files []string
124+
for _, thumbPath := range thumbPaths {
125+
folder, err := filepath.Glob(thumbPath)
126+
if err != nil {
127+
return err
128+
}
129+
files = append(files, folder...)
130+
}
131+
132+
// Filter out files for thumbnails that are used
133+
for _, collection := range collections {
134+
var photos []*Photo
135+
err := collection.cache.store.Find(&photos,
136+
bolthold.Where("HasThumb").Eq(true).Index("hasthumb").SortBy("Title"))
137+
if err != nil {
138+
return err
139+
}
140+
141+
for _, photo := range photos {
142+
thumbPath, err := filepath.Abs(photo.ThumbnailPath(collection))
143+
if err != nil {
144+
return err
145+
}
146+
147+
index := slices.Index(files, thumbPath)
148+
if index >= 0 {
149+
files = slices.Delete(files, index, index+1)
150+
}
151+
}
152+
}
153+
154+
// Delete remaining files
155+
for _, file := range files {
156+
log.Println("Deleting thumbnail", file)
157+
os.Remove(file)
158+
}
159+
160+
return nil
161+
}

0 commit comments

Comments
 (0)