2020-05-24 22:16:06 +02:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// FoldersByPath returns a slice of folders in a given directory incl sub directories in recursive mode.
|
2020-06-01 09:45:24 +02:00
|
|
|
func FoldersByPath(rootName, rootPath, path string, recursive bool) (folders entity.Folders, err error) {
|
2020-05-24 22:16:06 +02:00
|
|
|
dirs, err := fs.Dirs(filepath.Join(rootPath, path), recursive)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return folders, err
|
|
|
|
}
|
|
|
|
|
2020-06-01 09:45:24 +02:00
|
|
|
folders = make(entity.Folders, len(dirs))
|
2020-05-24 22:16:06 +02:00
|
|
|
|
|
|
|
for i, dir := range dirs {
|
2020-05-28 15:12:18 +02:00
|
|
|
folder := entity.FindFolder(rootName, filepath.Join(path, dir))
|
|
|
|
|
|
|
|
if folder == nil {
|
|
|
|
newFolder := entity.NewFolder(rootName, filepath.Join(path, dir), nil)
|
|
|
|
|
|
|
|
if err := newFolder.Create(); err != nil {
|
|
|
|
log.Errorf("folders: %s (create folder)", err.Error())
|
|
|
|
} else {
|
|
|
|
folders[i] = newFolder
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
folders[i] = *folder
|
2020-05-25 19:10:44 +02:00
|
|
|
}
|
2020-05-24 22:16:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return folders, nil
|
|
|
|
}
|
2020-05-30 15:42:04 +02:00
|
|
|
|
|
|
|
// AlbumFolders returns folders that should be added as album.
|
2020-06-01 09:45:24 +02:00
|
|
|
func AlbumFolders(threshold int) (folders entity.Folders, err error) {
|
2020-05-30 21:11:56 +02:00
|
|
|
db := UnscopedDb().Table("folders").
|
2020-05-30 15:42:04 +02:00
|
|
|
Select("folders.*, COUNT(photos.id) AS photo_count").
|
|
|
|
Joins("JOIN photos ON photos.photo_path = folders.path AND photos.deleted_at IS NULL AND photos.photo_quality >= 3").
|
|
|
|
Group("folders.path").
|
|
|
|
Having("photo_count >= ?", threshold)
|
|
|
|
|
|
|
|
if err := db.Scan(&folders).Error; err != nil {
|
|
|
|
return folders, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return folders, nil
|
|
|
|
}
|