2019-02-23 00:58:32 +01:00
|
|
|
package localFileStructure
|
|
|
|
|
|
|
|
import (
|
2019-02-25 23:58:44 +01:00
|
|
|
"fmt"
|
2019-02-25 23:36:18 +01:00
|
|
|
"github.com/sirupsen/logrus"
|
2019-02-23 00:58:32 +01:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2019-02-25 23:11:09 +01:00
|
|
|
"strings"
|
2019-02-23 00:58:32 +01:00
|
|
|
)
|
|
|
|
|
2019-02-25 23:11:09 +01:00
|
|
|
func ScanLocalFileStructure(path string) (map[string]*FilesystemNode, error) {
|
2019-02-25 23:58:44 +01:00
|
|
|
fullPathRoot, err := filepath.Abs(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-02-25 23:11:09 +01:00
|
|
|
|
2019-02-25 23:58:44 +01:00
|
|
|
logrus.Infof("Scanning %s for images...", fullPathRoot)
|
2019-02-25 23:36:18 +01:00
|
|
|
|
2019-02-25 23:58:44 +01:00
|
|
|
fileMap := make(map[string]*FilesystemNode)
|
2019-02-26 22:50:33 +01:00
|
|
|
fullPathReplace := fmt.Sprintf("%s%c", fullPathRoot, os.PathSeparator)
|
2019-02-25 23:36:18 +01:00
|
|
|
numberOfDirectories := 0
|
|
|
|
numberOfImages := 0
|
2019-02-23 00:58:32 +01:00
|
|
|
|
2019-02-27 22:11:47 +01:00
|
|
|
err = filepath.Walk(fullPathRoot, func(path string, info os.FileInfo, err error) error {
|
|
|
|
if fullPathRoot == path {
|
2019-02-23 00:58:32 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: Only allow jpg and png files here
|
|
|
|
|
2019-02-27 22:11:47 +01:00
|
|
|
key := strings.Replace(path, fullPathReplace, "", 1)
|
2019-02-25 23:11:09 +01:00
|
|
|
|
2019-02-27 22:11:47 +01:00
|
|
|
fileMap[path] = &FilesystemNode{
|
|
|
|
Key: key,
|
|
|
|
Path: path,
|
|
|
|
Name: info.Name(),
|
|
|
|
IsDir: info.IsDir(),
|
|
|
|
ModTime: info.ModTime(),
|
2019-02-23 00:58:32 +01:00
|
|
|
}
|
2019-02-25 23:36:18 +01:00
|
|
|
|
|
|
|
if info.IsDir() {
|
|
|
|
numberOfDirectories += 1
|
|
|
|
} else {
|
|
|
|
numberOfImages += 1
|
|
|
|
}
|
|
|
|
|
2019-02-23 22:02:12 +01:00
|
|
|
return nil
|
2019-02-23 00:58:32 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
2019-02-24 21:38:28 +01:00
|
|
|
return nil, err
|
2019-02-23 00:58:32 +01:00
|
|
|
}
|
|
|
|
|
2019-02-25 23:36:18 +01:00
|
|
|
logrus.Infof("Found %d directories and %d images on the local filesystem", numberOfDirectories, numberOfImages)
|
|
|
|
|
2019-02-24 21:38:28 +01:00
|
|
|
return fileMap, nil
|
2019-02-23 00:58:32 +01:00
|
|
|
}
|