1
Fork 0

V2 Ignore Directories

Looks for .photoviewignore file in all root dirs and potential subdirs.
Subdirs inherit the ignore rules from parent dir. If a .photoviewignore
file is found in a subdir, the new ignore rules are appended to the
existing ignore rules.

Ignore match engine is from 'github.com/sabhiram/go-gitignore'.

Signed-off-by: Kjeldgaard <Kjeldgaard@users.noreply.github.com>
This commit is contained in:
Kjeldgaard 2021-02-23 15:59:37 +01:00
parent f3ccc60e26
commit 7ab9a12e43
1 changed files with 43 additions and 0 deletions

View File

@ -1,6 +1,7 @@
package scanner package scanner
import ( import (
"bufio"
"container/list" "container/list"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
@ -12,9 +13,32 @@ import (
"github.com/photoview/photoview/api/graphql/notification" "github.com/photoview/photoview/api/graphql/notification"
"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"
) )
func getPhotoviewIgnore(ignorePath string) ([]string , error){
var photoviewIgnore []string
// Open .photoviewignore file, if exists
photoviewIgnoreFile, err := os.Open(path.Join(ignorePath, ".photoviewignore"))
if err != nil {
return photoviewIgnore, err
}
// Close file on exit
defer photoviewIgnoreFile.Close()
// Read and save .photoviewignore data
scanner := bufio.NewScanner(photoviewIgnoreFile)
for scanner.Scan() {
photoviewIgnore = append(photoviewIgnore, scanner.Text())
log.Printf("Ignore found: %s", scanner.Text())
}
return photoviewIgnore, scanner.Err()
}
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 {
@ -36,6 +60,7 @@ func findAlbumsForUser(db *gorm.DB, user *models.User, album_cache *AlbumScanner
type scanInfo struct { type scanInfo struct {
path string path string
parent *models.Album parent *models.Album
ignore []string
} }
scanQueue := list.New() scanQueue := list.New()
@ -52,6 +77,7 @@ func findAlbumsForUser(db *gorm.DB, user *models.User, album_cache *AlbumScanner
scanQueue.PushBack(scanInfo{ scanQueue.PushBack(scanInfo{
path: album.Path, path: album.Path,
parent: nil, parent: nil,
ignore: nil,
}) })
} }
} }
@ -64,6 +90,7 @@ func findAlbumsForUser(db *gorm.DB, user *models.User, album_cache *AlbumScanner
albumPath := albumInfo.path albumPath := albumInfo.path
albumParent := albumInfo.parent albumParent := albumInfo.parent
albumIgnore := albumInfo.ignore
// Read path // Read path
dirContent, err := ioutil.ReadDir(albumPath) dirContent, err := ioutil.ReadDir(albumPath)
@ -72,6 +99,21 @@ func findAlbumsForUser(db *gorm.DB, user *models.User, album_cache *AlbumScanner
continue continue
} }
// Skip this dir if in ignore list
ignorePaths := ignore.CompileIgnoreLines(albumIgnore...)
if (ignorePaths.MatchesPath(albumPath)) {
log.Printf("Skip, directroy %s is in ignore file", albumPath)
continue
}
// Update ignore dir list
photoviewIgnore, err := getPhotoviewIgnore(albumPath)
if err != nil {
log.Printf("Failed to get ignore file, err = %s", err)
} else {
albumIgnore = append(albumIgnore, photoviewIgnore...)
}
// Will become new album or album from db // Will become new album or album from db
var album *models.Album var album *models.Album
@ -153,6 +195,7 @@ func findAlbumsForUser(db *gorm.DB, user *models.User, album_cache *AlbumScanner
scanQueue.PushBack(scanInfo{ scanQueue.PushBack(scanInfo{
path: subalbumPath, path: subalbumPath,
parent: album, parent: album,
ignore: albumIgnore,
}) })
} }
} }