/
main.go
371 lines (321 loc) · 9.16 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package main
import (
"crypto/md5"
"encoding/gob"
"errors"
"flag"
"fmt"
"hash/fnv"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"net/url"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"
"code.google.com/p/goauth2/oauth"
drive "code.google.com/p/google-api-go-client/drive/v2"
)
var config = &oauth.Config{
ClientId: "", // Set by --clientid or --clientid_file
ClientSecret: "", // Set by --secret or --secret_file
Scope: "", // filled in per-API
AuthURL: "https://accounts.google.com/o/oauth2/auth",
TokenURL: "https://accounts.google.com/o/oauth2/token",
}
// Flags
var (
clientId = flag.String("clientid", "", "OAuth Client ID. If non-empty, overrides --clientid_file")
clientIdFile = flag.String("clientid_file", "clientid.dat",
"Name of a file containing just the project's OAuth Client ID from https://code.google.com/apis/console/")
secret = flag.String("secret", "", "OAuth Client Secret. If non-empty, overrides --secret_file")
secretFile = flag.String("secret_file", "clientsecret.dat",
"Name of a file containing just the project's OAuth Client Secret from https://code.google.com/apis/console/")
cacheToken = flag.Bool("cachetoken", true, "cache the OAuth token")
debug = flag.Bool("debug", false, "show HTTP traffic")
driveFolder = flag.String("drivefolder", "", "The name of a folder on Googel Drive")
localFolder = flag.String("localfolder", "", "The folder on the local file system")
)
func getDriveFilesByFolder(service *drive.Service, folderName string) (map[string]*drive.File, string) {
query := fmt.Sprintf("mimeType = 'application/vnd.google-apps.folder' and title = '%s'", folderName)
folderList, err := service.Files.List().Q(query).Do()
log.Printf("Got folderList, err: %#v, %v", folderList, err)
if len(folderList.Items) != 1 {
log.Fatalf("Number of folders: %d != 1 for %s on Google Drive", len(folderList.Items), *driveFolder)
}
files := make(map[string]*drive.File)
folderId := folderList.Items[0].Id
query = fmt.Sprintf("'%s' in parents", folderId)
fileList, err := service.Files.List().Q(query).Do()
log.Printf("Got fileList, err: %#v, %v", fileList, err)
for _, file := range fileList.Items {
key := fmt.Sprintf("%s:%s", file.Title, file.Md5Checksum)
files[key] = file
}
doPaging := true
for doPaging && fileList.NextPageToken != "" {
log.Printf("Going to the next page")
fileList, err = service.Files.List().Q(query).PageToken(fileList.NextPageToken).Do()
for _, file := range fileList.Items {
key := fmt.Sprintf("%s:%s", file.Title, file.Md5Checksum)
files[key] = file
}
}
return files, folderId
}
type File struct {
filename string
base string
md5 string
}
func getLocalFiles(folderName string) map[string]File {
files := make(map[string]File)
glob := filepath.Join(folderName, "*")
filenames, _ := filepath.Glob(glob)
for _, filename := range filenames {
fileInfo, err := os.Stat(filename)
if err != nil {
log.Fatalf("Failed to stat %s", filename)
}
if fileInfo.IsDir() || strings.HasPrefix(fileInfo.Name(), ".") {
continue
}
file, err := os.Open(filename)
if err != nil {
log.Fatalf("Failed to open %s", filename)
}
md5er := md5.New()
io.Copy(md5er, file)
hash := fmt.Sprintf("%x", md5er.Sum(nil))
file.Close()
base := filepath.Base(filename)
key := fmt.Sprintf("%s:%s", base, hash)
files[key] = File{filename, base, hash}
}
return files
}
func mergeFolders(driveFiles map[string]*drive.File, localFiles map[string]File) []File {
var remote []*drive.File
var local []File
synced := make(map[string]bool)
for key, file := range driveFiles {
_, ok := localFiles[key]
if !ok {
remote = append(remote, file)
} else {
synced[key] = true
}
}
for key, file := range localFiles {
_, ok := driveFiles[key]
if !ok {
local = append(local, file)
} else {
synced[key] = true
}
}
fmt.Printf("%v files are synced correctly\n", len(synced))
if len(remote) > 0 {
fmt.Printf("%v files are visible on remote but not local\n", len(remote))
for _, file := range remote {
fmt.Printf("%v\t%v\t%v\t%v\t%v\n",
file.Title,
file.FileSize,
file.OwnerNames,
file.Md5Checksum,
file.Id,
)
}
}
if len(local) > 0 {
fmt.Printf("%v files have not been synched\n", len(local))
for _, file := range local {
fmt.Printf("%v\t%v\n",
file.filename,
file.md5,
)
}
}
return local
}
func main() {
flag.Parse()
config.Scope = drive.DriveScope
config.ClientId = valueOrFileContents(*clientId, *clientIdFile)
config.ClientSecret = valueOrFileContents(*secret, *secretFile)
client := getOAuthClient(config)
service, _ := drive.New(client)
// Get the ID here
driveFiles, folderId := getDriveFilesByFolder(service, *driveFolder)
localFiles := getLocalFiles(*localFolder)
toSync := mergeFolders(driveFiles, localFiles)
// Synchronization
fmt.Printf("Synchronizing %v files", len(toSync))
work := make(chan uploadRequest, 1)
done := make(chan bool, len(toSync))
for i := 0; i < 3; i++ {
go uploadWorker(service, work, done)
}
total := len(toSync)
for _, file := range toSync {
goFile, err := os.Open(file.filename)
if err != nil {
log.Fatalf("Could not open file %s: %s", file.filename, err)
}
driveFile := drive.File{
Title: file.base,
Parents: []*drive.ParentReference{
&drive.ParentReference{Id: folderId},
},
}
work <- uploadRequest{&driveFile, goFile, file.filename}
total = total - 1
log.Printf("%d files left to sync", total)
}
for _, _ = range toSync {
<-done
}
log.Printf("Done synchronizing!")
}
type uploadRequest struct {
driveFile *drive.File
localFile *os.File
filename string
}
func uploadWorker(service *drive.Service, work <-chan uploadRequest, done chan bool) {
for req := range work {
log.Printf("++ Uploading %s", req.filename)
_, err := service.Files.Insert(req.driveFile).Media(req.localFile).Do()
if err != nil {
log.Fatalf("Failed when uploading %s: %s", req.filename, err)
}
log.Printf("-- Done uploading %s", req.filename)
req.localFile.Close()
done <- true
}
}
func osUserCacheDir() string {
switch runtime.GOOS {
case "darwin":
return filepath.Join(os.Getenv("HOME"), "Library", "Caches")
case "linux", "freebsd":
return filepath.Join(os.Getenv("HOME"), ".cache")
}
log.Printf("TODO: osUserCacheDir on GOOS %q", runtime.GOOS)
return "."
}
func tokenCacheFile(config *oauth.Config) string {
hash := fnv.New32a()
hash.Write([]byte(config.ClientId))
hash.Write([]byte(config.ClientSecret))
hash.Write([]byte(config.Scope))
fn := fmt.Sprintf("gdrive-sync-tok%v", hash.Sum32())
return filepath.Join(osUserCacheDir(), url.QueryEscape(fn))
}
func tokenFromFile(file string) (*oauth.Token, error) {
if !*cacheToken {
return nil, errors.New("--cachetoken is false")
}
f, err := os.Open(file)
if err != nil {
return nil, err
}
t := new(oauth.Token)
err = gob.NewDecoder(f).Decode(t)
return t, err
}
func saveToken(file string, token *oauth.Token) {
f, err := os.Create(file)
if err != nil {
log.Printf("Warning: failed to cache oauth token: %v", err)
return
}
defer f.Close()
gob.NewEncoder(f).Encode(token)
}
func condDebugTransport(rt http.RoundTripper) http.RoundTripper {
if *debug {
return &logTransport{rt}
}
return rt
}
func getOAuthClient(config *oauth.Config) *http.Client {
cacheFile := tokenCacheFile(config)
token, err := tokenFromFile(cacheFile)
if err != nil {
token = tokenFromWeb(config)
saveToken(cacheFile, token)
} else {
log.Printf("Using cached token %#v from %q", token, cacheFile)
}
t := &oauth.Transport{
Token: token,
Config: config,
Transport: condDebugTransport(http.DefaultTransport),
}
return t.Client()
}
func tokenFromWeb(config *oauth.Config) *oauth.Token {
ch := make(chan string)
randState := fmt.Sprintf("st%d", time.Now().UnixNano())
ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
if req.URL.Path == "/favicon.ico" {
http.Error(rw, "", 404)
return
}
if req.FormValue("state") != randState {
log.Printf("State doesn't match: req = %#v", req)
http.Error(rw, "", 500)
return
}
if code := req.FormValue("code"); code != "" {
fmt.Fprintf(rw, "<h1>Success</h1>Authorized.")
rw.(http.Flusher).Flush()
ch <- code
return
}
log.Printf("no code")
http.Error(rw, "", 500)
}))
defer ts.Close()
config.RedirectURL = ts.URL
authUrl := config.AuthCodeURL(randState)
go openUrl(authUrl)
log.Printf("Authorize this app at: %s", authUrl)
code := <-ch
log.Printf("Got code: %s", code)
t := &oauth.Transport{
Config: config,
Transport: condDebugTransport(http.DefaultTransport),
}
_, err := t.Exchange(code)
if err != nil {
log.Fatalf("Token exchange error: %v", err)
}
return t.Token
}
func openUrl(url string) {
try := []string{"xdg-open", "google-chrome", "open"}
for _, bin := range try {
err := exec.Command(bin, url).Run()
if err == nil {
return
}
}
log.Printf("Error opening URL in browser.")
}
func valueOrFileContents(value string, filename string) string {
if value != "" {
return value
}
slurp, err := ioutil.ReadFile(filename)
if err != nil {
log.Fatalf("Error reading %q: %v", filename, err)
}
return strings.TrimSpace(string(slurp))
}