-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlist_files.go
65 lines (55 loc) · 1.54 KB
/
list_files.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
package driveext
import (
"fmt"
"path/filepath"
"strings"
"github.com/pkg/errors"
"google.golang.org/api/drive/v3"
)
func ListFiles(driveService *drive.Service, folderID string) (DriveFiles, error) {
return listFilesWithPath(driveService, folderID, "/")
}
func listFilesWithPath(driveService *drive.Service, folderID, path string) (DriveFiles, error) {
var files []*DriveFile
var nextPageToken string
for {
q := "trashed = false"
if folderID != "" {
q = q + fmt.Sprintf(" and '%s' in parents", folderID)
}
filesListCall := driveService.Files.List().
PageSize(100).
Fields("nextPageToken, files(id, name, size, md5Checksum, mimeType, trashed)").
OrderBy("name").
Q(q)
if nextPageToken != "" {
filesListCall.PageToken(nextPageToken)
}
fileList, err := filesListCall.Do()
if err != nil {
return nil, errors.Wrap(err, "files.list call failed")
}
for _, file := range fileList.Files {
if file.MimeType == "application/vnd.google-apps.folder" {
fs, err := listFilesWithPath(driveService, file.Id, filepath.Join(path, file.Name))
if err != nil {
return nil, errors.Wrapf(err, "listing child folder failed (folderID: %s)", file.Id)
}
files = append(files, fs...)
continue
}
if strings.HasPrefix(file.MimeType, "application/vnd.google-apps.") {
continue // skip Google Docs
}
files = append(files, &DriveFile{
File: file,
Path: filepath.Join(path, file.Name),
})
}
nextPageToken = fileList.NextPageToken
if nextPageToken == "" {
break
}
}
return files, nil
}