2014-12-29 20:36:08 +08:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2020-11-19 20:34:28 +08:00
|
|
|
"errors"
|
2015-01-29 19:10:34 +08:00
|
|
|
"fmt"
|
2014-12-29 20:36:08 +08:00
|
|
|
"net/http"
|
2018-05-24 21:26:27 +08:00
|
|
|
"strconv"
|
|
|
|
"time"
|
2014-12-29 20:36:08 +08:00
|
|
|
|
2024-06-13 12:11:35 +08:00
|
|
|
"github.com/grafana/grafana/pkg/apimachinery/identity"
|
2020-03-04 19:57:20 +08:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2023-01-27 15:50:36 +08:00
|
|
|
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
|
2018-05-24 21:26:27 +08:00
|
|
|
"github.com/grafana/grafana/pkg/services/rendering"
|
2015-02-05 17:37:13 +08:00
|
|
|
"github.com/grafana/grafana/pkg/util"
|
2021-10-11 20:30:59 +08:00
|
|
|
"github.com/grafana/grafana/pkg/web"
|
2014-12-29 20:36:08 +08:00
|
|
|
)
|
|
|
|
|
2024-05-14 18:24:18 +08:00
|
|
|
func (hs *HTTPServer) RenderHandler(c *contextmodel.ReqContext) {
|
2019-01-29 05:18:48 +08:00
|
|
|
queryReader, err := util.NewURLQueryReader(c.Req.URL)
|
2017-11-28 22:20:22 +08:00
|
|
|
if err != nil {
|
2024-02-28 00:39:51 +08:00
|
|
|
c.Handle(hs.Cfg, http.StatusBadRequest, "Render parameters error", err)
|
2017-11-28 22:20:22 +08:00
|
|
|
return
|
|
|
|
}
|
2018-05-24 21:26:27 +08:00
|
|
|
|
2015-03-21 07:14:13 +08:00
|
|
|
queryParams := fmt.Sprintf("?%s", c.Req.URL.RawQuery)
|
2014-12-29 20:36:08 +08:00
|
|
|
|
2024-02-26 20:27:34 +08:00
|
|
|
width := c.QueryInt("width")
|
|
|
|
if width == 0 {
|
|
|
|
width = hs.Cfg.RendererDefaultImageWidth
|
2018-05-24 21:26:27 +08:00
|
|
|
}
|
|
|
|
|
2024-02-26 20:27:34 +08:00
|
|
|
height := c.QueryInt("height")
|
|
|
|
if height == 0 {
|
|
|
|
height = hs.Cfg.RendererDefaultImageHeight
|
2018-05-24 21:26:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
timeout, err := strconv.Atoi(queryReader.Get("timeout", "60"))
|
|
|
|
if err != nil {
|
2024-02-28 00:39:51 +08:00
|
|
|
c.Handle(hs.Cfg, http.StatusBadRequest, "Render parameters error", fmt.Errorf("cannot parse timeout as int: %s", err))
|
2018-05-24 21:26:27 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-26 20:27:34 +08:00
|
|
|
scale := c.QueryFloat64("scale")
|
|
|
|
if scale == 0 {
|
|
|
|
scale = hs.Cfg.RendererDefaultImageScale
|
2020-04-21 22:16:41 +08:00
|
|
|
}
|
|
|
|
|
2024-05-25 03:12:31 +08:00
|
|
|
theme := c.QueryStrings("theme")
|
|
|
|
var themeModel models.Theme
|
|
|
|
if len(theme) > 0 {
|
|
|
|
themeStr := theme[0]
|
|
|
|
_, err := models.ParseTheme(themeStr)
|
|
|
|
if err != nil {
|
|
|
|
c.Handle(hs.Cfg, http.StatusBadRequest, "Render parameters error: theme can only be light or dark", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
themeModel = models.Theme(themeStr)
|
|
|
|
} else {
|
|
|
|
themeModel = models.ThemeDark
|
|
|
|
}
|
|
|
|
|
2020-04-21 22:16:41 +08:00
|
|
|
headers := http.Header{}
|
|
|
|
acceptLanguageHeader := c.Req.Header.Values("Accept-Language")
|
|
|
|
if len(acceptLanguageHeader) > 0 {
|
|
|
|
headers["Accept-Language"] = acceptLanguageHeader
|
|
|
|
}
|
|
|
|
|
2025-04-10 20:42:23 +08:00
|
|
|
userID, err := identity.UserIdentifier(c.GetID())
|
2024-08-09 23:20:24 +08:00
|
|
|
if err != nil {
|
|
|
|
hs.log.Debug("Failed to parse user id", "err", err)
|
2023-10-09 22:07:28 +08:00
|
|
|
}
|
|
|
|
|
2024-02-08 20:09:34 +08:00
|
|
|
encoding := queryReader.Get("encoding", "")
|
|
|
|
|
2024-05-14 18:24:18 +08:00
|
|
|
renderType := rendering.RenderPNG
|
|
|
|
if encoding == "pdf" {
|
|
|
|
renderType = rendering.RenderPDF
|
|
|
|
}
|
|
|
|
|
|
|
|
result, err := hs.RenderService.Render(c.Req.Context(), renderType, rendering.Opts{
|
|
|
|
CommonOpts: rendering.CommonOpts{
|
|
|
|
TimeoutOpts: rendering.TimeoutOpts{
|
|
|
|
Timeout: time.Duration(timeout) * time.Second,
|
|
|
|
},
|
|
|
|
AuthOpts: rendering.AuthOpts{
|
2025-04-10 20:42:23 +08:00
|
|
|
OrgID: c.GetOrgID(),
|
2024-05-14 18:24:18 +08:00
|
|
|
UserID: userID,
|
2025-04-10 20:42:23 +08:00
|
|
|
OrgRole: c.GetOrgRole(),
|
2024-05-14 18:24:18 +08:00
|
|
|
},
|
|
|
|
Path: web.Params(c.Req)["*"] + queryParams,
|
|
|
|
Timezone: queryReader.Get("tz", ""),
|
|
|
|
ConcurrentLimit: hs.Cfg.RendererConcurrentRequestLimit,
|
|
|
|
Headers: headers,
|
2022-01-27 06:02:19 +08:00
|
|
|
},
|
2020-04-21 22:16:41 +08:00
|
|
|
Width: width,
|
|
|
|
Height: height,
|
|
|
|
DeviceScaleFactor: scale,
|
2024-05-25 03:12:31 +08:00
|
|
|
Theme: themeModel,
|
2022-01-27 06:02:19 +08:00
|
|
|
}, nil)
|
2020-11-19 20:34:28 +08:00
|
|
|
if err != nil {
|
2025-04-24 21:31:19 +08:00
|
|
|
if errors.Is(err, rendering.ErrTooManyRequests) {
|
|
|
|
c.JsonApiErr(http.StatusTooManyRequests, "Too many rendering requests", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-19 20:34:28 +08:00
|
|
|
if errors.Is(err, rendering.ErrTimeout) {
|
2024-02-28 00:39:51 +08:00
|
|
|
c.Handle(hs.Cfg, http.StatusInternalServerError, err.Error(), err)
|
2020-11-19 20:34:28 +08:00
|
|
|
return
|
|
|
|
}
|
2018-06-07 21:15:13 +08:00
|
|
|
|
2024-02-28 00:39:51 +08:00
|
|
|
c.Handle(hs.Cfg, http.StatusInternalServerError, "Rendering failed.", err)
|
2015-02-05 19:23:24 +08:00
|
|
|
return
|
2014-12-29 20:36:08 +08:00
|
|
|
}
|
|
|
|
|
2024-02-08 20:09:34 +08:00
|
|
|
if encoding == "pdf" {
|
|
|
|
c.Resp.Header().Set("Content-Type", "application/pdf")
|
|
|
|
} else {
|
|
|
|
c.Resp.Header().Set("Content-Type", "image/png")
|
|
|
|
}
|
|
|
|
|
2023-12-18 20:21:57 +08:00
|
|
|
c.Resp.Header().Set("Cache-Control", "private")
|
2021-09-01 17:18:30 +08:00
|
|
|
http.ServeFile(c.Resp, c.Req, result.FilePath)
|
2014-12-29 20:36:08 +08:00
|
|
|
}
|