Skip to content

Commit

Permalink
Full scan
Browse files Browse the repository at this point in the history
  • Loading branch information
rigon committed Jul 25, 2023
1 parent 0171c3e commit 1d83b30
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 13 deletions.
14 changes: 6 additions & 8 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,16 +235,14 @@ func main() {
log.Println("Scanning for photos in background...")
// First cache all albums
for _, collection := range config.collections {
collection.QuickScan()
collection.Scan(config.fullScan)
}
// Clean thumbnails of deleted photos0
if config.fullScan {
CleanupThumbnails(config.collections)
}
// Validate data in cache
// if !config.fullScan {
// // for _, collection := range config.collections {
// // //collection.Validate()
// // }
// }
// Then create thumbnails
if !config.cacheThumbnails {
if config.cacheThumbnails {
for _, collection := range config.collections {
collection.CreateThumbnails()
}
Expand Down
123 changes: 118 additions & 5 deletions server/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,82 @@ package main

import (
"log"
"os"
"path/filepath"

"github.com/timshannon/bolthold"
"golang.org/x/exp/slices"
)

func (collection *Collection) QuickScan() error {
func (collection *Collection) Scan(fullScan bool) error {
albums, err := collection.GetAlbums()
if err != nil {
return err
}

for _, album := range albums {
if collection.cache.WasAlbumSaved(album) {
// Skip album if it was already scanned and it is a quick scan
if !fullScan && collection.cache.WasAlbumSaved(album) {
continue
}
collection.GetAlbumWithPhotos(album.Name, false)

// Load album
album, err = collection.GetAlbumWithPhotos(album.Name, fullScan)
if err != nil {
log.Println(err)
}

// Skip the rest if it's a quick scan
if !fullScan {
continue
}

// Validate if photos have thumbnails
for _, photo := range album.photosMap {
thumbPath := photo.ThumbnailPath(collection)

// If the file doesn't exist
_, err := os.Stat(thumbPath)
hasThumb := !os.IsNotExist(err)

// Update flag if it is different than stored
if photo.HasThumb != hasThumb {
photo.HasThumb = hasThumb
collection.cache.AddPhotoInfo(photo)
}
}

// Validate if all entries in the cacheDB are still valid
var photos []*Photo
err = collection.cache.store.Find(&photos, bolthold.Where("Album").Eq(album.Name).And("Id").MatchFunc(
func(id string) (bool, error) {
p, e := album.GetPhoto(id)
if e == nil && p != nil {
return false, nil
}
return true, nil
}))
if err == nil {
collection.cache.DeletePhotoInfo(photos...)
}
}
return nil

// Clean entries in the cacheDB of deleted albums
var photos []*Photo
err = collection.cache.store.Find(&photos, bolthold.Where("Album").MatchFunc(
func(album string) (bool, error) {
return !collection.cache.IsAlbum(album), nil
}))
if err == nil {
collection.cache.DeletePhotoInfo(photos...)
}

return err
}

func (collection *Collection) CreateThumbnails() error {
result, err := collection.cache.store.FindAggregate(Photo{}, bolthold.Where("HasThumb").Not().Eq(true).Index("hasthumb").SortBy("Title"), "Album")
result, err := collection.cache.store.FindAggregate(Photo{},
bolthold.Where("HasThumb").Not().Eq(true).Index("hasthumb").SortBy("Title"), "Album")
if err != nil {
return err
}
Expand All @@ -46,3 +102,60 @@ func (collection *Collection) CreateThumbnails() error {
}
return nil
}

func CleanupThumbnails(collections map[string]*Collection) error {
var thumbPaths []string

// Get thumbs paths for all collections
for _, collection := range collections {
// Thumbnails filenames are exactly 64 chars long followed by .jpg
thumbPath, err := filepath.Abs(filepath.Join(collection.ThumbsPath,
"????????????????????????????????????????????????????????????????.jpg"))
if err != nil {
return err
}
if !slices.Contains(thumbPaths, thumbPath) {
thumbPaths = append(thumbPaths, thumbPath)
}
}

// Gather all files from thumbs folders
var files []string
for _, thumbPath := range thumbPaths {
folder, err := filepath.Glob(thumbPath)
if err != nil {
return err
}
files = append(files, folder...)
}

// Filter out files for thumbnails that are used
for _, collection := range collections {
var photos []*Photo
err := collection.cache.store.Find(&photos,
bolthold.Where("HasThumb").Eq(true).Index("hasthumb").SortBy("Title"))
if err != nil {
return err
}

for _, photo := range photos {
thumbPath, err := filepath.Abs(photo.ThumbnailPath(collection))
if err != nil {
return err
}

index := slices.Index(files, thumbPath)
if index >= 0 {
files = slices.Delete(files, index, index+1)
}
}
}

// Delete remaining files
for _, file := range files {
log.Println("Deleting thumbnail", file)
os.Remove(file)
}

return nil
}

0 comments on commit 1d83b30

Please sign in to comment.