Storage: Add basic file upload management (#50638)

This commit is contained in:
Ryan McKinley
2022-07-05 10:53:41 -07:00
committed by GitHub
parent 4a76436be2
commit 4a00c7ebde
25 changed files with 1105 additions and 174 deletions
+15 -11
View File
@@ -26,9 +26,9 @@ var ErrValidationFailed = errors.New("request validation failed")
var ErrFileAlreadyExists = errors.New("file exists")
const RootPublicStatic = "public-static"
const RootUpload = "upload"
const RootResources = "resources"
const MAX_UPLOAD_SIZE = 1024 * 1024 // 1MB
const MAX_UPLOAD_SIZE = 3 * 1024 * 1024 // 3MB
type StorageService interface {
registry.BackgroundService
@@ -60,21 +60,25 @@ func ProvideService(sql *sqlstore.SQLStore, features featuremgmt.FeatureToggles,
Path: cfg.StaticRootPath,
Roots: []string{
"/testdata/",
// "/img/icons/",
// "/img/bg/",
"/img/",
"/gazetteer/",
"/maps/",
},
}).setReadOnly(true).setBuiltin(true),
}).setReadOnly(true).setBuiltin(true).
setDescription("Access files from the static public files"),
}
initializeOrgStorages := func(orgId int64) []storageRuntime {
storages := make([]storageRuntime, 0)
if features.IsEnabled(featuremgmt.FlagStorageLocalUpload) {
config := &StorageSQLConfig{orgId: orgId}
storages = append(storages, newSQLStorage(RootUpload, "Local file upload", config, sql).setBuiltin(true))
storages = append(storages,
newSQLStorage(RootResources,
"Resources",
&StorageSQLConfig{orgId: orgId}, sql).
setBuiltin(true).
setDescription("Upload custom resource files"))
}
return storages
}
@@ -133,16 +137,16 @@ type UploadRequest struct {
}
func (s *standardStorageService) Upload(ctx context.Context, user *models.SignedInUser, req *UploadRequest) error {
upload, _ := s.tree.getRoot(getOrgId(user), RootUpload)
upload, _ := s.tree.getRoot(getOrgId(user), RootResources)
if upload == nil {
return ErrUploadFeatureDisabled
}
if !strings.HasPrefix(req.Path, RootUpload+"/") {
if !strings.HasPrefix(req.Path, RootResources+"/") {
return ErrUnsupportedStorage
}
storagePath := strings.TrimPrefix(req.Path, RootUpload)
storagePath := strings.TrimPrefix(req.Path, RootResources)
validationResult := s.validateUploadRequest(ctx, user, req, storagePath)
if !validationResult.ok {
grafanaStorageLogger.Warn("file upload validation failed", "filetype", req.MimeType, "path", req.Path, "reason", validationResult.reason)
@@ -178,7 +182,7 @@ func (s *standardStorageService) Upload(ctx context.Context, user *models.Signed
}
func (s *standardStorageService) Delete(ctx context.Context, user *models.SignedInUser, path string) error {
upload, _ := s.tree.getRoot(getOrgId(user), RootUpload)
upload, _ := s.tree.getRoot(getOrgId(user), RootResources)
if upload == nil {
return fmt.Errorf("upload feature is not enabled")
}