package main import ( "fmt" "os" "path" "path/filepath" "porg/fileop" "porg/pipe" "porg/storage" "time" ) type File struct { path string sha256 string } type MoveCommand struct { src string dest string } 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 { return nil, err } else { return &File{path: file, sha256: sha}, nil } } } 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}, nil } } func Delete(path string) { empty, err := fileop.IsEmpty(path) if err != nil { fmt.Printf("Error reading %s: %s\n", path, err.Error()) } if empty { err := os.Remove(path) if err != nil { fmt.Printf("Error deleting %s: %s\n", path, err.Error()) } else { fmt.Printf("Folder deleted %s\n", path) } } } 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() { 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)) } }