added md5 sums to the local file upload indexing
This commit is contained in:
parent
643729af1f
commit
2808c85147
@ -44,7 +44,7 @@ func Run() {
|
|||||||
logErrorAndExit(err, 5)
|
logErrorAndExit(err, 5)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = synchronizeImages(filesystemNodes, categories)
|
err = synchronizeImages(context, filesystemNodes, categories)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logErrorAndExit(err, 6)
|
logErrorAndExit(err, 6)
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,10 @@ import (
|
|||||||
|
|
||||||
func synchronizeImages(context *AppContext, fileSystem map[string]*localFileStructure.FilesystemNode, existingCategories map[string]*category.PiwigoCategory) error {
|
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)
|
missingFiles := findMissingImages(imageFiles)
|
||||||
uploadImages(missingFiles)
|
uploadImages(missingFiles)
|
||||||
@ -17,7 +20,7 @@ func synchronizeImages(context *AppContext, fileSystem map[string]*localFileStru
|
|||||||
return errors.New("synchronizeImages: NOT IMPLEMENTED")
|
return errors.New("synchronizeImages: NOT IMPLEMENTED")
|
||||||
}
|
}
|
||||||
|
|
||||||
func findMissingImages(imageFiles []string) []string {
|
func findMissingImages(imageFiles []*localFileStructure.ImageNode) []string {
|
||||||
|
|
||||||
logrus.Warnln("Finding missing images (NotImplemented)")
|
logrus.Warnln("Finding missing images (NotImplemented)")
|
||||||
|
|
||||||
@ -27,15 +30,3 @@ func findMissingImages(imageFiles []string) []string {
|
|||||||
func uploadImages(missingFiles []string) {
|
func uploadImages(missingFiles []string) {
|
||||||
logrus.Warnln("Uploading missing images (NotImplemented)")
|
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
|
|
||||||
}
|
|
||||||
|
30
internal/pkg/localFileStructure/checksumCalculator.go
Normal file
30
internal/pkg/localFileStructure/checksumCalculator.go
Normal 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
|
||||||
|
}
|
@ -21,19 +21,21 @@ func ScanLocalFileStructure(path string) (map[string]*FilesystemNode, error) {
|
|||||||
numberOfDirectories := 0
|
numberOfDirectories := 0
|
||||||
numberOfImages := 0
|
numberOfImages := 0
|
||||||
|
|
||||||
err = filepath.Walk(fullPathRoot, func(p string, info os.FileInfo, err error) error {
|
err = filepath.Walk(fullPathRoot, func(path string, info os.FileInfo, err error) error {
|
||||||
if fullPathRoot == p {
|
if fullPathRoot == path {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: Only allow jpg and png files here
|
//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,
|
Key: key,
|
||||||
|
Path: path,
|
||||||
Name: info.Name(),
|
Name: info.Name(),
|
||||||
IsDir: info.IsDir(),
|
IsDir: info.IsDir(),
|
||||||
|
ModTime: info.ModTime(),
|
||||||
}
|
}
|
||||||
|
|
||||||
if info.IsDir() {
|
if info.IsDir() {
|
||||||
|
31
internal/pkg/localFileStructure/imageList.go
Normal file
31
internal/pkg/localFileStructure/imageList.go
Normal 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
|
||||||
|
}
|
@ -1,13 +1,24 @@
|
|||||||
package localFileStructure
|
package localFileStructure
|
||||||
|
|
||||||
import "fmt"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
type FilesystemNode struct {
|
type FilesystemNode struct {
|
||||||
Key string
|
Key string
|
||||||
|
Path string
|
||||||
Name string
|
Name string
|
||||||
IsDir bool
|
IsDir bool
|
||||||
|
ModTime time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *FilesystemNode) String() string {
|
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
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package picture
|
package picture
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
http://pictures.haefelfinger.net/ws.php?format=json
|
http://pictures.haefelfinger.net/ws.php?format=json
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user