2022-03-17 12:19:23 -05:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
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"
|
|
|
|
"github.com/grafana/grafana/pkg/models"
|
|
|
|
"github.com/grafana/grafana/pkg/web"
|
|
|
|
)
|
|
|
|
|
|
|
|
// HTTPStorageService passes raw HTTP requests to a well typed storage service
|
|
|
|
type HTTPStorageService interface {
|
|
|
|
List(c *models.ReqContext) response.Response
|
|
|
|
Read(c *models.ReqContext) response.Response
|
|
|
|
Delete(c *models.ReqContext) response.Response
|
|
|
|
Upload(c *models.ReqContext) response.Response
|
|
|
|
}
|
|
|
|
|
|
|
|
type httpStorage struct {
|
|
|
|
store StorageService
|
|
|
|
}
|
|
|
|
|
|
|
|
func ProvideHTTPService(store StorageService) HTTPStorageService {
|
|
|
|
return &httpStorage{
|
|
|
|
store: store,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpStorage) Upload(c *models.ReqContext) response.Response {
|
2022-04-27 14:12:48 -05:00
|
|
|
// 32 MB is the default used by FormFile()
|
|
|
|
if err := c.Req.ParseMultipartForm(32 << 20); err != nil {
|
|
|
|
return response.Error(400, "error in parsing form", err)
|
|
|
|
}
|
|
|
|
const MAX_UPLOAD_SIZE = 1024 * 1024
|
|
|
|
c.Req.Body = http.MaxBytesReader(c.Resp, c.Req.Body, MAX_UPLOAD_SIZE)
|
|
|
|
if err := c.Req.ParseMultipartForm(MAX_UPLOAD_SIZE); err != nil {
|
|
|
|
return response.Error(400, "Please limit file uploaded under 1MB", err)
|
|
|
|
}
|
|
|
|
res, err := s.store.Upload(c.Req.Context(), c.SignedInUser, c.Req.MultipartForm)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(500, "Internal Server Error", err)
|
|
|
|
}
|
2022-03-17 12:19:23 -05:00
|
|
|
|
2022-04-27 14:12:48 -05:00
|
|
|
return response.JSON(res.statusCode, map[string]interface{}{
|
|
|
|
"message": res.message,
|
|
|
|
"path": res.path,
|
|
|
|
"file": res.fileName,
|
|
|
|
"err": true,
|
2022-03-17 12:19:23 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpStorage) Read(c *models.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-04-27 14:12:48 -05:00
|
|
|
file, err := s.store.Read(c.Req.Context(), c.SignedInUser, scope+"/"+path)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(400, "cannot call read", err)
|
|
|
|
}
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpStorage) Delete(c *models.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
|
|
|
|
_, path := getPathAndScope(c)
|
|
|
|
err := s.store.Delete(c.Req.Context(), c.SignedInUser, "/"+path)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(400, "cannot call delete", err)
|
|
|
|
}
|
|
|
|
return response.JSON(200, map[string]string{
|
|
|
|
"message": "Removed file from storage",
|
|
|
|
"path": path,
|
2022-03-17 12:19:23 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpStorage) List(c *models.ReqContext) response.Response {
|
|
|
|
params := web.Params(c.Req)
|
|
|
|
path := params["*"]
|
|
|
|
frame, err := s.store.List(c.Req.Context(), c.SignedInUser, path)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(400, "error reading path", err)
|
|
|
|
}
|
|
|
|
if frame == nil {
|
|
|
|
return response.Error(404, "not found", nil)
|
|
|
|
}
|
2022-04-15 07:01:58 -05:00
|
|
|
return response.JSONStreaming(http.StatusOK, frame)
|
2022-03-17 12:19:23 -05:00
|
|
|
}
|