This repository was archived by the owner on Apr 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsync.go
More file actions
158 lines (137 loc) · 3.37 KB
/
sync.go
File metadata and controls
158 lines (137 loc) · 3.37 KB
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
package main
import (
"github.com/ym/oss-aliyun-go"
"io/ioutil"
"log"
"mime"
"os"
"path/filepath"
"strings"
)
var (
a oss.Auth
o *oss.OSS
b *oss.Bucket
err error
)
const (
DefaultContentType = "application/octet-stream"
)
func syncFiles() {
a = oss.Auth{config.accessId, config.accessKey}
o = &oss.OSS{a, config.endpoint, config.endpoint}
b = o.Bucket(config.bucket)
remoteFiles, _ := getRemoteFiles()
localFiles, _ := getLocalFiles()
for path, file := range localFiles {
fn := config.prefix + strings.TrimLeft(strings.TrimPrefix(path, config.source), "/")
if remote, ok := remoteFiles[fn]; ok {
if file.Mode().IsDir() == false {
data, err := getFile(path)
if err == nil {
hash := hashBytes(data)
if compareHash(hash, remote.ETag) == false {
log.Printf("Remote file %s existing, etag %s != %s.\n", fn, remote.ETag, hash)
putFile(path, fn, data)
}
}
}
} else {
if file.Mode().IsDir() == true {
putDirectory(fn)
} else {
data, err := getFile(path)
if err == nil {
putFile(path, fn, data)
}
}
}
}
if config.deleteIfNotFound {
for path, _ := range remoteFiles {
fn := strings.TrimLeft(strings.TrimPrefix(path, config.prefix), "/")
local := config.source + "/" + fn
remote := config.prefix + fn
if _, ok := localFiles[local]; ok == false {
log.Printf("Remove remote file %s.\n", remote)
b.Del(remote)
}
}
}
}
func putDirectory(filename string) {
if filename == "" {
return
}
data := []byte("")
err := b.Put(filename, data, "text/plain", oss.Private)
if err != nil {
log.Printf("Unable to create directory %s: %s.\n", filename, err.Error())
} else {
log.Printf("Created directory %s.\n", filename)
}
}
func getFile(path string) ([]byte, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
log.Printf("Unable to read local file %s: %s.\n", path, err.Error())
}
return data, err
}
func putFile(path string, filename string, data []byte) {
// get content type
filetype := mime.TypeByExtension(filepath.Ext(path))
if filetype == "" {
log.Printf("Unable to detect content type of file %s, using %s.\n", filename, DefaultContentType)
filetype = DefaultContentType
}
err = b.Put(filename, data, filetype, oss.Private)
if err != nil {
log.Printf("Unable to upload file %s: %s.\n", filename, err.Error())
} else {
log.Printf("Uploaded file: %s ... ", filename)
}
}
func getRemoteFiles() (files map[string]oss.ContentInfo, err error) {
var (
marker = ""
max = 512
get func(string) error
)
files = make(map[string]oss.ContentInfo)
get = func(prefix string) error {
remoteFiles, err := b.List(prefix, "/", marker, max)
if err != nil {
return err
}
for _, p := range remoteFiles.CommonPrefixes {
marker = ""
get(p)
}
for _, file := range remoteFiles.Contents {
files[file.Key] = file
}
if remoteFiles.IsTruncated {
marker = remoteFiles.NextMarker
get(prefix)
}
return nil
}
err = get(config.prefix)
if err != nil {
return files, err
}
return files, nil
}
func getLocalFiles() (files map[string]os.FileInfo, err error) {
files = make(map[string]os.FileInfo)
err = filepath.Walk(config.source, func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Printf("%s while looking up %s", err.Error(), path)
return err
}
files[path] = info
return nil
})
return files, err
}