Skip to content

Commit

Permalink
FIX: Saving favorites multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
rigon committed Jul 25, 2023
1 parent a0d01da commit 0171c3e
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 34 deletions.
2 changes: 1 addition & 1 deletion server/album.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (album *Album) GetPhotos(collection *Collection) error {
// Update photo with favorite album
result := targetPhoto.AddFavorite(collection, album)
if result {
go targetCollection.cache.AddPhotoInfo(targetAlbum, targetPhoto)
go targetCollection.cache.AddPhotoInfo(targetPhoto)
}

photo := targetPhoto.CopyForPseudoAlbum(targetCollection, targetAlbum)
Expand Down
21 changes: 9 additions & 12 deletions server/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ func (c Cache) FillPhotosInfo(album *Album) (err error) {
for _, photo := range album.photosMap {
var data Photo
count++
key := album.Name + ":" + photo.Id
err := c.store.TxGet(tx, key, &data)
err := c.store.TxGet(tx, photo.Key(), &data)
// Does not require update
if err == nil && data.Id == photo.Id && data.Collection == photo.Collection &&
data.Album == photo.Album && len(data.Files) == len(photo.Files) { // Validate some fields
Expand All @@ -170,18 +169,17 @@ func (c Cache) FillPhotosInfo(album *Album) (err error) {
}

// Update missing entries
return c.AddPhotoInfo(album, update...)
return c.AddPhotoInfo(update...)
}

// Add or update info for photos
func (c Cache) AddPhotoInfo(album *Album, photos ...*Photo) error {
func (c Cache) AddPhotoInfo(photos ...*Photo) error {
return c.store.Bolt().Update(func(tx *bolt.Tx) error {
for i, photo := range photos {
if i%100 == 0 || i == len(photos)-1 {
log.Printf("Updating cache info %s (%d/%d)", album.Name, i+1, len(photos))
log.Printf("Updating cache info %s (%d/%d)", photo.Album, i+1, len(photos))
}
key := album.Name + ":" + photo.Id
err := c.store.TxUpsert(tx, key, photo)
err := c.store.TxUpsert(tx, photo.Key(), photo)
if err != nil {
log.Println(err)
}
Expand All @@ -191,20 +189,19 @@ func (c Cache) AddPhotoInfo(album *Album, photos ...*Photo) error {
}

// Delete photo info
func (c Cache) DeletePhotoInfo(album *Album, photos ...*Photo) ([]*Photo, error) {
func (c Cache) DeletePhotoInfo(photos ...*Photo) ([]*Photo, error) {
return photos, c.store.Bolt().Update(func(tx *bolt.Tx) error {
for i, photo := range photos {
if i%100 == 0 || i == len(photos)-1 {
log.Printf("Removing cache info %s (%d/%d)", album.Name, i+1, len(photos))
log.Printf("Removing cache info %s (%d/%d)", photo.Album, i+1, len(photos))
}
key := album.Name + ":" + photo.Id
// Update info to return
err := c.store.TxGet(tx, key, photo)
err := c.store.TxGet(tx, photo.Key(), photo)
if err != nil {
log.Println(err)
}
// Delete entry
err = c.store.TxDelete(tx, key, photo)
err = c.store.TxDelete(tx, photo.Key(), photo)
if err != nil {
log.Println(err)
}
Expand Down
6 changes: 4 additions & 2 deletions server/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (c *Collection) IsAlbum(albumName string) bool {
func (c *Collection) GetAlbum(albumName string) (*Album, error) {
// Check if album exists
if !c.IsAlbum(albumName) {
return nil, errors.New("album not found")
return nil, errors.New("album not found: " + albumName)
}
// Check for regular album (i.e. folder)
filename := filepath.Join(c.PhotosPath, albumName)
Expand Down Expand Up @@ -161,7 +161,9 @@ func (c *Collection) GetAlbumWithPhotos(albumName string, forceUpdate bool) (*Al
// Get photos from the disk
album.GetPhotos(c)
// Fill photos with info in cache (e.g. height and width)
c.cache.FillPhotosInfo(album)
if !album.IsPseudo { // Pseudo albums already have info filled
c.cache.FillPhotosInfo(album)
}
// ...and save to cache
c.cache.SaveAlbum(album)

Expand Down
1 change: 1 addition & 0 deletions server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/zulucmd/zflag v1.1.2
gitlab.com/golang-utils/image2 v0.0.1
go.etcd.io/bbolt v1.3.7
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1
golang.org/x/image v0.9.0
golang.org/x/net v0.12.0
)
Expand Down
2 changes: 2 additions & 0 deletions server/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.11.0 h1:6Ewdq3tDic1mg5xRO4milcWCfMVQhI4NkqWWvqejpuA=
golang.org/x/crypto v0.11.0/go.mod h1:xgJhtzW8F9jGdVFWZESrid1U1bjeNy4zgy5cRr/CIio=
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 h1:MGwJjxBy0HJshjDNfLsYO8xppfqWlA5ZT9OhtUUhTNw=
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/image v0.9.0 h1:QrzfX26snvCM20hIhBwuHI/ThTg18b/+kcKdXHvnR+g=
golang.org/x/image v0.9.0/go.mod h1:jtrku+n79PfroUbvDdeUWMAI+heR786BofxrbiSF+J0=
Expand Down
16 changes: 11 additions & 5 deletions server/photo.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"path/filepath"
"strings"
"time"

"golang.org/x/exp/slices"
)

type Photo struct {
Expand All @@ -30,6 +32,10 @@ type Photo struct {
HasThumb bool `json:"-" boltholdIndex:"hasthumb"` // Indicates if the thumbnail was generated
}

func (photo *Photo) Key() string {
return photo.Album + ":" + photo.Id
}

// Add pseudo album to the favorites list
func (photo *Photo) AddFavorite(srcCollection *Collection, srcAlbum *Album) bool {
collection, album := srcCollection.Name, srcAlbum.Name
Expand All @@ -49,16 +55,16 @@ func (photo *Photo) RemoveFavorite(srcCollection *Collection, srcAlbum *Album) b
for i, favorite := range photo.Favorite {
// Remove it from the list if present
if srcCollection.Name == favorite.Collection && srcAlbum.Name == favorite.Album {
photo.Favorite = append(photo.Favorite[:i], photo.Favorite[i+1:]...)
photo.Favorite = slices.Delete(photo.Favorite, i, i+1)
return true
}
}
return false
}

// Returns the path location for the thumbnail
func (photo *Photo) ThumbnailPath(collection *Collection, album *Album) string {
name := strings.Join([]string{collection.Name, album.Name, photo.Id}, ":")
func (photo *Photo) ThumbnailPath(collection *Collection) string {
name := strings.Join([]string{photo.Collection, photo.Album, photo.Id}, ":")
hash := sha256.Sum256([]byte(name))
encoded := hex.EncodeToString(hash[:])
return filepath.Join(collection.ThumbsPath, encoded+".jpg")
Expand Down Expand Up @@ -96,7 +102,7 @@ func (photo *Photo) MainFile() *File {
}

func (photo *Photo) GetThumbnail(collection *Collection, album *Album, w io.Writer) error {
thumbPath := photo.ThumbnailPath(collection, album)
thumbPath := photo.ThumbnailPath(collection)

// If the file doesn't exist
if _, err := os.Stat(thumbPath); os.IsNotExist(err) {
Expand Down Expand Up @@ -125,7 +131,7 @@ func (photo *Photo) GetThumbnail(collection *Collection, album *Album, w io.Writ
// Update flag to indicate that the thumbnail was generated
if !photo.HasThumb {
photo.HasThumb = true
collection.cache.AddPhotoInfo(album, photo)
collection.cache.AddPhotoInfo(photo)
}
return nil
}
Expand Down
20 changes: 6 additions & 14 deletions server/pseudo.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"os"
"path/filepath"
"strings"

"golang.org/x/exp/slices"
)

const PSEUDO_ALBUM_EXT = ".PG-ALBUM"
Expand Down Expand Up @@ -190,7 +192,7 @@ func (album *Album) EditPseudoAlbum(collection *Collection, query PseudoAlbumSav
} else {
if found >= 0 {
// Remove the entry
entries = append(entries[:found], entries[found+1:]...)
entries = slices.Delete(entries, found, found+1)
} else {
errs = append(errs, "entry could not be found: "+edit.Photo)
}
Expand All @@ -213,8 +215,6 @@ func (album *Album) EditPseudoAlbum(collection *Collection, query PseudoAlbumSav

// Update in background cached entries that were changed
go func() {
var photos []*Photo

for _, entry := range updated {
fromCollection, err := GetCollection(entry.Collection)
if err != nil {
Expand All @@ -227,7 +227,7 @@ func (album *Album) EditPseudoAlbum(collection *Collection, query PseudoAlbumSav
continue
}

var fromPhotos []*Photo
var photos []*Photo
for _, photo := range entry.Photos {
fromPhoto, err := fromAlbum.GetPhoto(photo)
if err != nil {
Expand All @@ -241,23 +241,15 @@ func (album *Album) EditPseudoAlbum(collection *Collection, query PseudoAlbumSav
result = fromPhoto.RemoveFavorite(collection, album)
}
if result {
fromPhotos = append(fromPhotos, fromPhoto)
photos = append(photos, fromPhoto)
}
photos = append(photos, fromPhoto.CopyForPseudoAlbum(fromCollection, fromAlbum))
}
// Update info about cached photos
err = fromCollection.cache.AddPhotoInfo(fromAlbum, fromPhotos...)
err = fromCollection.cache.AddPhotoInfo(photos...)
if err != nil {
log.Println(err)
}
}

// Add or remove updated entries from cache of the pseudo album
if isAdd {
collection.cache.AddPhotoInfo(album, photos...)
} else {
collection.cache.DeletePhotoInfo(album, photos...)
}
}()

if len(errs) > 0 {
Expand Down

0 comments on commit 0171c3e

Please sign in to comment.