Store ignore data in cache
Store album ignore data in cache and not database. Fix albums with only ignore-media-only not to be shown in album list. Use github.com/sabhiram/go-gitignore exclusively for photoviewignore. Signed-off-by: Kjeldgaard <Kjeldgaard@users.noreply.github.com>
This commit is contained in:
parent
3ad12e3025
commit
8afff70d9a
|
@ -17,7 +17,6 @@ type Album struct {
|
||||||
Owners []User `gorm:"many2many:user_albums"`
|
Owners []User `gorm:"many2many:user_albums"`
|
||||||
Path string `gorm:"not null"`
|
Path string `gorm:"not null"`
|
||||||
PathHash string `gorm:"unique"`
|
PathHash string `gorm:"unique"`
|
||||||
IgnoreFiles string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Album) FilePath() string {
|
func (a *Album) FilePath() string {
|
||||||
|
|
|
@ -10,6 +10,7 @@ import (
|
||||||
type AlbumScannerCache struct {
|
type AlbumScannerCache struct {
|
||||||
path_contains_photos map[string]bool
|
path_contains_photos map[string]bool
|
||||||
photo_types map[string]MediaType
|
photo_types map[string]MediaType
|
||||||
|
ignore_data map[string][]string
|
||||||
mutex sync.Mutex
|
mutex sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,6 +18,7 @@ func MakeAlbumCache() *AlbumScannerCache {
|
||||||
return &AlbumScannerCache{
|
return &AlbumScannerCache{
|
||||||
path_contains_photos: make(map[string]bool),
|
path_contains_photos: make(map[string]bool),
|
||||||
photo_types: make(map[string]MediaType),
|
photo_types: make(map[string]MediaType),
|
||||||
|
ignore_data: make(map[string][]string),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,3 +86,22 @@ func (c *AlbumScannerCache) GetMediaType(path string) (*MediaType, error) {
|
||||||
|
|
||||||
return mediaType, nil
|
return mediaType, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *AlbumScannerCache) GetAlbumIgnore(path string) (*[]string) {
|
||||||
|
c.mutex.Lock()
|
||||||
|
defer c.mutex.Unlock()
|
||||||
|
|
||||||
|
ignore_data, found := c.ignore_data[path]
|
||||||
|
if found {
|
||||||
|
return &ignore_data
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *AlbumScannerCache) InsertAlbumIgnore(path string, ignore_data []string) {
|
||||||
|
c.mutex.Lock()
|
||||||
|
defer c.mutex.Unlock()
|
||||||
|
|
||||||
|
c.ignore_data[path] = ignore_data
|
||||||
|
}
|
||||||
|
|
|
@ -5,8 +5,6 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/photoview/photoview/api/graphql/models"
|
"github.com/photoview/photoview/api/graphql/models"
|
||||||
|
@ -14,6 +12,7 @@ import (
|
||||||
"github.com/photoview/photoview/api/scanner/face_detection"
|
"github.com/photoview/photoview/api/scanner/face_detection"
|
||||||
"github.com/photoview/photoview/api/utils"
|
"github.com/photoview/photoview/api/utils"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
"github.com/sabhiram/go-gitignore"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -139,30 +138,19 @@ func findMediaForAlbum(album *models.Album, cache *AlbumScannerCache, db *gorm.D
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get file ignores
|
// Get ignore data
|
||||||
var ignores []string
|
albumIgnore := ignore.CompileIgnoreLines(*cache.GetAlbumIgnore(album.Path)...)
|
||||||
if (len(album.IgnoreFiles) > 0) {
|
|
||||||
ignores = strings.Split(album.IgnoreFiles, ",")
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, item := range dirContent {
|
for _, item := range dirContent {
|
||||||
photoPath := path.Join(album.Path, item.Name())
|
photoPath := path.Join(album.Path, item.Name())
|
||||||
|
|
||||||
skipFile := false
|
if !item.IsDir() && isPathMedia(photoPath, cache) {
|
||||||
for _, ignore := range ignores {
|
// Match file against ignore data
|
||||||
match, _ := filepath.Match(ignore, item.Name())
|
if (albumIgnore.MatchesPath(item.Name())) {
|
||||||
if (match) {
|
log.Printf("File %s ignored\n", item.Name())
|
||||||
// Skip file if it matches an ignore entry
|
|
||||||
log.Printf("Ignore match, ignore = %s, file = %s\n", ignore, item.Name())
|
|
||||||
skipFile = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (skipFile) {
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if !item.IsDir() && isPathMedia(photoPath, cache) {
|
|
||||||
// Skip the JPEGs that are compressed version of raw files
|
// Skip the JPEGs that are compressed version of raw files
|
||||||
counterpartFile := scanForRawCounterpartFile(photoPath)
|
counterpartFile := scanForRawCounterpartFile(photoPath)
|
||||||
if counterpartFile != nil {
|
if counterpartFile != nil {
|
||||||
|
|
|
@ -8,7 +8,6 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/photoview/photoview/api/graphql/models"
|
"github.com/photoview/photoview/api/graphql/models"
|
||||||
"github.com/photoview/photoview/api/graphql/notification"
|
"github.com/photoview/photoview/api/graphql/notification"
|
||||||
|
@ -18,21 +17,6 @@ import (
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func isFileIgnore(ignoreEntry string) (bool){
|
|
||||||
if (strings.Contains(ignoreEntry, "/")){
|
|
||||||
// Folder ignore if entry contains '/'
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
if (strings.Contains(ignoreEntry, ".")){
|
|
||||||
// File ignore if entry contains '.' and not '/'
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// Otherwise folder ignore entry
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func getPhotoviewIgnore(ignorePath string) ([]string , error){
|
func getPhotoviewIgnore(ignorePath string) ([]string , error){
|
||||||
var photoviewIgnore []string
|
var photoviewIgnore []string
|
||||||
|
|
||||||
|
@ -55,20 +39,6 @@ func getPhotoviewIgnore(ignorePath string) ([]string , error){
|
||||||
return photoviewIgnore, scanner.Err()
|
return photoviewIgnore, scanner.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
func getIgnoreFiles(ignoreAll []string) (string) {
|
|
||||||
var ignoreFiles []string
|
|
||||||
|
|
||||||
if (len(ignoreAll) > 0) {
|
|
||||||
for _, ignoreItem := range ignoreAll {
|
|
||||||
if (isFileIgnore(ignoreItem)) {
|
|
||||||
ignoreFiles = append(ignoreFiles, ignoreItem)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return strings.Join(ignoreFiles, ",")
|
|
||||||
}
|
|
||||||
|
|
||||||
func findAlbumsForUser(db *gorm.DB, user *models.User, album_cache *AlbumScannerCache) ([]*models.Album, []error) {
|
func findAlbumsForUser(db *gorm.DB, user *models.User, album_cache *AlbumScannerCache) ([]*models.Album, []error) {
|
||||||
|
|
||||||
if err := user.FillAlbums(db); err != nil {
|
if err := user.FillAlbums(db); err != nil {
|
||||||
|
@ -175,9 +145,11 @@ func findAlbumsForUser(db *gorm.DB, user *models.User, album_cache *AlbumScanner
|
||||||
Title: albumTitle,
|
Title: albumTitle,
|
||||||
ParentAlbumID: albumParentID,
|
ParentAlbumID: albumParentID,
|
||||||
Path: albumPath,
|
Path: albumPath,
|
||||||
IgnoreFiles: getIgnoreFiles(albumIgnore),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Store album ignore
|
||||||
|
album_cache.InsertAlbumIgnore(albumPath, albumIgnore)
|
||||||
|
|
||||||
if err := tx.Create(&album).Error; err != nil {
|
if err := tx.Create(&album).Error; err != nil {
|
||||||
return errors.Wrap(err, "insert album into database")
|
return errors.Wrap(err, "insert album into database")
|
||||||
}
|
}
|
||||||
|
@ -200,7 +172,9 @@ func findAlbumsForUser(db *gorm.DB, user *models.User, album_cache *AlbumScanner
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
album.IgnoreFiles = getIgnoreFiles(albumIgnore)
|
|
||||||
|
// Update album ignore
|
||||||
|
album_cache.InsertAlbumIgnore(albumPath, albumIgnore)
|
||||||
}
|
}
|
||||||
|
|
||||||
userAlbums = append(userAlbums, album)
|
userAlbums = append(userAlbums, album)
|
||||||
|
@ -222,7 +196,7 @@ func findAlbumsForUser(db *gorm.DB, user *models.User, album_cache *AlbumScanner
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if item.IsDir() && directoryContainsPhotos(subalbumPath, album_cache) {
|
if item.IsDir() && directoryContainsPhotos(subalbumPath, album_cache, albumIgnore) {
|
||||||
scanQueue.PushBack(scanInfo{
|
scanQueue.PushBack(scanInfo{
|
||||||
path: subalbumPath,
|
path: subalbumPath,
|
||||||
parent: album,
|
parent: album,
|
||||||
|
@ -238,7 +212,7 @@ func findAlbumsForUser(db *gorm.DB, user *models.User, album_cache *AlbumScanner
|
||||||
return userAlbums, scanErrors
|
return userAlbums, scanErrors
|
||||||
}
|
}
|
||||||
|
|
||||||
func directoryContainsPhotos(rootPath string, cache *AlbumScannerCache) bool {
|
func directoryContainsPhotos(rootPath string, cache *AlbumScannerCache, albumIgnore []string) bool {
|
||||||
|
|
||||||
if contains_image := cache.AlbumContainsPhotos(rootPath); contains_image != nil {
|
if contains_image := cache.AlbumContainsPhotos(rootPath); contains_image != nil {
|
||||||
return *contains_image
|
return *contains_image
|
||||||
|
@ -256,6 +230,15 @@ func directoryContainsPhotos(rootPath string, cache *AlbumScannerCache) bool {
|
||||||
|
|
||||||
scanned_directories = append(scanned_directories, dirPath)
|
scanned_directories = append(scanned_directories, dirPath)
|
||||||
|
|
||||||
|
// Update ignore dir list
|
||||||
|
photoviewIgnore, err := getPhotoviewIgnore(dirPath)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to get ignore file, err = %s", err)
|
||||||
|
} else {
|
||||||
|
albumIgnore = append(albumIgnore, photoviewIgnore...)
|
||||||
|
}
|
||||||
|
ignoreEntries := ignore.CompileIgnoreLines(albumIgnore...)
|
||||||
|
|
||||||
dirContent, err := ioutil.ReadDir(dirPath)
|
dirContent, err := ioutil.ReadDir(dirPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ScannerError("Could not read directory (%s): %s\n", dirPath, err.Error())
|
ScannerError("Could not read directory (%s): %s\n", dirPath, err.Error())
|
||||||
|
@ -268,6 +251,11 @@ func directoryContainsPhotos(rootPath string, cache *AlbumScannerCache) bool {
|
||||||
scanQueue.PushBack(filePath)
|
scanQueue.PushBack(filePath)
|
||||||
} else {
|
} else {
|
||||||
if isPathMedia(filePath, cache) {
|
if isPathMedia(filePath, cache) {
|
||||||
|
if ignoreEntries.MatchesPath(fileInfo.Name()) {
|
||||||
|
log.Printf("Match found %s, continue search for media", fileInfo.Name())
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
log.Printf("Insert Album %s %s, contains photo is true", dirPath, rootPath)
|
||||||
cache.InsertAlbumPaths(dirPath, rootPath, true)
|
cache.InsertAlbumPaths(dirPath, rootPath, true)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
@ -277,6 +265,7 @@ func directoryContainsPhotos(rootPath string, cache *AlbumScannerCache) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, scanned_path := range scanned_directories {
|
for _, scanned_path := range scanned_directories {
|
||||||
|
log.Printf("Insert Album %s, contains photo is false", scanned_path)
|
||||||
cache.InsertAlbumPath(scanned_path, false)
|
cache.InsertAlbumPath(scanned_path, false)
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
|
|
Loading…
Reference in New Issue