photoprism/internal/query/folders.go
Michael Mayer 02800e796e Guess title, location and date from file and path name #154
Signed-off-by: Michael Mayer <michael@liquidbytes.net>
2020-05-28 15:12:18 +02:00

39 lines
925 B
Go

package query
import (
"path/filepath"
"github.com/photoprism/photoprism/internal/entity"
"github.com/photoprism/photoprism/pkg/fs"
)
type Folders []entity.Folder
// FoldersByPath returns a slice of folders in a given directory incl sub directories in recursive mode.
func FoldersByPath(rootName, rootPath, path string, recursive bool) (folders Folders, err error) {
dirs, err := fs.Dirs(filepath.Join(rootPath, path), recursive)
if err != nil {
return folders, err
}
folders = make(Folders, len(dirs))
for i, dir := range dirs {
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
}
}
return folders, nil
}