From 7ab9a12e434540e65f3e57e81166919b90d20c07 Mon Sep 17 00:00:00 2001
From: Kjeldgaard <Kjeldgaard@users.noreply.github.com>
Date: Tue, 23 Feb 2021 15:59:37 +0100
Subject: [PATCH 1/2] 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>
---
 api/scanner/scanner_user.go | 43 +++++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)

diff --git a/api/scanner/scanner_user.go b/api/scanner/scanner_user.go
index 6e10b3a..e4f57ca 100644
--- a/api/scanner/scanner_user.go
+++ b/api/scanner/scanner_user.go
@@ -1,6 +1,7 @@
 package scanner
 
 import (
+	"bufio"
 	"container/list"
 	"fmt"
 	"io/ioutil"
@@ -12,9 +13,32 @@ import (
 	"github.com/photoview/photoview/api/graphql/notification"
 	"github.com/photoview/photoview/api/utils"
 	"github.com/pkg/errors"
+	"github.com/sabhiram/go-gitignore"
 	"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) {
 
 	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 {
 		path   string
 		parent *models.Album
+		ignore []string
 	}
 
 	scanQueue := list.New()
@@ -52,6 +77,7 @@ func findAlbumsForUser(db *gorm.DB, user *models.User, album_cache *AlbumScanner
 			scanQueue.PushBack(scanInfo{
 				path:   album.Path,
 				parent: nil,
+				ignore: nil,
 			})
 		}
 	}
@@ -64,6 +90,7 @@ func findAlbumsForUser(db *gorm.DB, user *models.User, album_cache *AlbumScanner
 
 		albumPath := albumInfo.path
 		albumParent := albumInfo.parent
+		albumIgnore := albumInfo.ignore
 
 		// Read path
 		dirContent, err := ioutil.ReadDir(albumPath)
@@ -72,6 +99,21 @@ func findAlbumsForUser(db *gorm.DB, user *models.User, album_cache *AlbumScanner
 			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
 		var album *models.Album
 
@@ -153,6 +195,7 @@ func findAlbumsForUser(db *gorm.DB, user *models.User, album_cache *AlbumScanner
 				scanQueue.PushBack(scanInfo{
 					path:   subalbumPath,
 					parent: album,
+					ignore: albumIgnore,
 				})
 			}
 		}

From 222a288077b38ac1fd20ac4845d68860bdbe88e3 Mon Sep 17 00:00:00 2001
From: Kjeldgaard <Kjeldgaard@users.noreply.github.com>
Date: Tue, 23 Feb 2021 20:57:05 +0100
Subject: [PATCH 2/2] V2 Ignore Directories plus trailing slash

Updated ignore dirs feature to allow trailing slashes in
.photoviewignore file(s).

Signed-off-by: Kjeldgaard <Kjeldgaard@users.noreply.github.com>
---
 api/scanner/scanner_user.go | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/api/scanner/scanner_user.go b/api/scanner/scanner_user.go
index e4f57ca..b6cc3cf 100644
--- a/api/scanner/scanner_user.go
+++ b/api/scanner/scanner_user.go
@@ -101,7 +101,7 @@ func findAlbumsForUser(db *gorm.DB, user *models.User, album_cache *AlbumScanner
 
 		// Skip this dir if in ignore list
 		ignorePaths := ignore.CompileIgnoreLines(albumIgnore...)
-		if (ignorePaths.MatchesPath(albumPath)) {
+		if (ignorePaths.MatchesPath(albumPath + "/")) {
 			log.Printf("Skip, directroy %s is in ignore file", albumPath)
 			continue
 		}
