Storage: validation and sanitization stubs (#50523)

* 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

* validation/sanitization

* refactor pathValidationError check

* refactor, rename sanitize to transform

* add a todo

* refactor

* transform -> sanitize

* lint fix

* #50608: fix jpg/jpeg

Co-authored-by: Tania B <yalyna.ts@gmail.com>
Co-authored-by: Ryan McKinley <ryantxu@gmail.com>
This commit is contained in:
Artur Wierzbicki
2022-06-15 12:32:29 +04:00
committed by GitHub
co-authored by Tania B Ryan McKinley
parent dfb0f6b1b8
commit cc4473faf3
6 changed files with 154 additions and 50 deletions
+6 -11
View File
@@ -34,18 +34,12 @@ func ProvideHTTPService(store StorageService) HTTPStorageService {
func UploadErrorToStatusCode(err error) int {
switch {
case errors.Is(err, ErrUploadFeatureDisabled):
return 404
case errors.Is(err, ErrUnsupportedStorage):
return 400
case errors.Is(err, ErrUnsupportedFolder):
return 400
case errors.Is(err, ErrFileTooBig):
return 400
case errors.Is(err, ErrInvalidPath):
return 400
case errors.Is(err, ErrInvalidFileType):
case errors.Is(err, ErrValidationFailed):
return 400
case errors.Is(err, ErrFileAlreadyExists):
@@ -102,9 +96,10 @@ func (s *httpStorage) Upload(c *models.ReqContext) response.Response {
mimeType := http.DetectContentType(data)
err = s.store.Upload(c.Req.Context(), c.SignedInUser, UploadRequest{
err = s.store.Upload(c.Req.Context(), c.SignedInUser, &UploadRequest{
Contents: data,
MimeType: mimeType,
EntityType: EntityTypeImage,
Path: path,
OverwriteExistingFile: true,
})