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{
Key: key,
Name: info.Name(),
IsDir: info.IsDir(),
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
Name string
IsDir bool
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

@ -12,11 +12,11 @@ func ImageUploadRequired(context *piwigo.PiwigoContext, files []string) (bool, e
}
/*
http://pictures.haefelfinger.net/ws.php?format=json
{
"md5sum_list": "d327416a83452b91764ed2888a5630a3,6d5f122e2b98bc1a192850e89fc2ae8c,40bfe8dd8349ccdedd4a939f9191cafa"
}
*/
http://pictures.haefelfinger.net/ws.php?format=json
{
"md5sum_list": "d327416a83452b91764ed2888a5630a3,6d5f122e2b98bc1a192850e89fc2ae8c,40bfe8dd8349ccdedd4a939f9191cafa"
}
*/
return false, nil
}

View File

@ -1,6 +1,5 @@
package picture
/*
http://pictures.haefelfinger.net/ws.php?format=json
{
@ -8,4 +7,4 @@ http://pictures.haefelfinger.net/ws.php?format=json
"original_sum": "1234567",
"position": "1"
}
*/
*/