adds support for file exteions and directory ignores to the local scanner

still some todos left
This commit is contained in:
Philipp Häfelfinger 2020-04-14 01:08:07 +02:00
parent 3a1ba53162
commit e07a12409b
3 changed files with 80 additions and 10 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2019 Philipp Haefelfinger (http://www.haefelfinger.ch/). All Rights Reserved.
* Copyright (C) 2020 Philipp Haefelfinger (http://www.haefelfinger.ch/). All Rights Reserved.
* This application is licensed under GPLv2. See the LICENSE file in the root directory of the project.
*/
@ -36,7 +36,12 @@ func Run() {
logErrorAndExit(err, 2)
}
filesystemNodes, err := localFileStructure.ScanLocalFileStructure(context.localRootPath)
//TODO: make params here as flags
supportedExtensions := make([]string, 0)
supportedExtensions = append(supportedExtensions, "jpg")
supportedExtensions = append(supportedExtensions, "png")
filesystemNodes, err := localFileStructure.ScanLocalFileStructure(context.localRootPath, supportedExtensions, make([]string,0))
if err != nil {
logErrorAndExit(err, 3)
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2019 Philipp Haefelfinger (http://www.haefelfinger.ch/). All Rights Reserved.
* Copyright (C) 2020 Philipp Haefelfinger (http://www.haefelfinger.ch/). All Rights Reserved.
* This application is licensed under GPLv2. See the LICENSE file in the root directory of the project.
*/
@ -8,20 +8,67 @@ package localFileStructure
import "testing"
func Test_ScanLocalFileStructure_should_find_testfile(t *testing.T) {
supportedExtensions := make([]string, 0)
supportedExtensions = append(supportedExtensions, "jpg")
images, err := ScanLocalFileStructure("../../../test/")
images, err := ScanLocalFileStructure("../../../test/", supportedExtensions, make([]string, 0))
if err != nil {
t.Fatal(err)
}
if len(images) != 2 { // 1x folder, 1x image
t.Error("Did not find expected testfiles. Expected at least one!")
}
containsTestImage := false
for _, img := range images {
if img.Name == "testimage.jpg" {
containsTestImage = true
}
}
if !containsTestImage {
t.Errorf("Did not find the expected testimage.")
}
}
func Test_ScanLocalFileStructure_should_ignore_test_directory(t *testing.T) {
supportedExtensions := make([]string, 0)
supportedExtensions = append(supportedExtensions, "jpg")
ignores := make([]string, 0)
ignores = append(ignores, "images")
images, err := ScanLocalFileStructure("../../../test/", supportedExtensions, ignores)
if err != nil {
t.Fatal(err)
}
if len(images) != 0 {
t.Error("Did find expected testfiles. Expected no files as test folder is excluded!")
}
}
func Test_ScanLocalFileStructure_should_not_find_jpg_when_only_png_supported(t *testing.T) {
supportedExtensions := make([]string, 0)
supportedExtensions = append(supportedExtensions, "png")
images, err := ScanLocalFileStructure("../../../test/", supportedExtensions, make([]string, 0))
if err != nil {
t.Fatal(err)
}
if len(images) != 1 {
t.Error("Did not find expected testfiles. Expected at least one!")
t.Error("Did find expected testfiles. Expected no files as extension is not supported!")
}
containsTestImage := false
for _, img := range images {
if img.Name != "testimage.jpg" {
t.Errorf("Did not find the expected testimage.")
if img.Name == "testimage.jpg" {
containsTestImage = true
}
}
if containsTestImage {
t.Errorf("Did find the testimage. This should not happen as png is searched but jpg found")
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2019 Philipp Haefelfinger (http://www.haefelfinger.ch/). All Rights Reserved.
* Copyright (C) 2020 Philipp Haefelfinger (http://www.haefelfinger.ch/). All Rights Reserved.
* This application is licensed under GPLv2. See the LICENSE file in the root directory of the project.
*/
@ -26,12 +26,22 @@ func (n *FilesystemNode) String() string {
return fmt.Sprintf("FilesystemNode: %s", n.Path)
}
func ScanLocalFileStructure(path string) (map[string]*FilesystemNode, error) {
func ScanLocalFileStructure(path string, extensions []string, ignoreFolders []string) (map[string]*FilesystemNode, error) {
fullPathRoot, err := filepath.Abs(path)
if err != nil {
return nil, err
}
ignoredDirectoriesMap := make(map[string]struct{}, len(ignoreFolders))
for _, ignoredFolder := range ignoreFolders {
ignoredDirectoriesMap[strings.ToLower(ignoredFolder)] = struct{}{}
}
extensionsMap := make(map[string]struct{}, len(extensions))
for _, extension := range extensions {
extensionsMap["."+strings.ToLower(extension)] = struct{}{}
}
logrus.Infof("Scanning %s for images...", fullPathRoot)
fileMap := make(map[string]*FilesystemNode)
@ -49,11 +59,19 @@ func ScanLocalFileStructure(path string) (map[string]*FilesystemNode, error) {
return nil
}
_, FolderIsIgnored := ignoredDirectoriesMap[strings.ToLower(info.Name())]
if FolderIsIgnored && info.IsDir() {
logrus.Tracef("Skipping ignored directory %s", path)
return filepath.SkipDir
}
extension := strings.ToLower(filepath.Ext(path))
if extension != ".jpg" && extension != ".png" && !info.IsDir() {
_, extensionSupported := extensionsMap[extension]
if !extensionSupported && !info.IsDir() {
return nil
}
//TODO: Add code to strip directories at the end of the path (e.g. /rootdir/eventname/png/file.png -> eventname/file.png
key := strings.Replace(path, fullPathReplace, "", 1)
fileMap[path] = &FilesystemNode{