Storage: refactor - decouple the Upload method from HTTP concepts (#50378)

* add `IsPathValidationError` util to fs api

* refactor storage.Upload method

* remove unused struct

* extract `RootUpload` constant

* move file validation outside of the service

* Make UploadErrorToStatusCode exported

* refactor pathValidationError check

* if -> switch

Co-authored-by: Tania B <yalyna.ts@gmail.com>
This commit is contained in:
Artur Wierzbicki
2022-06-13 21:21:50 +04:00
committed by GitHub
co-authored by Tania B
parent 3b9d8da296
commit 9779f684d1
3 changed files with 157 additions and 85 deletions
+7 -8
View File
@@ -3,7 +3,6 @@ package store
import (
"bytes"
"context"
"mime/multipart"
"os"
"path/filepath"
"testing"
@@ -11,9 +10,9 @@ import (
"github.com/grafana/grafana-plugin-sdk-go/experimental"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/tsdb/testdatasource"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@@ -60,12 +59,12 @@ func TestUpload(t *testing.T) {
path, err := os.Getwd()
require.NoError(t, err)
cfg := &setting.Cfg{AppURL: "http://localhost:3000/", DataPath: path}
s := ProvideService(nil, features, cfg)
testForm := &multipart.Form{
Value: map[string][]string{},
File: map[string][]*multipart.FileHeader{},
s := ProvideService(sqlstore.InitTestDB(t), features, cfg)
request := UploadRequest{
Contents: make([]byte, 0),
Path: "upload/myFile.jpg",
MimeType: "image/jpeg",
}
res, err := s.Upload(context.Background(), dummyUser, testForm)
err = s.Upload(context.Background(), dummyUser, request)
require.NoError(t, err)
assert.Equal(t, res.path, "upload")
}