forked from frizinak/gphoto2go
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ex_camera.go
68 lines (59 loc) · 1.65 KB
/
ex_camera.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
66
67
68
package gphoto2
import (
"path"
"strings"
)
func (cam *Camera) ListFoldersRec(p string) ([]string, error) {
if !strings.HasSuffix(p, "/") {
p = p + "/"
}
if folders, err := cam.ListFolders(p); err != nil {
return nil, err
} else {
var result []string
for _, folder := range folders {
folder = path.Join(p, folder)
result = append(result, folder)
if f, err := cam.ListFoldersRec(folder); err != nil {
return nil, err
} else {
for _, f := range f {
result = append(result, f)
}
}
}
return result, nil
}
}
func (cam *Camera) ListFilesRecYield(p string, yield func(folder string, file string) error) error {
if !strings.HasSuffix(p, "/") {
p = p + "/"
}
if folders, err := cam.ListFoldersRec(p); err != nil {
return err
} else {
for _, folder := range append([]string{p}, folders...) {
if f, err := cam.ListFiles(folder); err != nil {
return err
} else {
for _, f := range f {
if err := yield(folder, f); err != nil {
return err
}
}
}
}
}
return nil
}
func (cam *Camera) ListFilesRec(p string) ([]string, error) {
var files []string
if err := cam.ListFilesRecYield(p, func(folder, file string) error {
files = append(files, path.Join(folder, file))
return nil
}); err != nil {
return nil, err
} else {
return files, nil
}
}