-
Notifications
You must be signed in to change notification settings - Fork 5
/
getfilelist.go
339 lines (312 loc) · 8.96 KB
/
getfilelist.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
// Package getfilelist (getfilelist.go) :
// This is a Golang library to retrieve the file list with the folder tree from the specific folder of Google Drive.
package getfilelist
import (
"fmt"
"os"
"strings"
drive "google.golang.org/api/drive/v3"
"google.golang.org/api/googleapi"
)
const (
defFields = "files(createdTime,description,id,mimeType,modifiedTime,name,owners,parents,permissions,shared,size,webContentLink,webViewLink),nextPageToken"
)
// BaseInfo : Base information
type BaseInfo struct {
CustomFields string
FolderID string
InputtedMimeType []string
SearchFolder *drive.File
Srv *drive.Service
}
// FileListDl : Retrieved file list.
type FileListDl struct {
SearchedFolder *drive.File `json:"searchedFolder,omitempty"`
FolderTree *FolderTree `json:"folderTree,omitempty"`
FileList []FileListEle `json:"fileList,omitempty"`
TotalNumberOfFiles int64 `json:"totalNumberOfFiles,omitempty"`
TotalNumberOfFolders int64 `json:"totalNumberOfFolders,omitempty"`
}
// FolderTree : Struct for folder tree.
type FolderTree struct {
IDs [][]string `json:"id,omitempty"`
Names []string `json:"names,omitempty"`
Folders []string `json:"folders,omitempty"`
}
// FileListEle : Struct for file list.
type FileListEle struct {
FolderTree []string `json:"folderTree"`
Files []*drive.File `json:"files"`
}
// fileListSt : File list.
type fileListSt struct {
NextPageToken string
Files []*drive.File
}
// forFT : For creating folder tree.
type forFT struct {
Name string
ID string
Parent string
Tree []string
}
// folderTr : For creating folder tree.
type folderTr struct {
Temp [][]forFT
Search string
}
// forFTTemp : For creating folder tree.
type forFTTemp struct {
Temp []forFT
}
// getFilesFromFolder : Retrieve file list from folder list.
func (b *BaseInfo) getFilesFromFolder(FolderTree *FolderTree) *FileListDl {
f := &FileListDl{}
f.SearchedFolder = b.SearchFolder
f.FolderTree = FolderTree
fields := func() string {
if b.CustomFields == "" {
return defFields
}
if !strings.Contains(b.CustomFields, "nextPageToken") {
b.CustomFields += ",nextPageToken"
}
return b.CustomFields
}()
var mType string
if len(b.InputtedMimeType) > 0 {
for i, e := range b.InputtedMimeType {
if i == 0 {
mType = " and (mimeType='" + e + "'"
continue
}
mType += " or mimeType='" + e + "'"
}
mType += ")"
}
for i, id := range FolderTree.Folders {
q := "'" + id + "' in parents and mimeType != 'application/vnd.google-apps.folder' and trashed=false" + mType
fm, err := b.getListLoop(q, fields)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
var fe FileListEle
fe.FolderTree = FolderTree.IDs[i]
fe.Files = append(fe.Files, fm.Files...)
f.FileList = append(f.FileList, fe)
}
f.TotalNumberOfFolders = int64(len(f.FolderTree.Folders))
f.TotalNumberOfFiles = func() int64 {
var c int64
for _, e := range f.FileList {
c += int64(len(e.Files))
}
return c
}()
return f
}
// getList : For retrieving file list.
func (b *BaseInfo) getList(ptoken, q, fields string) (*drive.FileList, error) {
var err error
var r *drive.FileList
f := []googleapi.Field{"nextPageToken", googleapi.Field(fields)}
filesListCall := b.Srv.Files.List().PageSize(1000).PageToken(ptoken).OrderBy("name").Q(q).SupportsAllDrives(true).IncludeItemsFromAllDrives(true).Fields(f...)
if b.SearchFolder.DriveId != "" {
r, err = filesListCall.Corpora("drive").DriveId(b.SearchFolder.DriveId).Do()
} else {
r, err = filesListCall.Do()
}
if err != nil {
return nil, err
}
return r, nil
}
// getListLoop : Loop for retrieving file list.
func (b *BaseInfo) getListLoop(q, fields string) (*fileListSt, error) {
f := &fileListSt{}
nextPageToken := ""
for {
body, err := b.getList(nextPageToken, q, fields)
if err != nil {
return nil, err
}
f.Files = append(f.Files, body.Files...)
if body.NextPageToken == "" {
break
}
nextPageToken = body.NextPageToken
}
return f, nil
}
// getAllfoldersRecursively : Recursively get folder tree using Drive API.
func (b *BaseInfo) getAllfoldersRecursively(id string, parents []string, fls *folderTr) *folderTr {
q := "'" + id + "' in parents and mimeType='application/vnd.google-apps.folder' and trashed=false"
fields := "files(id,mimeType,name,parents,size),nextPageToken"
fm, err := b.getListLoop(q, fields)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
var temp forFTTemp
for _, e := range fm.Files {
ForFt := &forFT{
ID: e.Id,
Name: e.Name,
Parent: func() string {
if len(e.Parents) > 0 {
return e.Parents[0]
}
return ""
}(),
Tree: append(parents, id),
}
temp.Temp = append(temp.Temp, *ForFt)
}
if len(temp.Temp) > 0 {
fls.Temp = append(fls.Temp, temp.Temp)
for _, e := range temp.Temp {
b.getAllfoldersRecursively(e.ID, e.Tree, fls)
}
}
return fls
}
// createFolderTreeID : Create a folder tree.
func createFolderTreeID(fm *fileListSt, id string, parents []string, fls *folderTr) *folderTr {
var temp forFTTemp
for _, e := range fm.Files {
if len(e.Parents) > 0 && e.Parents[0] == id {
ForFt := &forFT{
ID: e.Id,
Name: e.Name,
Parent: func() string {
if len(e.Parents) > 0 {
return e.Parents[0]
}
return ""
}(),
Tree: append(parents, id),
}
temp.Temp = append(temp.Temp, *ForFt)
}
}
if len(temp.Temp) > 0 {
fls.Temp = append(fls.Temp, temp.Temp)
for _, e := range temp.Temp {
createFolderTreeID(fm, e.ID, e.Tree, fls)
}
}
return fls
}
// getDlFoldersS : Retrieve each folder from folder list using folder ID. This is for shared folders.
func (fr *folderTr) getDlFoldersS(searchFolderName string) *FolderTree {
fT := &FolderTree{}
fT.Folders = append(fT.Folders, fr.Search)
fT.Names = append(fT.Names, searchFolderName)
fT.IDs = append(fT.IDs, []string{fr.Search})
for _, e := range fr.Temp {
for _, f := range e {
fT.Folders = append(fT.Folders, f.ID)
var tmp []string
tmp = append(tmp, f.Tree...)
tmp = append(tmp, f.ID)
fT.IDs = append(fT.IDs, tmp)
fT.Names = append(fT.Names, f.Name)
}
}
return fT
}
// getFolderByFolder : Retrieve folder tree by folder by folder.
func (b *BaseInfo) getFolderByFolder() *FolderTree {
tr := &folderTr{Search: b.SearchFolder.Id}
return b.getAllfoldersRecursively(b.SearchFolder.Id, []string{}, tr).getDlFoldersS(b.SearchFolder.Name)
}
// getFromFolders : Retrieve folder tree from all folders.
func (b *BaseInfo) getFromAllFolders() *FolderTree {
q := "mimeType='application/vnd.google-apps.folder' and trashed=false"
fields := "files(id,mimeType,name,parents,size),nextPageToken"
fm, err := b.getListLoop(q, fields)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
tr := &folderTr{Search: b.SearchFolder.Id}
return createFolderTreeID(fm, b.SearchFolder.Id, []string{}, tr).getDlFoldersS(b.SearchFolder.Name)
}
// getFileInf : Retrieve file infomation using Drive API.
func (b *BaseInfo) getFileInf() error {
fields := []googleapi.Field{"createdTime,id,mimeType,modifiedTime,name,owners,parents,shared,webContentLink,webViewLink,driveId"}
res, err := b.Srv.Files.Get(b.FolderID).SupportsAllDrives(true).Fields(fields...).Do()
if err != nil {
return err
}
b.SearchFolder = res
return nil
}
// Fields : Set fields for file list.
func (b *BaseInfo) Fields(fields string) *BaseInfo {
b.CustomFields = fields
return b
}
// Folder : Set folder ID
func Folder(folderID string) *BaseInfo {
b := &BaseInfo{
FolderID: folderID,
}
return b
}
// MimeType : Set mimeType
func (b *BaseInfo) MimeType(mimeType []string) *BaseInfo {
b.InputtedMimeType = mimeType
return b
}
// GetFolderTree : Retrieve only folder tree under the specific folder.
func (b *BaseInfo) GetFolderTree(service *drive.Service) (*FolderTree, error) {
b.Srv = service
if err := b.getFileInf(); err != nil {
return nil, err
}
if b.SearchFolder.Shared {
ft := b.getFolderByFolder()
return ft, nil
}
ft := b.getFromAllFolders()
return ft, nil
}
// Do : Retrieve all file list and folder tree under the specific folder.
func (b *BaseInfo) Do(service *drive.Service) (*FileListDl, error) {
b.Srv = service
if err := b.getFileInf(); err != nil {
return nil, err
}
if b.SearchFolder.Shared {
ft := b.getFolderByFolder()
return b.getFilesFromFolder(ft), nil
}
ft := b.getFromAllFolders()
return b.getFilesFromFolder(ft), nil
}
// GetFolderTree : Retrieve only folder tree under root.
func GetFolderTree(service *drive.Service) (*FolderTree, error) {
b := &BaseInfo{
Srv: service,
FolderID: "root",
}
if err := b.getFileInf(); err != nil {
return nil, err
}
ft := b.getFromAllFolders()
return ft, nil
}
// Do : Retrieve all file list and folder tree under root.
func Do(service *drive.Service) (*FileListDl, error) {
b := &BaseInfo{
Srv: service,
FolderID: "root",
}
if err := b.getFileInf(); err != nil {
return nil, err
}
ft := b.getFromAllFolders()
return b.getFilesFromFolder(ft), nil
}