-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.go
68 lines (57 loc) · 1.63 KB
/
list.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 main
import (
"path/filepath"
"fmt"
"io/fs"
"errors"
"net/url"
"net/http"
)
func listFiles(dir string, recursive bool) ([]string, error) {
to_report := []string{}
err := filepath.WalkDir(dir, func(path string, info fs.DirEntry, err error) error {
if err != nil {
return err
}
is_dir := info.IsDir()
if is_dir {
if recursive || dir == path {
return nil
}
}
rel, err := filepath.Rel(dir, path)
if err != nil {
return err
}
if !recursive && is_dir {
to_report = append(to_report, rel + "/")
return fs.SkipDir
} else {
to_report = append(to_report, rel)
return nil
}
})
if err != nil {
return nil, fmt.Errorf("failed to obtain a directory listing; %w", err)
}
return to_report, nil
}
func listFilesHandler(r *http.Request, registry string) ([]string, error) {
qparams := r.URL.Query()
path := qparams.Get("path")
recursive := (qparams.Get("recursive") == "true")
if path == "" {
path = registry
} else {
var err error
path, err = url.QueryUnescape(path)
if err != nil {
return nil, newHttpError(http.StatusBadRequest, fmt.Errorf("invalid 'path'; %w", err))
} else if !filepath.IsLocal(path) {
return nil, newHttpError(http.StatusBadRequest, errors.New("'path' is not local to the registry"))
}
path = filepath.Join(registry, path)
}
all, err := listFiles(path, recursive)
return all, err
}