2022-03-17 12:19:23 -05:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
2022-07-08 13:23:16 -05:00
|
|
|
"encoding/json"
|
2022-06-13 12:21:50 -05:00
|
|
|
"errors"
|
2022-07-05 12:53:41 -05:00
|
|
|
"fmt"
|
2022-07-08 13:23:16 -05:00
|
|
|
"io"
|
2022-04-15 07:01:58 -05:00
|
|
|
"net/http"
|
2022-04-27 14:12:48 -05:00
|
|
|
"strings"
|
2022-04-15 07:01:58 -05:00
|
|
|
|
2022-03-17 12:19:23 -05:00
|
|
|
"github.com/grafana/grafana/pkg/api/response"
|
2022-07-29 01:26:44 -05:00
|
|
|
"github.com/grafana/grafana/pkg/api/routing"
|
|
|
|
"github.com/grafana/grafana/pkg/middleware"
|
2023-01-27 01:50:36 -06:00
|
|
|
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
|
2022-07-05 12:53:41 -05:00
|
|
|
"github.com/grafana/grafana/pkg/util"
|
2022-03-17 12:19:23 -05:00
|
|
|
"github.com/grafana/grafana/pkg/web"
|
|
|
|
)
|
|
|
|
|
2022-06-13 12:21:50 -05:00
|
|
|
func UploadErrorToStatusCode(err error) int {
|
|
|
|
switch {
|
2022-07-17 13:41:54 -05:00
|
|
|
case errors.Is(err, ErrStorageNotFound):
|
2022-06-15 03:32:29 -05:00
|
|
|
return 404
|
2022-06-13 12:21:50 -05:00
|
|
|
|
2022-06-15 03:32:29 -05:00
|
|
|
case errors.Is(err, ErrUnsupportedStorage):
|
2022-06-13 12:21:50 -05:00
|
|
|
return 400
|
|
|
|
|
2022-06-15 03:32:29 -05:00
|
|
|
case errors.Is(err, ErrValidationFailed):
|
2022-06-13 12:21:50 -05:00
|
|
|
return 400
|
|
|
|
|
2022-08-01 13:53:44 -05:00
|
|
|
case errors.Is(err, ErrQuotaReached):
|
|
|
|
return 400
|
|
|
|
|
2022-06-13 12:21:50 -05:00
|
|
|
case errors.Is(err, ErrFileAlreadyExists):
|
|
|
|
return 400
|
|
|
|
|
2022-07-17 13:41:54 -05:00
|
|
|
case errors.Is(err, ErrAccessDenied):
|
|
|
|
return 403
|
|
|
|
|
2022-06-13 12:21:50 -05:00
|
|
|
default:
|
|
|
|
return 500
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-29 01:26:44 -05:00
|
|
|
func (s *standardStorageService) RegisterHTTPRoutes(storageRoute routing.RouteRegister) {
|
|
|
|
storageRoute.Get("/list/", routing.Wrap(s.list))
|
|
|
|
storageRoute.Get("/list/*", routing.Wrap(s.list))
|
|
|
|
storageRoute.Get("/read/*", routing.Wrap(s.read))
|
|
|
|
storageRoute.Get("/options/*", routing.Wrap(s.getOptions))
|
|
|
|
|
|
|
|
// Write paths
|
|
|
|
reqGrafanaAdmin := middleware.ReqGrafanaAdmin
|
|
|
|
storageRoute.Post("/write/*", reqGrafanaAdmin, routing.Wrap(s.doWrite))
|
|
|
|
storageRoute.Post("/delete/*", reqGrafanaAdmin, routing.Wrap(s.doDelete))
|
|
|
|
storageRoute.Post("/upload", reqGrafanaAdmin, routing.Wrap(s.doUpload))
|
|
|
|
storageRoute.Post("/createFolder", reqGrafanaAdmin, routing.Wrap(s.doCreateFolder))
|
|
|
|
storageRoute.Post("/deleteFolder", reqGrafanaAdmin, routing.Wrap(s.doDeleteFolder))
|
|
|
|
storageRoute.Get("/config", reqGrafanaAdmin, routing.Wrap(s.getConfig))
|
|
|
|
}
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (s *standardStorageService) doWrite(c *contextmodel.ReqContext) response.Response {
|
2022-07-29 01:26:44 -05:00
|
|
|
scope, path := getPathAndScope(c)
|
|
|
|
cmd := &WriteValueRequest{}
|
|
|
|
if err := web.Bind(c.Req, cmd); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
|
|
|
cmd.Path = scope + "/" + path
|
|
|
|
rsp, err := s.write(c.Req.Context(), c.SignedInUser, cmd)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "save error", err)
|
|
|
|
}
|
2024-02-27 10:39:51 -06:00
|
|
|
return response.JSON(http.StatusOK, rsp)
|
2022-07-29 01:26:44 -05:00
|
|
|
}
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (s *standardStorageService) doUpload(c *contextmodel.ReqContext) response.Response {
|
2022-07-13 12:15:25 -05:00
|
|
|
type rspInfo struct {
|
|
|
|
Message string `json:"message,omitempty"`
|
|
|
|
Path string `json:"path,omitempty"`
|
|
|
|
Count int `json:"count,omitempty"`
|
|
|
|
Bytes int `json:"bytes,omitempty"`
|
|
|
|
Error bool `json:"err,omitempty"`
|
2022-04-27 14:12:48 -05:00
|
|
|
}
|
2022-07-13 12:15:25 -05:00
|
|
|
rsp := &rspInfo{Message: "uploaded"}
|
|
|
|
|
2022-04-27 14:12:48 -05:00
|
|
|
c.Req.Body = http.MaxBytesReader(c.Resp, c.Req.Body, MAX_UPLOAD_SIZE)
|
|
|
|
if err := c.Req.ParseMultipartForm(MAX_UPLOAD_SIZE); err != nil {
|
2022-07-13 12:15:25 -05:00
|
|
|
rsp.Message = fmt.Sprintf("Please limit file uploaded under %s", util.ByteCountSI(MAX_UPLOAD_SIZE))
|
|
|
|
rsp.Error = true
|
2024-02-27 10:39:51 -06:00
|
|
|
return response.JSON(http.StatusBadRequest, rsp)
|
2022-07-13 12:15:25 -05:00
|
|
|
}
|
|
|
|
message := getMultipartFormValue(c.Req, "message")
|
|
|
|
overwriteExistingFile := getMultipartFormValue(c.Req, "overwriteExistingFile") != "false" // must explicitly overwrite
|
|
|
|
folder := getMultipartFormValue(c.Req, "folder")
|
|
|
|
|
|
|
|
for k, fileHeaders := range c.Req.MultipartForm.File {
|
|
|
|
path := getMultipartFormValue(c.Req, k+".path") // match the path with a file
|
|
|
|
if len(fileHeaders) > 1 {
|
|
|
|
path = ""
|
|
|
|
}
|
|
|
|
if path == "" && folder == "" {
|
|
|
|
rsp.Message = "please specify the upload folder or full path"
|
|
|
|
rsp.Error = true
|
2024-02-27 10:39:51 -06:00
|
|
|
return response.JSON(http.StatusBadRequest, rsp)
|
2022-07-13 12:15:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, fileHeader := range fileHeaders {
|
|
|
|
// restrict file size based on file size
|
|
|
|
// open each file to copy contents
|
|
|
|
file, err := fileHeader.Open()
|
|
|
|
if err != nil {
|
2024-02-27 10:39:51 -06:00
|
|
|
return response.Error(http.StatusInternalServerError, "Internal Server Error", err)
|
2022-07-13 12:15:25 -05:00
|
|
|
}
|
|
|
|
err = file.Close()
|
|
|
|
if err != nil {
|
2024-02-27 10:39:51 -06:00
|
|
|
return response.Error(http.StatusInternalServerError, "Internal Server Error", err)
|
2022-07-13 12:15:25 -05:00
|
|
|
}
|
2022-08-10 08:37:51 -05:00
|
|
|
data, err := io.ReadAll(file)
|
2022-07-13 12:15:25 -05:00
|
|
|
if err != nil {
|
2024-02-27 10:39:51 -06:00
|
|
|
return response.Error(http.StatusInternalServerError, "Internal Server Error", err)
|
2022-07-13 12:15:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if path == "" {
|
|
|
|
path = folder + "/" + fileHeader.Filename
|
|
|
|
}
|
|
|
|
|
|
|
|
entityType := EntityTypeJSON
|
|
|
|
mimeType := http.DetectContentType(data)
|
2022-07-18 05:36:54 -05:00
|
|
|
if strings.HasPrefix(mimeType, "image") || strings.HasSuffix(path, ".svg") {
|
2022-07-13 12:15:25 -05:00
|
|
|
entityType = EntityTypeImage
|
|
|
|
}
|
|
|
|
|
2022-07-29 01:26:44 -05:00
|
|
|
err = s.Upload(c.Req.Context(), c.SignedInUser, &UploadRequest{
|
2022-07-13 12:15:25 -05:00
|
|
|
Contents: data,
|
|
|
|
EntityType: entityType,
|
|
|
|
Path: path,
|
|
|
|
OverwriteExistingFile: overwriteExistingFile,
|
|
|
|
Properties: map[string]string{
|
|
|
|
"message": message, // the commit/changelog entry
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(UploadErrorToStatusCode(err), err.Error(), err)
|
|
|
|
}
|
|
|
|
rsp.Count++
|
|
|
|
rsp.Bytes += len(data)
|
|
|
|
rsp.Path = path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-27 10:39:51 -06:00
|
|
|
return response.JSON(http.StatusOK, rsp)
|
2022-03-17 12:19:23 -05:00
|
|
|
}
|
|
|
|
|
2022-07-13 12:15:25 -05:00
|
|
|
func getMultipartFormValue(req *http.Request, key string) string {
|
2022-07-12 09:13:57 -05:00
|
|
|
v, ok := req.MultipartForm.Value[key]
|
|
|
|
if !ok || len(v) != 1 {
|
2022-07-13 12:15:25 -05:00
|
|
|
return ""
|
2022-07-12 09:13:57 -05:00
|
|
|
}
|
2022-07-13 12:15:25 -05:00
|
|
|
return v[0]
|
2022-07-12 09:13:57 -05:00
|
|
|
}
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (s *standardStorageService) read(c *contextmodel.ReqContext) response.Response {
|
2022-04-27 14:12:48 -05:00
|
|
|
// full path is api/storage/read/upload/example.jpg, but we only want the part after read
|
2022-03-17 12:19:23 -05:00
|
|
|
scope, path := getPathAndScope(c)
|
2022-07-29 01:26:44 -05:00
|
|
|
file, err := s.Read(c.Req.Context(), c.SignedInUser, scope+"/"+path)
|
2022-04-27 14:12:48 -05:00
|
|
|
if err != nil {
|
2024-02-27 10:39:51 -06:00
|
|
|
return response.Error(http.StatusBadRequest, "cannot call read", err)
|
2022-04-27 14:12:48 -05:00
|
|
|
}
|
2022-07-07 06:32:18 -05:00
|
|
|
|
|
|
|
if file == nil || file.Contents == nil {
|
2024-02-27 10:39:51 -06:00
|
|
|
return response.Error(http.StatusNotFound, "file does not exist", err)
|
2022-07-07 06:32:18 -05:00
|
|
|
}
|
|
|
|
|
2022-04-27 14:12:48 -05:00
|
|
|
// set the correct content type for svg
|
|
|
|
if strings.HasSuffix(path, ".svg") {
|
|
|
|
c.Resp.Header().Set("Content-Type", "image/svg+xml")
|
|
|
|
}
|
|
|
|
return response.Respond(200, file.Contents)
|
2022-03-17 12:19:23 -05:00
|
|
|
}
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (s *standardStorageService) getOptions(c *contextmodel.ReqContext) response.Response {
|
2022-07-29 01:26:44 -05:00
|
|
|
scope, path := getPathAndScope(c)
|
|
|
|
opts, err := s.getWorkflowOptions(c.Req.Context(), c.SignedInUser, scope+"/"+path)
|
|
|
|
if err != nil {
|
2024-02-27 10:39:51 -06:00
|
|
|
return response.Error(http.StatusBadRequest, err.Error(), err)
|
2022-07-29 01:26:44 -05:00
|
|
|
}
|
2024-02-27 10:39:51 -06:00
|
|
|
return response.JSON(http.StatusOK, opts)
|
2022-07-29 01:26:44 -05:00
|
|
|
}
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (s *standardStorageService) doDelete(c *contextmodel.ReqContext) response.Response {
|
2022-04-27 14:12:48 -05:00
|
|
|
// full path is api/storage/delete/upload/example.jpg, but we only want the part after upload
|
2022-07-08 13:23:16 -05:00
|
|
|
scope, path := getPathAndScope(c)
|
|
|
|
|
2022-07-29 01:26:44 -05:00
|
|
|
err := s.Delete(c.Req.Context(), c.SignedInUser, scope+"/"+path)
|
2022-04-27 14:12:48 -05:00
|
|
|
if err != nil {
|
2024-02-27 10:39:51 -06:00
|
|
|
return response.Error(http.StatusBadRequest, "failed to delete the file: "+err.Error(), err)
|
2022-04-27 14:12:48 -05:00
|
|
|
}
|
2024-02-27 10:39:51 -06:00
|
|
|
return response.JSON(http.StatusOK, map[string]any{
|
2022-04-27 14:12:48 -05:00
|
|
|
"message": "Removed file from storage",
|
2022-07-08 13:23:16 -05:00
|
|
|
"success": true,
|
|
|
|
"path": path,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (s *standardStorageService) doDeleteFolder(c *contextmodel.ReqContext) response.Response {
|
2022-07-08 13:23:16 -05:00
|
|
|
body, err := io.ReadAll(c.Req.Body)
|
|
|
|
if err != nil {
|
2024-02-27 10:39:51 -06:00
|
|
|
return response.Error(http.StatusInternalServerError, "error reading bytes", err)
|
2022-07-08 13:23:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd := &DeleteFolderCmd{}
|
|
|
|
err = json.Unmarshal(body, cmd)
|
|
|
|
if err != nil {
|
2024-02-27 10:39:51 -06:00
|
|
|
return response.Error(http.StatusBadRequest, "error parsing body", err)
|
2022-07-08 13:23:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if cmd.Path == "" {
|
2024-02-27 10:39:51 -06:00
|
|
|
return response.Error(http.StatusBadRequest, "empty path", err)
|
2022-07-08 13:23:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// full path is api/storage/delete/upload/example.jpg, but we only want the part after upload
|
|
|
|
_, path := getPathAndScope(c)
|
2022-07-29 01:26:44 -05:00
|
|
|
if err := s.DeleteFolder(c.Req.Context(), c.SignedInUser, cmd); err != nil {
|
2024-02-27 10:39:51 -06:00
|
|
|
return response.Error(http.StatusBadRequest, "failed to delete the folder: "+err.Error(), err)
|
2022-07-08 13:23:16 -05:00
|
|
|
}
|
|
|
|
|
2024-02-27 10:39:51 -06:00
|
|
|
return response.JSON(http.StatusOK, map[string]any{
|
2022-07-08 13:23:16 -05:00
|
|
|
"message": "Removed folder from storage",
|
|
|
|
"success": true,
|
2022-04-27 14:12:48 -05:00
|
|
|
"path": path,
|
2022-03-17 12:19:23 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (s *standardStorageService) doCreateFolder(c *contextmodel.ReqContext) response.Response {
|
2022-07-08 13:23:16 -05:00
|
|
|
body, err := io.ReadAll(c.Req.Body)
|
|
|
|
if err != nil {
|
2024-02-27 10:39:51 -06:00
|
|
|
return response.Error(http.StatusInternalServerError, "error reading bytes", err)
|
2022-07-08 13:23:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd := &CreateFolderCmd{}
|
|
|
|
err = json.Unmarshal(body, cmd)
|
|
|
|
if err != nil {
|
2024-02-27 10:39:51 -06:00
|
|
|
return response.Error(http.StatusBadRequest, "error parsing body", err)
|
2022-07-08 13:23:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if cmd.Path == "" {
|
2024-02-27 10:39:51 -06:00
|
|
|
return response.Error(http.StatusBadRequest, "empty path", err)
|
2022-07-08 13:23:16 -05:00
|
|
|
}
|
|
|
|
|
2022-07-29 01:26:44 -05:00
|
|
|
if err := s.CreateFolder(c.Req.Context(), c.SignedInUser, cmd); err != nil {
|
2024-02-27 10:39:51 -06:00
|
|
|
return response.Error(http.StatusBadRequest, "failed to create the folder: "+err.Error(), err)
|
2022-07-08 13:23:16 -05:00
|
|
|
}
|
|
|
|
|
2024-02-27 10:39:51 -06:00
|
|
|
return response.JSON(http.StatusOK, map[string]any{
|
2022-07-08 13:23:16 -05:00
|
|
|
"message": "Folder created",
|
|
|
|
"success": true,
|
|
|
|
"path": cmd.Path,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (s *standardStorageService) list(c *contextmodel.ReqContext) response.Response {
|
2022-03-17 12:19:23 -05:00
|
|
|
params := web.Params(c.Req)
|
|
|
|
path := params["*"]
|
2023-11-01 11:42:24 -05:00
|
|
|
// maxFiles of 0 will result in default behaviour from wrapper
|
|
|
|
frame, err := s.List(c.Req.Context(), c.SignedInUser, path, 0)
|
2022-03-17 12:19:23 -05:00
|
|
|
if err != nil {
|
2024-02-27 10:39:51 -06:00
|
|
|
return response.Error(http.StatusBadRequest, "error reading path", err)
|
2022-03-17 12:19:23 -05:00
|
|
|
}
|
|
|
|
if frame == nil {
|
2024-02-27 10:39:51 -06:00
|
|
|
return response.Error(http.StatusNotFound, "not found", nil)
|
2022-03-17 12:19:23 -05:00
|
|
|
}
|
2022-04-15 07:01:58 -05:00
|
|
|
return response.JSONStreaming(http.StatusOK, frame)
|
2022-03-17 12:19:23 -05:00
|
|
|
}
|
2022-07-29 01:26:44 -05:00
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (s *standardStorageService) getConfig(c *contextmodel.ReqContext) response.Response {
|
2022-07-29 01:26:44 -05:00
|
|
|
roots := make([]RootStorageMeta, 0)
|
2023-10-09 03:40:19 -05:00
|
|
|
orgId := c.SignedInUser.GetOrgID()
|
2022-07-29 01:26:44 -05:00
|
|
|
t := s.tree
|
|
|
|
t.assureOrgIsInitialized(orgId)
|
2022-07-29 14:58:47 -05:00
|
|
|
|
|
|
|
storages := t.getStorages(orgId)
|
|
|
|
for _, s := range storages {
|
|
|
|
roots = append(roots, s.Meta())
|
2022-07-29 01:26:44 -05:00
|
|
|
}
|
2024-02-27 10:39:51 -06:00
|
|
|
return response.JSON(http.StatusOK, roots)
|
2022-07-29 01:26:44 -05:00
|
|
|
}
|