aboutsummaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorGabriel Arakaki Giovanini <mail@gabrielgio.me>2022-09-10 17:33:30 +0200
committerGabriel Arakaki Giovanini <mail@gabrielgio.me>2022-09-10 17:33:30 +0200
commit3451d56ead6e57f503962b876c89284f1fb73a90 (patch)
tree172599f1f3acd77bc918c55403eb78255ced43e6 /main.go
parent544bbeeaf836436305cbed87ae1019511de62535 (diff)
downloadporg-3451d56ead6e57f503962b876c89284f1fb73a90.tar.gz
porg-3451d56ead6e57f503962b876c89284f1fb73a90.tar.bz2
porg-3451d56ead6e57f503962b876c89284f1fb73a90.zip
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.
Diffstat (limited to 'main.go')
-rw-r--r--main.go69
1 files changed, 57 insertions, 12 deletions
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)
+ }
}