forked from petrpulc/gdrive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.go
65 lines (51 loc) · 1.17 KB
/
path.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 drive
import (
"fmt"
"google.golang.org/api/drive/v3"
"path/filepath"
)
func (self *Drive) newPathfinder() *remotePathfinder {
return &remotePathfinder{
service: self.service.Files,
files: make(map[string]*drive.File),
}
}
type remotePathfinder struct {
service *drive.FilesService
files map[string]*drive.File
}
func (self *remotePathfinder) absPath(f *drive.File) (string, error) {
name := f.Name
if len(f.Parents) == 0 {
return name, nil
}
var path []string
for {
parent, err := self.getParent(f.Parents[0])
if err != nil {
return "", err
}
// Stop when we find the root dir
if len(parent.Parents) == 0 {
break
}
path = append([]string{parent.Name}, path...)
f = parent
}
path = append(path, name)
return filepath.Join(path...), nil
}
func (self *remotePathfinder) getParent(id string) (*drive.File, error) {
// Check cache
if f, ok := self.files[id]; ok {
return f, nil
}
// Fetch file from drive
f, err := self.service.Get(id).SupportsTeamDrives(true).Fields("id", "name", "parents").Do()
if err != nil {
return nil, fmt.Errorf("Failed to get file: %s", err)
}
// Save in cache
self.files[f.Id] = f
return f, nil
}