-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.go
190 lines (165 loc) · 4.72 KB
/
upload.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 gcloud
import (
"context"
"fmt"
"io"
"os"
"time"
"github.com/lomorage/lomo-backup/common/types"
"github.com/sirupsen/logrus"
"golang.org/x/oauth2/google"
"google.golang.org/api/drive/v3"
"google.golang.org/api/option"
)
const mimiTypeFolder = "application/vnd.google-apps.folder"
type Config struct {
CredFilename string
TokenFilename string
RefreshToken bool
}
type DriveClient struct {
srv *drive.Service
}
func CreateDriveClient(conf *Config) (*DriveClient, error) {
ctx := context.Background()
b, err := os.ReadFile(conf.CredFilename)
if err != nil {
return nil, fmt.Errorf("unable to read client secret file: %v", err)
}
// If modifying these scopes, delete your previously saved token.json.
config, err := google.ConfigFromJSON(b, drive.DriveScope)
if err != nil {
return nil, fmt.Errorf("unable to parse client secret file to config: %v", err)
}
token, err := tokenFromFile(conf.TokenFilename)
if err != nil {
return nil, err
}
if conf.RefreshToken {
token, err = refreshAccessToken(ctx, config, token)
if err != nil {
return nil, err
}
}
err = SaveToken(conf.TokenFilename, token)
if err != nil {
return nil, err
}
cli := &DriveClient{}
cli.srv, err = drive.NewService(ctx, option.WithHTTPClient(config.Client(ctx, token)))
return cli, err
}
func (c *DriveClient) GetFile(fileID string) (io.ReadCloser, error) {
f, err := c.srv.Files.Get(fileID).Download()
if err != nil {
return nil, err
}
return f.Body, nil
}
func (c *DriveClient) GetFileID(filename, parentFolderID string) (string, string, error) {
query := fmt.Sprintf("name = '%s'", filename)
if parentFolderID != "" {
query += fmt.Sprintf(" and '%s' in parents", parentFolderID)
}
files, err := c.srv.Files.List().Q(query).PageSize(1).Fields("files(id, name, parents)").Do()
if err != nil {
return "", "", err
}
if len(files.Files) == 0 {
return "", "", nil
}
parentID := ""
if len(files.Files[0].Parents) == 0 {
logrus.Warnf("File '%s' is not in any folder.", filename)
} else {
parentID = files.Files[0].Parents[0]
}
return files.Files[0].Id, parentID, nil
}
func (c *DriveClient) GetAndCreateFileIDIfNotExist(filename, parentFolderID string, r io.Reader, modTime time.Time) (bool, string, error) {
fileID, pid, err := c.GetFileID(filename, parentFolderID)
if err != nil {
return false, "", err
}
if fileID == "" {
// not exist, create new one
fileID, err = c.CreateFile(filename, parentFolderID, r, modTime)
return false, fileID, err
}
if parentFolderID != "" && pid != parentFolderID {
return false, "", fmt.Errorf("obtain wrong parent ID while attempting to find '%s' folder ID ", filename)
}
return true, fileID, nil
}
func (c *DriveClient) CreateFile(filename, parentFolderID string, r io.Reader, modTime time.Time) (string, error) {
file := &drive.File{
Name: filename,
CreatedTime: modTime.Format(time.RFC3339),
}
if parentFolderID != "" {
file.Parents = []string{parentFolderID}
}
if r == nil {
// it is a folder
file.MimeType = mimiTypeFolder
f, err := c.srv.Files.Create(file).Do()
if err != nil {
return "", err
}
return f.Id, nil
}
f, err := c.srv.Files.Create(file).Media(r).Do()
if err != nil {
return "", err
}
return f.Id, nil
}
func (c *DriveClient) UpdateFileMetadata(fileID string, metadata map[string]string) error {
file, err := c.srv.Files.Get(fileID).Fields("appProperties").Do()
if err != nil {
return fmt.Errorf("failed to retrieve file metadata: %v", err)
}
// Update the AppProperties
file.AppProperties = metadata
// Call the update method with modified metadata
_, err = c.srv.Files.Update(fileID, file).Fields("id, name, appProperties").Do()
if err != nil {
return fmt.Errorf("failed to update file metadata: %v", err)
}
return nil
}
func (c *DriveClient) ListFiles(folderID string) ([]*types.DirInfo, []*types.FileInfo, error) {
query := fmt.Sprintf("'%s' in parents", folderID)
list, err := c.srv.Files.List().Q(query).Fields("files(id, name, size, mimeType, createdTime, appProperties)").Do()
if err != nil {
return nil, nil, err
}
files := []*types.FileInfo{}
folders := []*types.DirInfo{}
for _, f := range list.Files {
t, err := time.Parse(time.RFC3339, f.CreatedTime)
if err != nil {
logrus.Warnf("Parse %s create time: %s", f.Name, err)
}
if f.MimeType == mimiTypeFolder {
folders = append(folders, &types.DirInfo{
RefID: f.Id,
Path: f.Name,
ModTime: t,
})
continue
}
fi := &types.FileInfo{
RefID: f.Id,
Name: f.Name,
Size: int(f.Size),
ModTime: t,
}
if f.AppProperties != nil {
fi.HashLocal = f.AppProperties[types.MetadataKeyHashOrig]
fi.HashRemote = f.AppProperties[types.MetadataKeyHashEncrypt]
}
files = append(files, fi)
}
return folders, files, nil
}