-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
190 lines (148 loc) · 4.06 KB
/
main.go
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package main
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"time"
"github.com/studio-b12/gowebdav"
)
// function to check if file exists
func doesFileExist(fileName string) bool {
_, error := os.Stat(fileName)
// check if error is "file not exists"
if os.IsNotExist(error) {
fmt.Printf("%v file does not exist\n", fileName)
return false
}
return true
}
func get_local_path(base_dir string) string {
path := "~" + base_dir
if strings.HasPrefix(path, "~/") {
dirname, _ := os.UserHomeDir()
path = filepath.Join(dirname, path[2:])
}
return path
}
func download_file(c *gowebdav.Client, base_dir string, file_name string, server_time time.Time) {
reader, _ := c.ReadStream(base_dir + file_name)
full_path := filepath.Join(get_local_path(base_dir), file_name)
fmt.Println("Downloading new File ", full_path)
f, _ := os.Create(full_path)
defer f.Close()
_, er := io.Copy(f, reader)
if er != nil {
fmt.Println(er)
}
err := os.Chtimes(full_path, server_time, server_time)
if err != nil {
fmt.Println(err)
}
}
func upload_file(c *gowebdav.Client, base_dir string, file_name string) {
full_path := filepath.Join(get_local_path(base_dir), file_name)
fmt.Println("Uploading new File ", full_path)
bytes, _ := os.ReadFile(full_path)
er := c.Write(filepath.Join(base_dir, file_name), bytes, 0644)
if er != nil {
fmt.Println(er)
}
fil, _ := os.Open(full_path)
defer fil.Close()
err := c.WriteStream(filepath.Join(base_dir, file_name), fil, 0644)
if err != nil {
fmt.Println(err)
}
}
func upload_a_local_folder(c *gowebdav.Client, base_dir string, local_folder_name string, local_base_path string) {
fmt.Println("uploading local folder ", base_dir, local_folder_name)
err := c.Mkdir(base_dir+local_folder_name, 0644)
if err != nil {
panic("error creating remote folder")
}
fils, err := os.ReadDir(filepath.Join(local_base_path, local_folder_name))
if err != nil {
panic("error reading local folder" + local_base_path + local_folder_name)
}
for _, file := range fils {
if file.IsDir() {
fmt.Println("-- ", base_dir+local_folder_name)
check_folder(c, base_dir+local_folder_name+"/")
} else {
upload_file(c, base_dir+local_folder_name+"/", file.Name())
}
}
}
func check_folder(c *gowebdav.Client, base_dir string) {
println("checking base dir = ", base_dir)
sync := make(map[string]time.Time)
files, e := c.ReadDir(base_dir)
if e != nil {
fmt.Println(e)
}
path := get_local_path(base_dir)
for _, file := range files {
if file.IsDir() {
check_folder(c, base_dir+file.Name()+"/")
} else {
full_path := filepath.Join(path, file.Name())
if !doesFileExist(full_path) {
download_file(c, base_dir, file.Name(), file.ModTime())
}
sync[base_dir+file.Name()] = file.ModTime()
// fmt.Println(base_dir + file.Name(), file.Size())
// if deu, ok := file.(gowebdav.File); ok {
// // fmt.Println(base_dir+file.Name(), deu.Size(), deu.ETag())
// }
}
}
fils, err := os.ReadDir(path)
if err != nil {
fmt.Println("Creating a folder", path)
er := os.Mkdir(path, os.ModePerm)
if er != nil {
fmt.Println(er)
}
for _, file := range files {
download_file(c, base_dir, file.Name(), file.ModTime())
}
}
for _, file := range fils {
if file.IsDir() {
_, err := c.Stat(filepath.Join(base_dir, file.Name()))
if err != nil {
fmt.Println("cretate a remote dir")
upload_a_local_folder(c, base_dir, file.Name(), path)
}
} else {
full_path := filepath.Join(path, file.Name())
fi, _ := os.Stat(full_path)
md := fi.ModTime()
k := sync[base_dir+file.Name()]
if k.After(md) {
download_file(c, base_dir, file.Name(), k)
}
if k.Before(md) {
upload_file(c, base_dir, file.Name())
}
}
}
}
func main() {
server := os.Getenv("WEBDAV_SERVER")
user := os.Getenv("WEBDAV_USER")
password := os.Getenv("WEBDAV_PASSWORD")
if server == "" {
panic("No server address set")
}
if user == "" {
panic("No server address set")
}
if password == "" {
panic("No server address set")
}
c := gowebdav.NewClient(server, user, password)
check_folder(c, "/notes/")
}