added md5 sums to the local file upload indexing

This commit is contained in:
Philipp Häfelfinger 2019-02-27 22:11:47 +01:00
parent 643729af1f
commit 2808c85147
8 changed files with 98 additions and 34 deletions

View File

@ -44,7 +44,7 @@ func Run() {
logErrorAndExit(err, 5)
}
err = synchronizeImages(filesystemNodes, categories)
err = synchronizeImages(context, filesystemNodes, categories)
if err != nil {
logErrorAndExit(err, 6)
}

View File

@ -9,7 +9,10 @@ import (
func synchronizeImages(context *AppContext, fileSystem map[string]*localFileStructure.FilesystemNode, existingCategories map[string]*category.PiwigoCategory) error {
imageFiles := getImageList(fileSystem)
imageFiles, err := localFileStructure.GetImageList(fileSystem)
if err != nil {
return err
}
missingFiles := findMissingImages(imageFiles)
uploadImages(missingFiles)
@ -17,7 +20,7 @@ func synchronizeImages(context *AppContext, fileSystem map[string]*localFileStru
return errors.New("synchronizeImages: NOT IMPLEMENTED")
}
func findMissingImages(imageFiles []string) []string {
func findMissingImages(imageFiles []*localFileStructure.ImageNode) []string {
logrus.Warnln("Finding missing images (NotImplemented)")
@ -27,15 +30,3 @@ func findMissingImages(imageFiles []string) []string {
func uploadImages(missingFiles []string) {
logrus.Warnln("Uploading missing images (NotImplemented)")
}
func getImageList(fileSystem map[string]*localFileStructure.FilesystemNode) []string {
imageFiles := []string{}
for _, file := range fileSystem {
if !file.IsDir {
imageFiles = append(imageFiles, file.Key)
}
}
return imageFiles
}

View File

@ -0,0 +1,30 @@
package localFileStructure
import (
"crypto/md5"
"fmt"
"github.com/sirupsen/logrus"
"io"
"os"
)
func calculateFileCheckSums(filePath string) (string, error) {
f, err := os.Open(filePath)
if err != nil {
logrus.Errorf("Could not open file %s", filePath)
return "", err
}
defer f.Close()
h := md5.New()
if _, err := io.Copy(h, f); err != nil {
logrus.Errorf("Could calculate md5 sum of file %s", filePath)
return "", err
}
md5sum := fmt.Sprintf("%x", md5.Sum(nil))
logrus.Tracef("Calculated md5 sum of %s - %s", filePath, md5sum)
return md5sum, nil
}

View File

@ -21,19 +21,21 @@ func ScanLocalFileStructure(path string) (map[string]*FilesystemNode, error) {
numberOfDirectories := 0
numberOfImages := 0
err = filepath.Walk(fullPathRoot, func(p string, info os.FileInfo, err error) error {
if fullPathRoot == p {
err = filepath.Walk(fullPathRoot, func(path string, info os.FileInfo, err error) error {
if fullPathRoot == path {
return nil
}
//TODO: Only allow jpg and png files here
key := strings.Replace(p, fullPathReplace, "", 1)
key := strings.Replace(path, fullPathReplace, "", 1)
fileMap[p] = &FilesystemNode{
fileMap[path] = &FilesystemNode{
Key: key,
Path: path,
Name: info.Name(),
IsDir: info.IsDir(),
ModTime: info.ModTime(),
}
if info.IsDir() {

View File

@ -0,0 +1,31 @@
package localFileStructure
import (
"github.com/sirupsen/logrus"
"time"
)
func GetImageList(fileSystem map[string]*FilesystemNode) ([]*ImageNode, error) {
imageFiles := []*ImageNode{}
for _, file := range fileSystem {
if file.IsDir {
continue
}
md5sum, err := calculateFileCheckSums(file.Path)
if err != nil {
return nil, err
}
logrus.Debugf("Image %s - %s - %s", md5sum, file.ModTime.Format(time.RFC3339), file.Path)
imageFiles = append(imageFiles, &ImageNode{
Path: file.Path,
ModTime: file.ModTime,
Md5Sum: md5sum,
})
}
return imageFiles, nil
}

View File

@ -1,13 +1,24 @@
package localFileStructure
import "fmt"
import (
"fmt"
"time"
)
type FilesystemNode struct {
Key string
Path string
Name string
IsDir bool
ModTime time.Time
}
func (n *FilesystemNode) String() string {
return fmt.Sprintf("FilesystemNode: %s", n.Key)
return fmt.Sprintf("FilesystemNode: %s", n.Path)
}
type ImageNode struct {
Path string
ModTime time.Time
Md5Sum string
}

View File

@ -1,6 +1,5 @@
package picture
/*
http://pictures.haefelfinger.net/ws.php?format=json
{