Skip to content

Commit

Permalink
List all files matching query
Browse files Browse the repository at this point in the history
  • Loading branch information
prasmussen committed Feb 6, 2016
1 parent dd73d46 commit 803981c
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions drive/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"path/filepath"
"github.com/gyuho/goraph/graph"
"github.com/sabhiram/go-git-ignore"
"golang.org/x/net/context"
"google.golang.org/api/drive/v3"
"google.golang.org/api/googleapi"
)

const DefaultIgnoreFile = ".gdriveignore"
Expand Down Expand Up @@ -94,25 +96,37 @@ func prepareLocalFiles(root string) ([]*localFile, error) {
return files, err
}

func (self *Drive) listAllFiles(q string, fields []googleapi.Field) ([]*drive.File, error) {
var files []*drive.File

err := self.service.Files.List().Q(q).Fields(fields...).PageSize(1000).Pages(context.TODO(), func(fl *drive.FileList) error {
files = append(files, fl.Files...)
return nil
})

return files, err
}

func (self *Drive) prepareRemoteFiles(rootDir *drive.File) ([]*remoteFile, error) {
// Find all files which has rootDir as root
query := fmt.Sprintf("appProperties has {key='syncRootId' and value='%s'}", rootDir.Id)
fileList, err := self.service.Files.List().Q(query).Fields("files(id,name,parents,md5Checksum,mimeType)").Do()
fields := []googleapi.Field{"nextPageToken", "files(id,name,parents,md5Checksum,mimeType)"}
files, err := self.listAllFiles(query, fields)
if err != nil {
return nil, fmt.Errorf("Failed listing files: %s", err)
}

if err := checkFiles(fileList.Files); err != nil {
if err := checkFiles(files); err != nil {
return nil, err
}

relPaths, err := prepareRemoteRelPaths(rootDir.Id, fileList.Files)
relPaths, err := prepareRemoteRelPaths(rootDir, files)
if err != nil {
return nil, err
}

var remoteFiles []*remoteFile
for _, f := range fileList.Files {
for _, f := range files {
relPath, ok := relPaths[f.Id]
if !ok {
return nil, fmt.Errorf("File %s does not have a valid parent", f.Id)
Expand Down

0 comments on commit 803981c

Please sign in to comment.