From 3451d56ead6e57f503962b876c89284f1fb73a90 Mon Sep 17 00:00:00 2001 From: Gabriel Arakaki Giovanini Date: Sat, 10 Sep 2022 17:33:30 +0200 Subject: ref: Create a storage interface This `Storage` interface will define all the interactions with the storage system. For now I plan to support native file system through go's standard library and Nextcloud through *webdav*. So this is the first step in that direction. --- main.go | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 57 insertions(+), 12 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index f09a545..1a3c554 100644 --- a/main.go +++ b/main.go @@ -13,13 +13,26 @@ type SHAInfo struct { sha256 string } +type File struct { + Path string + SHA256 string + Time string +} + type MoveCommand struct { src string dest string } func Calculate(file string) *SHAInfo { - sha, err := fileop.CalculateSHA256(file) + f, err := os.Open(file) + if err != nil { + fmt.Println(err) + return nil + } + defer f.Close() + + sha, err := fileop.CalculateSHA256(f) if err != nil { fmt.Println(err) return nil @@ -28,15 +41,32 @@ func Calculate(file string) *SHAInfo { } } -func Move(info *SHAInfo) *MoveCommand { +func Move(base string) func(*SHAInfo) *MoveCommand { + return func(info *SHAInfo) *MoveCommand { + + 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} + } +} - base := filepath.Dir(info.path) - 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} +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(move *MoveCommand) { @@ -45,11 +75,26 @@ func Apply(move *MoveCommand) { if err := os.Rename(move.src, move.dest); err != nil { fmt.Println(err) } + } func main() { - files := fileop.WalkFolder("") - shas := pipe.Proc(files, 8, Calculate) - cmds := pipe.Proc(shas, 1, Move) - pipe.TailProc(cmds, 4, Apply) + 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) + } } -- cgit v1.2.3