From eb1b7d7d9149114eb6b4287b7cb40c49dccfb26e Mon Sep 17 00:00:00 2001 From: Gabriel Arakaki Giovanini Date: Sun, 16 Oct 2022 19:13:41 +0200 Subject: feat: Add storage interface With this is easier to interact with storage layers. --- main.go | 97 ++++++++++++++++++++++++++++++++++------------------------------- 1 file changed, 51 insertions(+), 46 deletions(-) (limited to 'main.go') 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)) } } -- cgit v1.2.3