"Fossies" - the Fresh Open Source Software Archive

Member "gdrive-2.1.1/drive/path.go" (28 May 2021, 1178 Bytes) of package /linux/misc/old/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     "google.golang.org/api/drive/v3"
    6     "path/filepath"
    7 )
    8 
    9 func (self *Drive) newPathfinder() *remotePathfinder {
   10     return &remotePathfinder{
   11         service: self.service.Files,
   12         files:   make(map[string]*drive.File),
   13     }
   14 }
   15 
   16 type remotePathfinder struct {
   17     service *drive.FilesService
   18     files   map[string]*drive.File
   19 }
   20 
   21 func (self *remotePathfinder) absPath(f *drive.File) (string, error) {
   22     name := f.Name
   23 
   24     if len(f.Parents) == 0 {
   25         return name, nil
   26     }
   27 
   28     var path []string
   29 
   30     for {
   31         parent, err := self.getParent(f.Parents[0])
   32         if err != nil {
   33             return "", err
   34         }
   35 
   36         // Stop when we find the root dir
   37         if len(parent.Parents) == 0 {
   38             break
   39         }
   40 
   41         path = append([]string{parent.Name}, path...)
   42         f = parent
   43     }
   44 
   45     path = append(path, name)
   46     return filepath.Join(path...), nil
   47 }
   48 
   49 func (self *remotePathfinder) getParent(id string) (*drive.File, error) {
   50     // Check cache
   51     if f, ok := self.files[id]; ok {
   52         return f, nil
   53     }
   54 
   55     // Fetch file from drive
   56     f, err := self.service.Get(id).Fields("id", "name", "parents").Do()
   57     if err != nil {
   58         return nil, fmt.Errorf("Failed to get file: %s", err)
   59     }
   60 
   61     // Save in cache
   62     self.files[f.Id] = f
   63 
   64     return f, nil
   65 }