aboutsummaryrefslogtreecommitdiff
path: root/storage.go
diff options
context:
space:
mode:
authorGabriel Arakaki Giovanini <mail@gabrielgio.me>2022-08-28 17:03:38 +0200
committerGabriel Arakaki Giovanini <mail@gabrielgio.me>2022-08-28 17:07:40 +0200
commit544bbeeaf836436305cbed87ae1019511de62535 (patch)
tree8c7b020bba75d4d850d4a1d538e7bc7db2f01c33 /storage.go
downloadporg-544bbeeaf836436305cbed87ae1019511de62535.tar.gz
porg-544bbeeaf836436305cbed87ae1019511de62535.tar.bz2
porg-544bbeeaf836436305cbed87ae1019511de62535.zip
feat: Add init source code
For now only `pipe` is feature complete. The order module needs to be changed a lot.
Diffstat (limited to 'storage.go')
-rw-r--r--storage.go68
1 files changed, 68 insertions, 0 deletions
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
+}