2014-12-29 06:36:08 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2020-11-19 06:34:28 -06:00
|
|
|
"errors"
|
2015-01-29 05:10:34 -06:00
|
|
|
"fmt"
|
2014-12-29 06:36:08 -06:00
|
|
|
"net/http"
|
2018-05-24 08:26:27 -05:00
|
|
|
"strconv"
|
|
|
|
"time"
|
2014-12-29 06:36:08 -06:00
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2023-10-09 09:07:28 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/auth/identity"
|
2023-01-27 01:50:36 -06:00
|
|
|
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
|
2018-05-24 08:26:27 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/rendering"
|
2015-02-05 03:37:13 -06:00
|
|
|
"github.com/grafana/grafana/pkg/util"
|
2021-10-11 07:30:59 -05:00
|
|
|
"github.com/grafana/grafana/pkg/web"
|
2014-12-29 06:36:08 -06:00
|
|
|
)
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (hs *HTTPServer) RenderToPng(c *contextmodel.ReqContext) {
|
2019-01-28 15:18:48 -06:00
|
|
|
queryReader, err := util.NewURLQueryReader(c.Req.URL)
|
2017-11-28 08:20:22 -06:00
|
|
|
if err != nil {
|
2020-12-15 12:09:04 -06:00
|
|
|
c.Handle(hs.Cfg, 400, "Render parameters error", err)
|
2017-11-28 08:20:22 -06:00
|
|
|
return
|
|
|
|
}
|
2018-05-24 08:26:27 -05:00
|
|
|
|
2015-03-20 18:14:13 -05:00
|
|
|
queryParams := fmt.Sprintf("?%s", c.Req.URL.RawQuery)
|
2014-12-29 06:36:08 -06:00
|
|
|
|
2018-05-24 08:26:27 -05:00
|
|
|
width, err := strconv.Atoi(queryReader.Get("width", "800"))
|
|
|
|
if err != nil {
|
2020-12-15 12:09:04 -06:00
|
|
|
c.Handle(hs.Cfg, 400, "Render parameters error", fmt.Errorf("cannot parse width as int: %s", err))
|
2018-05-24 08:26:27 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
height, err := strconv.Atoi(queryReader.Get("height", "400"))
|
|
|
|
if err != nil {
|
2020-12-15 12:09:04 -06:00
|
|
|
c.Handle(hs.Cfg, 400, "Render parameters error", fmt.Errorf("cannot parse height as int: %s", err))
|
2018-05-24 08:26:27 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
timeout, err := strconv.Atoi(queryReader.Get("timeout", "60"))
|
|
|
|
if err != nil {
|
2020-12-15 12:09:04 -06:00
|
|
|
c.Handle(hs.Cfg, 400, "Render parameters error", fmt.Errorf("cannot parse timeout as int: %s", err))
|
2018-05-24 08:26:27 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-21 09:16:41 -05:00
|
|
|
scale, err := strconv.ParseFloat(queryReader.Get("scale", "1"), 64)
|
|
|
|
if err != nil {
|
2020-12-15 12:09:04 -06:00
|
|
|
c.Handle(hs.Cfg, 400, "Render parameters error", fmt.Errorf("cannot parse scale as float: %s", err))
|
2020-04-21 09:16:41 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
headers := http.Header{}
|
|
|
|
acceptLanguageHeader := c.Req.Header.Values("Accept-Language")
|
|
|
|
if len(acceptLanguageHeader) > 0 {
|
|
|
|
headers["Accept-Language"] = acceptLanguageHeader
|
|
|
|
}
|
|
|
|
|
2023-10-09 09:07:28 -05:00
|
|
|
userID, errID := identity.UserIdentifier(c.SignedInUser.GetNamespacedID())
|
|
|
|
if errID != nil {
|
|
|
|
hs.log.Error("Failed to parse user id", "err", errID)
|
|
|
|
}
|
|
|
|
|
2018-05-24 08:26:27 -05:00
|
|
|
result, err := hs.RenderService.Render(c.Req.Context(), rendering.Opts{
|
2022-01-26 16:02:19 -06:00
|
|
|
TimeoutOpts: rendering.TimeoutOpts{
|
|
|
|
Timeout: time.Duration(timeout) * time.Second,
|
|
|
|
},
|
|
|
|
AuthOpts: rendering.AuthOpts{
|
2023-10-06 04:34:36 -05:00
|
|
|
OrgID: c.SignedInUser.GetOrgID(),
|
2023-10-09 09:07:28 -05:00
|
|
|
UserID: userID,
|
|
|
|
OrgRole: c.SignedInUser.GetOrgRole(),
|
2022-01-26 16:02:19 -06:00
|
|
|
},
|
2020-04-21 09:16:41 -05:00
|
|
|
Width: width,
|
|
|
|
Height: height,
|
2021-10-11 07:30:59 -05:00
|
|
|
Path: web.Params(c.Req)["*"] + queryParams,
|
2020-04-21 09:16:41 -05:00
|
|
|
Timezone: queryReader.Get("tz", ""),
|
|
|
|
Encoding: queryReader.Get("encoding", ""),
|
2020-04-27 10:25:08 -05:00
|
|
|
ConcurrentLimit: hs.Cfg.RendererConcurrentRequestLimit,
|
2020-04-21 09:16:41 -05:00
|
|
|
DeviceScaleFactor: scale,
|
|
|
|
Headers: headers,
|
2022-02-09 03:23:32 -06:00
|
|
|
Theme: models.ThemeDark,
|
2022-01-26 16:02:19 -06:00
|
|
|
}, nil)
|
2020-11-19 06:34:28 -06:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, rendering.ErrTimeout) {
|
2020-12-15 12:09:04 -06:00
|
|
|
c.Handle(hs.Cfg, 500, err.Error(), err)
|
2020-11-19 06:34:28 -06:00
|
|
|
return
|
|
|
|
}
|
2018-06-07 08:15:13 -05:00
|
|
|
|
2020-12-15 12:09:04 -06:00
|
|
|
c.Handle(hs.Cfg, 500, "Rendering failed.", err)
|
2015-02-05 05:23:24 -06:00
|
|
|
return
|
2014-12-29 06:36:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
c.Resp.Header().Set("Content-Type", "image/png")
|
2021-09-01 04:18:30 -05:00
|
|
|
http.ServeFile(c.Resp, c.Req, result.FilePath)
|
2014-12-29 06:36:08 -06:00
|
|
|
}
|