Skip to content

Commit 8c67e45

Browse files
committedOct 6, 2018
[Server][Cache] Add WMS GetLegendGraphic to store in cache
1 parent f79be97 commit 8c67e45

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed
 

‎src/server/services/wms/qgswmsgetlegendgraphics.cpp

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,60 @@ namespace QgsWms
3434
Q_UNUSED( version );
3535

3636
QgsServerRequest::Parameters params = request.parameters();
37+
QString format = params.value( QStringLiteral( "FORMAT" ), QStringLiteral( "PNG" ) );
3738

3839
QgsWmsParameters wmsParameters( QUrlQuery( request.url() ) );
40+
41+
// Get cached image
42+
QgsAccessControl *accessControl = serverIface->accessControls();
43+
QgsServerCacheManager *cacheManager = serverIface->cacheManager();
44+
if ( cacheManager )
45+
{
46+
ImageOutputFormat outputFormat = parseImageFormat( format );
47+
QString saveFormat;
48+
QString contentType;
49+
switch ( outputFormat )
50+
{
51+
case PNG:
52+
case PNG8:
53+
case PNG16:
54+
case PNG1:
55+
contentType = "image/png";
56+
saveFormat = "PNG";
57+
break;
58+
case JPEG:
59+
contentType = "image/jpeg";
60+
saveFormat = "JPEG";
61+
break;
62+
default:
63+
throw QgsServiceException( "InvalidFormat",
64+
QString( "Output format '%1' is not supported in the GetLegendGraphic request" ).arg( format ) );
65+
break;
66+
}
67+
68+
QImage image;
69+
QByteArray content = cacheManager->getCachedImage( project, request, accessControl );
70+
if ( !content.isEmpty() && image.loadFromData( content ) )
71+
{
72+
response.setHeader( QStringLiteral( "Content-Type" ), contentType );
73+
image.save( response.io(), qPrintable( saveFormat ) );
74+
return;
75+
}
76+
}
77+
3978
QgsRenderer renderer( serverIface, project, wmsParameters );
4079

4180
std::unique_ptr<QImage> result( renderer.getLegendGraphics() );
4281

4382
if ( result )
4483
{
45-
QString format = params.value( QStringLiteral( "FORMAT" ), QStringLiteral( "PNG" ) );
4684
writeImage( response, *result, format, renderer.getImageQuality() );
85+
if ( cacheManager )
86+
{
87+
QByteArray content = response.data();
88+
if ( !content.isEmpty() )
89+
cacheManager->setCachedImage( &content, project, request, accessControl );
90+
}
4791
}
4892
else
4993
{

0 commit comments

Comments
 (0)
Please sign in to comment.