-
Notifications
You must be signed in to change notification settings - Fork 11.7k
/
service.go
88 lines (73 loc) · 2.37 KB
/
service.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package store
import (
"context"
"os"
"path/filepath"
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/grafana/grafana/pkg/infra/filestorage"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/registry"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/setting"
)
var grafanaStorageLogger = log.New("grafanaStorageLogger")
const RootPublicStatic = "public-static"
type StorageService interface {
registry.BackgroundService
// List folder contents
List(ctx context.Context, user *models.SignedInUser, path string) (*data.Frame, error)
// Read raw file contents out of the store
Read(ctx context.Context, user *models.SignedInUser, path string) (*filestorage.File, error)
}
type standardStorageService struct {
sql *sqlstore.SQLStore
tree *nestedTree
}
func ProvideService(sql *sqlstore.SQLStore, features featuremgmt.FeatureToggles, cfg *setting.Cfg) StorageService {
roots := []storageRuntime{
newDiskStorage(RootPublicStatic, "Public static files", &StorageLocalDiskConfig{
Path: cfg.StaticRootPath,
Roots: []string{
"/testdata/",
// "/img/icons/",
// "/img/bg/",
"/img/",
"/gazetteer/",
"/maps/",
},
}).setReadOnly(true).setBuiltin(true),
}
storage := filepath.Join(cfg.DataPath, "storage")
_ = os.MkdirAll(storage, 0700)
if features.IsEnabled(featuremgmt.FlagStorageLocalUpload) {
roots = append(roots, newDiskStorage("upload", "Local file upload", &StorageLocalDiskConfig{
Path: filepath.Join(storage, "upload"),
}))
}
s := newStandardStorageService(roots)
s.sql = sql
return s
}
func newStandardStorageService(roots []storageRuntime) *standardStorageService {
res := &nestedTree{
roots: roots,
}
res.init()
return &standardStorageService{
tree: res,
}
}
func (s *standardStorageService) Run(ctx context.Context) error {
grafanaStorageLogger.Info("storage starting")
return nil
}
func (s *standardStorageService) List(ctx context.Context, user *models.SignedInUser, path string) (*data.Frame, error) {
// apply access control here
return s.tree.ListFolder(ctx, path)
}
func (s *standardStorageService) Read(ctx context.Context, user *models.SignedInUser, path string) (*filestorage.File, error) {
// TODO: permission check!
return s.tree.GetFile(ctx, path)
}