"Fossies" - the Fresh Open Source Software Archive

Member "gdrive-2.1.1/drive/list.go" (28 May 2021, 2781 Bytes) of package /linux/misc/gdrive-2.1.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Go source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 package drive
    2 
    3 import (
    4     "fmt"
    5     "golang.org/x/net/context"
    6     "google.golang.org/api/drive/v3"
    7     "google.golang.org/api/googleapi"
    8     "io"
    9     "text/tabwriter"
   10 )
   11 
   12 type ListFilesArgs struct {
   13     Out         io.Writer
   14     MaxFiles    int64
   15     NameWidth   int64
   16     Query       string
   17     SortOrder   string
   18     SkipHeader  bool
   19     SizeInBytes bool
   20     AbsPath     bool
   21 }
   22 
   23 func (self *Drive) List(args ListFilesArgs) (err error) {
   24     listArgs := listAllFilesArgs{
   25         query:     args.Query,
   26         fields:    []googleapi.Field{"nextPageToken", "files(id,name,md5Checksum,mimeType,size,createdTime,parents)"},
   27         sortOrder: args.SortOrder,
   28         maxFiles:  args.MaxFiles,
   29     }
   30     files, err := self.listAllFiles(listArgs)
   31     if err != nil {
   32         return fmt.Errorf("Failed to list files: %s", err)
   33     }
   34 
   35     pathfinder := self.newPathfinder()
   36 
   37     if args.AbsPath {
   38         // Replace name with absolute path
   39         for _, f := range files {
   40             f.Name, err = pathfinder.absPath(f)
   41             if err != nil {
   42                 return err
   43             }
   44         }
   45     }
   46 
   47     PrintFileList(PrintFileListArgs{
   48         Out:         args.Out,
   49         Files:       files,
   50         NameWidth:   int(args.NameWidth),
   51         SkipHeader:  args.SkipHeader,
   52         SizeInBytes: args.SizeInBytes,
   53     })
   54 
   55     return
   56 }
   57 
   58 type listAllFilesArgs struct {
   59     query     string
   60     fields    []googleapi.Field
   61     sortOrder string
   62     maxFiles  int64
   63 }
   64 
   65 func (self *Drive) listAllFiles(args listAllFilesArgs) ([]*drive.File, error) {
   66     var files []*drive.File
   67 
   68     var pageSize int64
   69     if args.maxFiles > 0 && args.maxFiles < 1000 {
   70         pageSize = args.maxFiles
   71     } else {
   72         pageSize = 1000
   73     }
   74 
   75     controlledStop := fmt.Errorf("Controlled stop")
   76 
   77     err := self.service.Files.List().Q(args.query).Fields(args.fields...).OrderBy(args.sortOrder).PageSize(pageSize).Pages(context.TODO(), func(fl *drive.FileList) error {
   78         files = append(files, fl.Files...)
   79 
   80         // Stop when we have all the files we need
   81         if args.maxFiles > 0 && len(files) >= int(args.maxFiles) {
   82             return controlledStop
   83         }
   84 
   85         return nil
   86     })
   87 
   88     if err != nil && err != controlledStop {
   89         return nil, err
   90     }
   91 
   92     if args.maxFiles > 0 {
   93         n := min(len(files), int(args.maxFiles))
   94         return files[:n], nil
   95     }
   96 
   97     return files, nil
   98 }
   99 
  100 type PrintFileListArgs struct {
  101     Out         io.Writer
  102     Files       []*drive.File
  103     NameWidth   int
  104     SkipHeader  bool
  105     SizeInBytes bool
  106 }
  107 
  108 func PrintFileList(args PrintFileListArgs) {
  109     w := new(tabwriter.Writer)
  110     w.Init(args.Out, 0, 0, 3, ' ', 0)
  111 
  112     if !args.SkipHeader {
  113         fmt.Fprintln(w, "Id\tName\tType\tSize\tCreated")
  114     }
  115 
  116     for _, f := range args.Files {
  117         fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n",
  118             f.Id,
  119             truncateString(f.Name, args.NameWidth),
  120             filetype(f),
  121             formatSize(f.Size, args.SizeInBytes),
  122             formatDatetime(f.CreatedTime),
  123         )
  124     }
  125 
  126     w.Flush()
  127 }
  128 
  129 func filetype(f *drive.File) string {
  130     if isDir(f) {
  131         return "dir"
  132     } else if isBinary(f) {
  133         return "bin"
  134     }
  135     return "doc"
  136 }