2014-12-29 06:36:08 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2015-01-29 05:10:34 -06:00
|
|
|
"fmt"
|
2014-12-29 06:36:08 -06:00
|
|
|
"net/http"
|
2018-06-07 08:15:13 -05:00
|
|
|
"runtime"
|
2018-05-24 08:26:27 -05:00
|
|
|
"strconv"
|
2018-06-07 08:15:13 -05:00
|
|
|
"strings"
|
2018-05-24 08:26:27 -05:00
|
|
|
"time"
|
2014-12-29 06:36:08 -06:00
|
|
|
|
2018-03-06 16:59:45 -06:00
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
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"
|
2014-12-29 06:36:08 -06:00
|
|
|
)
|
|
|
|
|
2018-05-24 08:26:27 -05:00
|
|
|
func (hs *HTTPServer) RenderToPng(c *m.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 {
|
2017-12-01 09:23:55 -06:00
|
|
|
c.Handle(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 {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
result, err := hs.RenderService.Render(c.Req.Context(), rendering.Opts{
|
2018-09-25 05:17:04 -05:00
|
|
|
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: 30,
|
2018-05-24 08:26:27 -05:00
|
|
|
})
|
2015-02-05 05:23:24 -06:00
|
|
|
|
2018-05-24 08:26:27 -05:00
|
|
|
if err != nil && err == rendering.ErrTimeout {
|
2017-12-28 07:37:10 -06:00
|
|
|
c.Handle(500, err.Error(), err)
|
|
|
|
return
|
|
|
|
}
|
2017-07-31 09:04:04 -05:00
|
|
|
|
2018-06-07 08:15:13 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-12-28 07:37:10 -06:00
|
|
|
if err != nil {
|
2017-07-31 09:04:04 -05:00
|
|
|
c.Handle(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")
|
2018-05-24 08:26:27 -05:00
|
|
|
http.ServeFile(c.Resp, c.Req.Request, result.FilePath)
|
2014-12-29 06:36:08 -06:00
|
|
|
}
|