mirror of
https://github.com/photoprism/photoprism.git
synced 2025-12-12 00:34:13 +01:00
Indexer: Add folder albums while indexing
This commit is contained in:
@@ -264,7 +264,7 @@ func (c *Config) UserConfig() ClientConfig {
|
||||
|
||||
c.Db().Table("albums").
|
||||
Select("SUM(album_type = ?) AS albums, SUM(album_type = ?) AS moments, SUM(album_type = ?) AS months, SUM(album_type = ?) AS states, SUM(album_type = ?) AS folders", entity.AlbumDefault, entity.AlbumMoment, entity.AlbumMonth, entity.AlbumState, entity.AlbumFolder).
|
||||
Where("deleted_at IS NULL").
|
||||
Where("deleted_at IS NULL AND (albums.album_type <> 'folder' OR albums.album_path IN (SELECT photos.photo_path FROM photos WHERE photos.deleted_at IS NULL))").
|
||||
Take(&result.Count)
|
||||
|
||||
c.Db().Table("files").
|
||||
|
||||
@@ -2,6 +2,7 @@ package entity
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -120,7 +121,9 @@ func NewAlbum(albumTitle, albumType string) *Album {
|
||||
|
||||
// NewFolderAlbum creates a new folder album.
|
||||
func NewFolderAlbum(albumTitle, albumPath, albumFilter string) *Album {
|
||||
if albumTitle == "" || albumPath == "" || albumFilter == "" {
|
||||
albumSlug := slug.Make(albumPath)
|
||||
|
||||
if albumTitle == "" || albumSlug == "" || albumPath == "" || albumFilter == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -130,7 +133,7 @@ func NewFolderAlbum(albumTitle, albumPath, albumFilter string) *Album {
|
||||
AlbumOrder: SortOrderAdded,
|
||||
AlbumType: AlbumFolder,
|
||||
AlbumTitle: albumTitle,
|
||||
AlbumSlug: slug.Make(albumPath),
|
||||
AlbumSlug: albumSlug,
|
||||
AlbumPath: albumPath,
|
||||
AlbumFilter: albumFilter,
|
||||
CreatedAt: now,
|
||||
@@ -223,10 +226,17 @@ func FindAlbumBySlug(albumSlug, albumType string) *Album {
|
||||
}
|
||||
|
||||
// FindFolderAlbum finds a matching folder album or returns nil.
|
||||
func FindFolderAlbum(albumSlug, albumPath string) *Album {
|
||||
func FindFolderAlbum(albumPath string) *Album {
|
||||
albumPath = strings.Trim(albumPath, string(os.PathSeparator))
|
||||
albumSlug := slug.Make(albumPath)
|
||||
|
||||
if albumSlug == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
result := Album{}
|
||||
|
||||
if err := UnscopedDb().Where("((album_slug <> '' AND album_slug = ?) OR album_path = ?) AND album_type = ?", albumSlug, albumPath, AlbumFolder).First(&result).Error; err != nil {
|
||||
if err := UnscopedDb().Where("album_slug = ? AND album_type = ?", albumSlug, AlbumFolder).First(&result).Error; err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -322,10 +332,17 @@ func (m *Album) Update(attr string, value interface{}) error {
|
||||
|
||||
// UpdateFolder updates the path, filter and slug for a folder album.
|
||||
func (m *Album) UpdateFolder(albumPath, albumFilter string) error {
|
||||
albumPath = strings.Trim(albumPath, string(os.PathSeparator))
|
||||
albumSlug := slug.Make(albumPath)
|
||||
|
||||
if albumSlug == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := UnscopedDb().Model(m).UpdateColumns(map[string]interface{}{
|
||||
"AlbumPath": albumPath,
|
||||
"AlbumPath": albumPath,
|
||||
"AlbumFilter": albumFilter,
|
||||
"AlbumSlug": slug.Make(albumPath),
|
||||
"AlbumSlug": albumSlug,
|
||||
}).Error; err != nil {
|
||||
return err
|
||||
} else if err := UnscopedDb().Exec("UPDATE albums SET album_path = NULL WHERE album_path = ? AND id <> ?", albumPath, m.ID).Error; err != nil {
|
||||
|
||||
@@ -4,17 +4,19 @@ import (
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gosimple/slug"
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/photoprism/photoprism/internal/event"
|
||||
"github.com/photoprism/photoprism/internal/form"
|
||||
"github.com/photoprism/photoprism/pkg/rnd"
|
||||
"github.com/photoprism/photoprism/pkg/txt"
|
||||
"github.com/ulule/deepcopier"
|
||||
)
|
||||
|
||||
var folderMutex = sync.Mutex{}
|
||||
|
||||
type Folders []Folder
|
||||
|
||||
// Folder represents a file system directory.
|
||||
@@ -153,13 +155,38 @@ func (m *Folder) Title() string {
|
||||
|
||||
// Saves the complete entity in the database.
|
||||
func (m *Folder) Create() error {
|
||||
folderMutex.Lock()
|
||||
defer folderMutex.Unlock()
|
||||
|
||||
if err := Db().Create(m).Error; err != nil {
|
||||
return err
|
||||
} else if m.Root != RootOriginals || m.Path == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
event.Publish("count.folders", event.Data{
|
||||
"count": 1,
|
||||
})
|
||||
f := form.PhotoSearch{
|
||||
Path: m.Path,
|
||||
Public: true,
|
||||
}
|
||||
|
||||
if a := FindFolderAlbum(m.Path); a != nil {
|
||||
if a.DeletedAt != nil {
|
||||
// Ignore.
|
||||
} else if err := a.UpdateFolder(m.Path, f.Serialize()); err != nil {
|
||||
log.Errorf("folder: %s (update album)", err.Error())
|
||||
}
|
||||
} else if a := NewFolderAlbum(m.Title(), m.Path, f.Serialize()); a != nil {
|
||||
a.AlbumYear = m.FolderYear
|
||||
a.AlbumMonth = m.FolderMonth
|
||||
a.AlbumDay = m.FolderDay
|
||||
a.AlbumCountry = m.FolderCountry
|
||||
|
||||
if err := a.Create(); err != nil {
|
||||
log.Errorf("folder: %s (add album)", err)
|
||||
} else {
|
||||
log.Infof("folder: added album %s (%s)", txt.Quote(a.AlbumTitle), a.AlbumFilter)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -168,6 +195,10 @@ func (m *Folder) Create() error {
|
||||
func FindFolder(root, pathName string) *Folder {
|
||||
pathName = strings.Trim(pathName, string(os.PathSeparator))
|
||||
|
||||
if pathName == RootPath {
|
||||
pathName = ""
|
||||
}
|
||||
|
||||
result := Folder{}
|
||||
|
||||
if err := Db().Where("path = ? AND root = ?", pathName, root).First(&result).Error; err == nil {
|
||||
|
||||
@@ -56,7 +56,7 @@ func Serialize(f interface{}, all bool) string {
|
||||
q = append(q, fmt.Sprintf("%s:%f", fieldName, val))
|
||||
}
|
||||
case string:
|
||||
if val := strings.TrimSpace(strings.ReplaceAll(fieldValue.String(), "\"", "")); val != "" {
|
||||
if val := strings.ReplaceAll(fieldValue.String(), "\"", ""); val != "" {
|
||||
if strings.ContainsAny(val, " :'()[]-+`") {
|
||||
q = append(q, fmt.Sprintf("%s:\"%s\"", fieldName, val))
|
||||
} else {
|
||||
|
||||
@@ -72,7 +72,7 @@ func (m *Moments) Start() (err error) {
|
||||
Public: true,
|
||||
}
|
||||
|
||||
if a := entity.FindFolderAlbum(mom.Slug(), mom.Path); a != nil {
|
||||
if a := entity.FindFolderAlbum(mom.Path); a != nil {
|
||||
if a.DeletedAt != nil {
|
||||
// Nothing to do.
|
||||
log.Tracef("moments: %s was deleted (%s)", txt.Quote(a.AlbumTitle), a.AlbumFilter)
|
||||
|
||||
Reference in New Issue
Block a user