adds handling for new key generation and configurable extensions
This commit is contained in:
parent
bef050474a
commit
033b5fa53c
@ -11,7 +11,7 @@ func Test_ScanLocalFileStructure_should_find_testfile(t *testing.T) {
|
||||
supportedExtensions := make([]string, 0)
|
||||
supportedExtensions = append(supportedExtensions, "jpg")
|
||||
|
||||
images, err := ScanLocalFileStructure("../../../test/", supportedExtensions, make([]string, 0))
|
||||
images, err := ScanLocalFileStructure("../../../test/", supportedExtensions, make([]string, 0), 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -32,13 +32,47 @@ func Test_ScanLocalFileStructure_should_find_testfile(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func Test_ScanLocalFileStructure_should_find_testfile_with_trimmed_folder(t *testing.T) {
|
||||
supportedExtensions := make([]string, 0)
|
||||
supportedExtensions = append(supportedExtensions, "jpg")
|
||||
|
||||
images, err := ScanLocalFileStructure("../../../test/", supportedExtensions, make([]string, 0), 1)
|
||||
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
|
||||
containsFolder := true
|
||||
for _, img := range images {
|
||||
if img.Name == "testimage.jpg" && img.Key == "root/testimage.jpg" && !img.IsDir {
|
||||
containsTestImage = true
|
||||
}
|
||||
|
||||
if img.IsDir && img.Key == "root" {
|
||||
containsFolder = true
|
||||
}
|
||||
}
|
||||
|
||||
if !containsTestImage {
|
||||
t.Errorf("Did not find the expected testimage.")
|
||||
}
|
||||
if !containsFolder {
|
||||
t.Errorf("Did not find the expected test folder.")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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)
|
||||
images, err := ScanLocalFileStructure("../../../test/", supportedExtensions, ignores, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -52,7 +86,7 @@ func Test_ScanLocalFileStructure_should_not_find_jpg_when_only_png_supported(t *
|
||||
supportedExtensions := make([]string, 0)
|
||||
supportedExtensions = append(supportedExtensions, "png")
|
||||
|
||||
images, err := ScanLocalFileStructure("../../../test/", supportedExtensions, make([]string, 0))
|
||||
images, err := ScanLocalFileStructure("../../../test/", supportedExtensions, make([]string, 0), 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
@ -26,15 +26,15 @@ func (n *FilesystemNode) String() string {
|
||||
return fmt.Sprintf("FilesystemNode: %s", n.Path)
|
||||
}
|
||||
|
||||
func ScanLocalFileStructure(path string, extensions []string, ignoreFolders []string) (map[string]*FilesystemNode, error) {
|
||||
func ScanLocalFileStructure(path string, extensions []string, ignoreDirs []string, dirSuffixToSkip int) (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{}{}
|
||||
ignoreDirsMap := make(map[string]struct{}, len(ignoreDirs))
|
||||
for _, ignoredFolder := range ignoreDirs {
|
||||
ignoreDirsMap[strings.ToLower(ignoredFolder)] = struct{}{}
|
||||
}
|
||||
|
||||
extensionsMap := make(map[string]struct{}, len(extensions))
|
||||
@ -42,6 +42,12 @@ func ScanLocalFileStructure(path string, extensions []string, ignoreFolders []st
|
||||
extensionsMap["."+strings.ToLower(extension)] = struct{}{}
|
||||
}
|
||||
|
||||
if len(extensionsMap) == 0 {
|
||||
logrus.Debug("No extensions specified, adding jpg and png")
|
||||
extensionsMap[".jpg"] = struct{}{}
|
||||
extensionsMap[".png"] = struct{}{}
|
||||
}
|
||||
|
||||
logrus.Infof("Scanning %s for images...", fullPathRoot)
|
||||
|
||||
fileMap := make(map[string]*FilesystemNode)
|
||||
@ -59,8 +65,8 @@ func ScanLocalFileStructure(path string, extensions []string, ignoreFolders []st
|
||||
return nil
|
||||
}
|
||||
|
||||
_, FolderIsIgnored := ignoredDirectoriesMap[strings.ToLower(info.Name())]
|
||||
if FolderIsIgnored && info.IsDir() {
|
||||
_, dirIgnored := ignoreDirsMap[strings.ToLower(info.Name())]
|
||||
if dirIgnored && info.IsDir() {
|
||||
logrus.Tracef("Skipping ignored directory %s", path)
|
||||
return filepath.SkipDir
|
||||
}
|
||||
@ -71,8 +77,7 @@ func ScanLocalFileStructure(path string, extensions []string, ignoreFolders []st
|
||||
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)
|
||||
key := buildKey(path, info, fullPathReplace, dirSuffixToSkip)
|
||||
|
||||
fileMap[path] = &FilesystemNode{
|
||||
Key: key,
|
||||
@ -99,3 +104,24 @@ func ScanLocalFileStructure(path string, extensions []string, ignoreFolders []st
|
||||
|
||||
return fileMap, nil
|
||||
}
|
||||
|
||||
func buildKey(path string, info os.FileInfo, fullPathReplace string, dirSuffixToSkip int) string {
|
||||
if info.IsDir() {
|
||||
return trimPathForKey(path, fullPathReplace, dirSuffixToSkip)
|
||||
}
|
||||
fileName := filepath.Base(path)
|
||||
directoryName := filepath.Dir(path)
|
||||
cleanDir := trimPathForKey(directoryName, fullPathReplace, dirSuffixToSkip)
|
||||
return filepath.Join(cleanDir, fileName)
|
||||
}
|
||||
|
||||
func trimPathForKey(path string, fullPathReplace string, dirSuffixToSkip int) string {
|
||||
trimmedPath := strings.Replace(path, fullPathReplace, "", 1)
|
||||
for i := 0; i < dirSuffixToSkip; i++ {
|
||||
trimmedPath = filepath.Clean(strings.TrimSuffix(trimmedPath, filepath.Base(trimmedPath)))
|
||||
}
|
||||
if trimmedPath == "." {
|
||||
return "root"
|
||||
}
|
||||
return trimmedPath
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user