aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: 7349781ece6e97e8699a2575e3facfa39e29a7cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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))
	}
}