aboutsummaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorGabriel Arakaki Giovanini <mail@gabrielgio.me>2022-10-16 19:13:41 +0200
committerGabriel Arakaki Giovanini <mail@gabrielgio.me>2022-10-16 19:16:35 +0200
commiteb1b7d7d9149114eb6b4287b7cb40c49dccfb26e (patch)
tree12f797651abeda9512d56d8922199ae9edb2a293 /main.go
parent98844247a424558939228b82e9b5f28d723c4fe0 (diff)
downloadporg-master.tar.gz
porg-master.tar.bz2
porg-master.zip
feat: Add storage interfaceHEADmaster
With this is easier to interact with storage layers.
Diffstat (limited to 'main.go')
-rw-r--r--main.go97
1 files changed, 51 insertions, 46 deletions
diff --git a/main.go b/main.go
index 1a3c554..7349781 100644
--- a/main.go
+++ b/main.go
@@ -3,52 +3,48 @@ package main
import (
"fmt"
"os"
+ "path"
"path/filepath"
"porg/fileop"
"porg/pipe"
+ "porg/storage"
+ "time"
)
-type SHAInfo struct {
+type File struct {
path string
sha256 string
}
-type File struct {
- Path string
- SHA256 string
- Time string
-}
-
type MoveCommand struct {
src string
dest string
}
-func Calculate(file string) *SHAInfo {
- f, err := os.Open(file)
- if err != nil {
- fmt.Println(err)
- return nil
- }
- defer f.Close()
+func Calculate(s storage.Storage) func(string) (*File, error) {
+ return func(file string) (*File, error) {
+ f, err := s.Get(file)
+ if err != nil {
+ return nil, err
+ }
- sha, err := fileop.CalculateSHA256(f)
- if err != nil {
- fmt.Println(err)
- return nil
- } else {
- return &SHAInfo{path: file, sha256: sha}
+ sha, err := fileop.CalculateSHA256(f)
+ if err != nil {
+ return nil, err
+ } else {
+ return &File{path: file, sha256: sha}, nil
+ }
}
}
-func Move(base string) func(*SHAInfo) *MoveCommand {
- return func(info *SHAInfo) *MoveCommand {
+func Move(base string) func(*File) (*MoveCommand, error) {
+ return func(info *File) (*MoveCommand, error) {
ext := filepath.Ext(info.path)
head := info.sha256[0:2]
tail := info.sha256[2:]
newPath := fmt.Sprintf("%s/%s/%s%s", base, head, tail, ext)
- return &MoveCommand{src: info.path, dest: newPath}
+ return &MoveCommand{src: info.path, dest: newPath}, nil
}
}
@@ -69,32 +65,41 @@ func Delete(path string) {
}
}
-func Apply(move *MoveCommand) {
- dir := filepath.Dir(move.dest)
- os.Mkdir(dir, 0755)
- if err := os.Rename(move.src, move.dest); err != nil {
- fmt.Println(err)
+func Apply(s storage.Storage, d storage.Storage) func(move *MoveCommand) error {
+ return func(move *MoveCommand) error {
+ dir := filepath.Dir(move.dest)
+ d.Mkdir(dir)
+ start := time.Now()
+ exists, err := d.Exists(move.dest)
+ if err != nil {
+ return fmt.Errorf("%s %s", time.Since(start), err.Error())
+ } else if !exists || err != nil {
+ fmt.Println(">>> ", time.Since(start), " ", move.dest)
+ r, _ := s.Get(move.src)
+ d.Put(move.dest, r)
+ } else {
+ fmt.Println("!!! ", time.Since(start), " ", move.src)
+ }
+ return nil
}
-
}
func main() {
- for _, path := range os.Args[1:] {
- info, err := os.Stat(path)
-
- if err != nil || !info.IsDir() {
- continue
- }
-
- fmt.Println("Processing folder")
- fmt.Println(path)
- files := fileop.WalkFolder(path, fileop.File)
- shas := pipe.Proc(files, 4, Calculate)
- cmds := pipe.Proc(shas, 1, Move(path))
- pipe.TailProc(cmds, 4, Apply)
-
- fmt.Println("Deleting empty folders")
- files = fileop.WalkFolder(path, fileop.Folder)
- pipe.TailProc(files, 2, Delete)
+ root := ""
+ username := ""
+ password := ""
+ nextcloud := storage.NewWebDavStorage(root, username, password)
+
+ fs := storage.NewFileSystem()
+ basePath := ""
+ list, _ := fs.List(basePath)
+
+ for _, f := range list {
+ base := path.Base(f)
+ fmt.Println("....", base)
+ files := fs.Walk(f, storage.File)
+ shas := pipe.Proc(files, 10, Calculate(fs))
+ cmds := pipe.Proc(shas, 1, Move("OF/"+base))
+ pipe.TailProc(cmds, 10, Apply(fs, nextcloud))
}
}