1
Fork 0

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:
Kjeldgaard 2021-03-10 21:51:36 +01:00
parent 3ad12e3025
commit 8afff70d9a
4 changed files with 53 additions and 56 deletions

View File

@ -17,7 +17,6 @@ type Album struct {
Owners []User `gorm:"many2many:user_albums"`
Path string `gorm:"not null"`
PathHash string `gorm:"unique"`
IgnoreFiles string
}
func (a *Album) FilePath() string {

View File

@ -10,6 +10,7 @@ import (
type AlbumScannerCache struct {
path_contains_photos map[string]bool
photo_types map[string]MediaType
ignore_data map[string][]string
mutex sync.Mutex
}
@ -17,6 +18,7 @@ func MakeAlbumCache() *AlbumScannerCache {
return &AlbumScannerCache{
path_contains_photos: make(map[string]bool),
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
}
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
}

View File

@ -5,8 +5,6 @@ import (
"io/ioutil"
"log"
"path"
"path/filepath"
"strings"
"time"
"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/utils"
"github.com/pkg/errors"
"github.com/sabhiram/go-gitignore"
"gorm.io/gorm"
)
@ -139,30 +138,19 @@ func findMediaForAlbum(album *models.Album, cache *AlbumScannerCache, db *gorm.D
return nil, err
}
// Get file ignores
var ignores []string
if (len(album.IgnoreFiles) > 0) {
ignores = strings.Split(album.IgnoreFiles, ",")
}
// Get ignore data
albumIgnore := ignore.CompileIgnoreLines(*cache.GetAlbumIgnore(album.Path)...)
for _, item := range dirContent {
photoPath := path.Join(album.Path, item.Name())
skipFile := false
for _, ignore := range ignores {
match, _ := filepath.Match(ignore, item.Name())
if (match) {
// 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) {
if !item.IsDir() && isPathMedia(photoPath, cache) {
// Match file against ignore data
if (albumIgnore.MatchesPath(item.Name())) {
log.Printf("File %s ignored\n", item.Name())
continue
}
if !item.IsDir() && isPathMedia(photoPath, cache) {
// Skip the JPEGs that are compressed version of raw files
counterpartFile := scanForRawCounterpartFile(photoPath)
if counterpartFile != nil {

View File

@ -8,7 +8,6 @@ import (
"log"
"os"
"path"
"strings"
"github.com/photoview/photoview/api/graphql/models"
"github.com/photoview/photoview/api/graphql/notification"
@ -18,21 +17,6 @@ import (
"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){
var photoviewIgnore []string
@ -55,20 +39,6 @@ func getPhotoviewIgnore(ignorePath string) ([]string , error){
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) {
if err := user.FillAlbums(db); err != nil {
@ -175,9 +145,11 @@ func findAlbumsForUser(db *gorm.DB, user *models.User, album_cache *AlbumScanner
Title: albumTitle,
ParentAlbumID: albumParentID,
Path: albumPath,
IgnoreFiles: getIgnoreFiles(albumIgnore),
}
// Store album ignore
album_cache.InsertAlbumIgnore(albumPath, albumIgnore)
if err := tx.Create(&album).Error; err != nil {
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
}
}
album.IgnoreFiles = getIgnoreFiles(albumIgnore)
// Update album ignore
album_cache.InsertAlbumIgnore(albumPath, albumIgnore)
}
userAlbums = append(userAlbums, album)
@ -222,7 +196,7 @@ func findAlbumsForUser(db *gorm.DB, user *models.User, album_cache *AlbumScanner
continue
}
if item.IsDir() && directoryContainsPhotos(subalbumPath, album_cache) {
if item.IsDir() && directoryContainsPhotos(subalbumPath, album_cache, albumIgnore) {
scanQueue.PushBack(scanInfo{
path: subalbumPath,
parent: album,
@ -238,7 +212,7 @@ func findAlbumsForUser(db *gorm.DB, user *models.User, album_cache *AlbumScanner
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 {
return *contains_image
@ -256,6 +230,15 @@ func directoryContainsPhotos(rootPath string, cache *AlbumScannerCache) bool {
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)
if err != nil {
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)
} else {
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)
return true
}
@ -277,6 +265,7 @@ func directoryContainsPhotos(rootPath string, cache *AlbumScannerCache) bool {
}
for _, scanned_path := range scanned_directories {
log.Printf("Insert Album %s, contains photo is false", scanned_path)
cache.InsertAlbumPath(scanned_path, false)
}
return false