/
main.go
150 lines (122 loc) · 2.98 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
package main
import (
"context"
"fmt"
"io/ioutil"
"log"
"sync"
"golang.org/x/oauth2/google"
"google.golang.org/api/drive/v3"
"google.golang.org/api/option"
)
func main() {
ctx := context.Background()
b, err := ioutil.ReadFile("credentials.json")
if err != nil {
log.Fatalf("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 {
log.Fatalf("Unable to parse client secret file to config: %v", err)
}
client := getClient(config)
srv, err := drive.NewService(ctx, option.WithHTTPClient(client))
if err != nil {
log.Fatalf("Unable to retrieve Drive client: %v", err)
}
moveFiles(ctx, srv)
//deleteFiles(ctx, srv)
}
func moveFiles(ctx context.Context, srv *drive.Service) {
rootId := GetRootId(srv)
rootFilesId := GetRootFilesId(srv)
fmt.Println("Files:")
pageSize := int64(100)
request := srv.Files.List().
Context(ctx).
PageSize(pageSize).
Corpora("user").
Q("'" + rootId + "' in parents and mimeType='image/jpeg'").
Fields("nextPageToken,files(id, name, parents)")
ch := make(chan *drive.File, pageSize)
wg := sync.WaitGroup{}
for x := 0; x < 3; x++ {
wg.Add(1)
y := x
go func() {
for f := range ch {
mf := drive.File{}
_, err := srv.Files.Update(f.Id, &mf).Context(ctx).RemoveParents(rootId).AddParents(rootFilesId).Fields("").Do()
if err != nil {
log.Fatalf("Failed to move file: %v", err)
}
fmt.Printf(" --%d - file moved successfully: %s (%s)\n", y, f.Name, f.Id)
}
wg.Done()
}()
}
for page := 0; ; page++ {
r, err := request.Do()
if err != nil {
log.Fatalf("Unable to retrieve files: %v", err)
}
for _, f := range r.Files {
fmt.Printf("page %d %s (%s)\n", page, f.Name, f.Id)
ch <- f
}
if r.NextPageToken == "" {
break
}
request.PageToken(r.NextPageToken)
}
close(ch)
wg.Wait()
}
func deleteFiles(ctx context.Context, srv *drive.Service) {
rootFilesId := GetRootFilesId(srv)
var percent = 1
fmt.Println("Files:")
pageSize := int64(100)
request := srv.Files.List().
Context(ctx).
PageSize(pageSize).
Corpora("user").
Q("'" + rootFilesId + "' in parents").
Fields("nextPageToken,files(id, name)")
ch := make(chan *drive.File, pageSize)
wg := sync.WaitGroup{}
for x := 0; x < 3; x++ {
wg.Add(1)
y := x
go func() {
for f := range ch {
if !ShouldDeleteFile(f.Name, percent) {
continue
}
err := srv.Files.Delete(f.Id).Do()
if err != nil {
log.Fatalf("Failed to delete file: %v", err)
}
fmt.Printf(" --%d - file deleted successfully: %s (%s)\n", y, f.Name, f.Id)
}
wg.Done()
}()
}
for page := 0; ; page++ {
r, err := request.Do()
if err != nil {
log.Fatalf("Unable to retrieve files: %v", err)
}
for _, f := range r.Files {
ch <- f
}
fmt.Printf("Processed page %d\n", page)
if r.NextPageToken == "" {
break
}
request.PageToken(r.NextPageToken)
}
close(ch)
wg.Wait()
}