Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset thumbnail if cover photo has been removed from album #127

Open
wants to merge 4 commits into
base: preview
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions internal/api/albums.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,10 @@ func RemovePhotosFromAlbum(router *gin.RouterGroup) {
event.SuccessMsg(i18n.MsgEntriesRemovedFrom, len(removed), clean.Log(a.Title()))
}

if err := a.ResetCoverIfNeeded(removed); err != nil {
log.Errorf("album: %s (reset thumbnail)", err)
}

RemoveFromAlbumCoverCache(a.AlbumUID)

PublishAlbumEvent(EntityUpdated, a.AlbumUID, c)
Expand Down
11 changes: 11 additions & 0 deletions internal/api/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,17 @@ func BatchPhotosPrivate(router *gin.RouterGroup) {
}

event.EntitiesUpdated("photos", photos)

// Reset the album covers, if any of the private photos were used as thumbnails.
if fileHashes, err := photos.Private().AllFileHashes(); err != nil {
log.Errorf("photos: %s (retrieve private photos file hashes)", err)
} else if len(fileHashes) > 0 {
if updated, err := query.RemovePhotosAsAlbumCovers(fileHashes); err != nil {
log.Errorf("photos: %s (removing private photos as album covers)", err)
} else {
log.Infof("photos: removed %d private %s as album covers", updated, english.PluralWord(int(updated), "photo", "photos"))
}
}
}

UpdateClientConfig()
Expand Down
32 changes: 31 additions & 1 deletion internal/entity/album.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ func NewMonthAlbum(albumTitle, albumSlug string, year, month int) *Album {
return result
}

// FindLabelAlbums finds all label albums (including deletes ones) or returns nil.
// FindLabelAlbums finds all label-based moment albums (including deletes ones) or returns nil.
func FindLabelAlbums() (result Albums) {
// Both "label" and "country / year" album have the same album type,
// so to distinguish between the two we have few options:
Expand All @@ -306,6 +306,16 @@ func FindLabelAlbums() (result Albums) {
return result
}

// FindAlbumByType finds all smart albums (including deleted ones) or returns nil.
func FindSmartAlbums() (result Albums) {
if err := UnscopedDb().Where("album_type = ? AND album_filter != ''", AlbumManual).Find(&result).Error; err != nil {
log.Errorf("album: %s (not found)", err)
return nil
}

return result
}

// FindCountriesByYearAlbums finds all moment albums (including deleted ones) or returns nil.
func FindCountriesByYearAlbums() (result Albums) {
if err := UnscopedDb().
Expand Down Expand Up @@ -895,3 +905,23 @@ func (m *Album) RemovePhotos(UIDs []string) (removed PhotoAlbums) {
func (m *Album) Links() Links {
return FindLinks("", m.AlbumUID)
}

// ResetCoverIfNeeded removes the album cover if it was part of the removed album photos.
func (m *Album) ResetCoverIfNeeded(removed PhotoAlbums) error {
if !m.HasThumb() {
return nil
}

f, err := FirstFileByHash(m.Thumb)
if err != nil {
return err
}

for _, pa := range removed {
if pa.PhotoUID == f.PhotoUID {
return m.ResetThumb()
}
}

return nil
}
22 changes: 22 additions & 0 deletions internal/entity/photo.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,28 @@ func (m Photos) UIDs() []string {
return result
}

// AllFileHashes returns the hashes for all photos files.
func (m Photos) AllFileHashes() (hashes []string, err error) {
err = Db().Model(File{}).
Where("photo_uid IN (?)", m.UIDs()).
Pluck("file_hash", &hashes).Error

return hashes, err
}

// Private returns a slice of all private photos.
func (m Photos) Private() Photos {
result := make(Photos, 0, len(m))

for _, el := range m {
if el.PhotoPrivate {
result = append(result, el)
}
}

return result
}

// MapKey returns a key referencing time and location for indexing.
func MapKey(takenAt time.Time, cellId string) string {
return path.Join(strconv.FormatInt(takenAt.Unix(), 36), cellId)
Expand Down
Loading