From 544bbeeaf836436305cbed87ae1019511de62535 Mon Sep 17 00:00:00 2001 From: Gabriel Arakaki Giovanini Date: Sun, 28 Aug 2022 17:03:38 +0200 Subject: feat: Add init source code For now only `pipe` is feature complete. The order module needs to be changed a lot. --- storage.go | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 storage.go (limited to 'storage.go') diff --git a/storage.go b/storage.go new file mode 100644 index 0000000..28637da --- /dev/null +++ b/storage.go @@ -0,0 +1,68 @@ +package main + +import ( + "crypto/sha256" + "fmt" + "io" + "os" + + "github.com/barasher/go-exiftool" +) + +type File struct { + Path string + SHA256 string + Time string +} + +type Storage interface { + Upsert(file *File) error + LoadByPath(path string) *File +} + +func NewFile(path string) *File { + return &File{Path: path} +} + +func (file *File) CalculateSHA256() error { + f, err := os.Open(file.Path) + if err != nil { + return err + } + defer f.Close() + + h := sha256.New() + if _, err := io.Copy(h, f); err != nil { + return err + } + + file.SHA256 = fmt.Sprintf("%x", h.Sum(nil)) + + return nil +} + +func (file *File) SetTime() error { + + et, err := exiftool.NewExiftool() + if err != nil { + return err + } + defer et.Close() + + fileInfos := et.ExtractMetadata(file.Path) + + for _, fileInfo := range fileInfos { + if fileInfo.Err != nil { + fmt.Printf("Error concerning %v: %v\n", fileInfo.File, fileInfo.Err) + continue + } + + v, ok := fileInfo.Fields["DateTimeOriginal"] + if ok { + t, _ := v.(string) + file.Time = t + } + } + + return nil +} -- cgit v1.2.3