-
Notifications
You must be signed in to change notification settings - Fork 28
/
local_storage.go
52 lines (42 loc) · 1.24 KB
/
local_storage.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
package main
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
)
type localStorage struct {
settings *backend.DataSourceInstanceSettings
customSettings dataSourceSettings
query dataSourceQuery
}
func newLocalStorage(_ context.Context, instance *dataSourceInstance, query dataSourceQuery, logger log.Logger) (*localStorage, error) {
customSettings, err := instance.Settings()
if err != nil {
return nil, err
}
return &localStorage{
settings: &instance.settings,
customSettings: customSettings,
query: query,
}, nil
}
func (c *localStorage) Open() (io.ReadCloser, error) {
if c.query.Path == "" {
return os.Open(filepath.ToSlash(c.settings.URL))
}
fullPath := filepath.Join(c.settings.URL, c.query.Path)
// Ensure users can't slip out of the directory configured by the admin.
if !strings.HasPrefix(fullPath, filepath.Clean(c.settings.URL)+string(os.PathSeparator)) {
return nil, fmt.Errorf("illegal file path: %s", c.query.Path)
}
return os.Open(filepath.ToSlash(fullPath))
}
func (c *localStorage) Stat() error {
_, err := os.Stat(filepath.ToSlash(c.settings.URL))
return err
}