mirror of
https://github.com/grafana/grafana.git
synced 2024-11-26 19:00:54 -06:00
76650e60e4
Fixes #23806 Co-Authored-By: Torkel Ödegaard <torkel@grafana.com> Co-Authored-By: Arve Knudsen <arve.knudsen@gmail.com>
92 lines
2.5 KiB
Go
92 lines
2.5 KiB
Go
package api
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"runtime"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/grafana/grafana/pkg/models"
|
|
"github.com/grafana/grafana/pkg/services/rendering"
|
|
"github.com/grafana/grafana/pkg/util"
|
|
)
|
|
|
|
func (hs *HTTPServer) RenderToPng(c *models.ReqContext) {
|
|
queryReader, err := util.NewURLQueryReader(c.Req.URL)
|
|
if err != nil {
|
|
c.Handle(400, "Render parameters error", err)
|
|
return
|
|
}
|
|
|
|
queryParams := fmt.Sprintf("?%s", c.Req.URL.RawQuery)
|
|
|
|
width, err := strconv.Atoi(queryReader.Get("width", "800"))
|
|
if err != nil {
|
|
c.Handle(400, "Render parameters error", fmt.Errorf("Cannot parse width as int: %s", err))
|
|
return
|
|
}
|
|
|
|
height, err := strconv.Atoi(queryReader.Get("height", "400"))
|
|
if err != nil {
|
|
c.Handle(400, "Render parameters error", fmt.Errorf("Cannot parse height as int: %s", err))
|
|
return
|
|
}
|
|
|
|
timeout, err := strconv.Atoi(queryReader.Get("timeout", "60"))
|
|
if err != nil {
|
|
c.Handle(400, "Render parameters error", fmt.Errorf("Cannot parse timeout as int: %s", err))
|
|
return
|
|
}
|
|
|
|
scale, err := strconv.ParseFloat(queryReader.Get("scale", "1"), 64)
|
|
if err != nil {
|
|
c.Handle(400, "Render parameters error", fmt.Errorf("Cannot parse scale as float: %s", err))
|
|
return
|
|
}
|
|
|
|
headers := http.Header{}
|
|
acceptLanguageHeader := c.Req.Header.Values("Accept-Language")
|
|
if len(acceptLanguageHeader) > 0 {
|
|
headers["Accept-Language"] = acceptLanguageHeader
|
|
}
|
|
|
|
result, err := hs.RenderService.Render(c.Req.Context(), rendering.Opts{
|
|
Width: width,
|
|
Height: height,
|
|
Timeout: time.Duration(timeout) * time.Second,
|
|
OrgId: c.OrgId,
|
|
UserId: c.UserId,
|
|
OrgRole: c.OrgRole,
|
|
Path: c.Params("*") + queryParams,
|
|
Timezone: queryReader.Get("tz", ""),
|
|
Encoding: queryReader.Get("encoding", ""),
|
|
ConcurrentLimit: hs.Cfg.RendererConcurrentRequestLimit,
|
|
DeviceScaleFactor: scale,
|
|
Headers: headers,
|
|
})
|
|
|
|
if err != nil && err == rendering.ErrTimeout {
|
|
c.Handle(500, err.Error(), err)
|
|
return
|
|
}
|
|
|
|
if err != nil && err == rendering.ErrPhantomJSNotInstalled {
|
|
if strings.HasPrefix(runtime.GOARCH, "arm") {
|
|
c.Handle(500, "Rendering failed - PhantomJS isn't included in arm build per default", err)
|
|
} else {
|
|
c.Handle(500, "Rendering failed - PhantomJS isn't installed correctly", err)
|
|
}
|
|
return
|
|
}
|
|
|
|
if err != nil {
|
|
c.Handle(500, "Rendering failed.", err)
|
|
return
|
|
}
|
|
|
|
c.Resp.Header().Set("Content-Type", "image/png")
|
|
http.ServeFile(c.Resp, c.Req.Request, result.FilePath)
|
|
}
|