-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsync_notes_gdrive.go
125 lines (102 loc) · 2.74 KB
/
sync_notes_gdrive.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
package main
import (
"context"
"crypto/md5"
"encoding/hex"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"google.golang.org/api/drive/v3"
"google.golang.org/api/option"
)
type SyncNotesGdriveCmd struct {
FolderId string `required:"true" help:"ID of the Google Drive folder containing the notes"`
Creds string `required:"true" help:"Path to the service account key JSON file"`
}
func (cmd *SyncNotesGdriveCmd) Run() error {
ctx := context.Background()
drv, err := drive.NewService(ctx, option.WithCredentialsFile(cmd.Creds))
if err != nil {
return fmt.Errorf("failed to create Drive service: %w", err)
}
if err := cmd.syncFolder(ctx, drv, cmd.FolderId, "content/daily-notes/"); err != nil {
return fmt.Errorf("failed to sync folder: %w", err)
}
return nil
}
func (cmd *SyncNotesGdriveCmd) syncFolder(ctx context.Context, drv *drive.Service, folderID, localPath string) error {
var pageToken string
for {
query := drv.Files.List().
Q(fmt.Sprintf("'%s' in parents and trashed = false", folderID)).
Fields("nextPageToken, files(id, name, mimeType, md5Checksum)")
if pageToken != "" {
query = query.PageToken(pageToken)
}
result, err := query.Do()
if err != nil {
return fmt.Errorf("failed to list files: %w", err)
}
for _, f := range result.Files {
localPath := filepath.Join(localPath, f.Name)
if f.MimeType == "application/vnd.google-apps.folder" {
if err := os.MkdirAll(localPath, 0755); err != nil {
return fmt.Errorf("failed to create local folder: %w", err)
}
if err := cmd.syncFolder(ctx, drv, f.Id, localPath); err != nil {
return err
}
} else {
if needsSync(localPath, f.Md5Checksum) {
if err := downloadFile(drv, f.Id, localPath); err != nil {
return fmt.Errorf("failed to download file %s: %w", f.Name, err)
}
fmt.Println(strings.TrimPrefix(localPath, "content/daily-notes/"))
}
}
}
if result.NextPageToken == "" {
break
}
pageToken = result.NextPageToken
}
return nil
}
func needsSync(path, remoteMD5 string) bool {
if _, err := os.Stat(path); os.IsNotExist(err) {
return true
}
localMD5, err := calculateMD5(path)
if err != nil {
return true
}
return localMD5 != remoteMD5
}
func calculateMD5(path string) (string, error) {
f, err := os.Open(path)
if err != nil {
return "", err
}
defer f.Close()
h := md5.New()
if _, err := io.Copy(h, f); err != nil {
return "", err
}
return hex.EncodeToString(h.Sum(nil)), nil
}
func downloadFile(srv *drive.Service, fileID, path string) error {
resp, err := srv.Files.Get(fileID).Download()
if err != nil {
return err
}
defer resp.Body.Close()
out, err := os.Create(path)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, resp.Body)
return err
}