Skip to content

Commit 05e9ab9

Browse files
committed
[Server][Feature][needs-docs] Update WMTS service to use cache manager for tiles
1 parent 059232a commit 05e9ab9

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

src/server/services/wmts/qgswmtsgettile.cpp

+42-1
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,57 @@ namespace QgsWmts
2727
QgsServerResponse &response )
2828
{
2929
Q_UNUSED( version );
30-
3130
QgsServerRequest::Parameters params = request.parameters();
3231

3332
// WMS query
3433
QUrlQuery query = translateWmtsParamToWmsQueryItem( QStringLiteral( "GetMap" ), params, project, serverIface );
3534

35+
// Get cached image
36+
QStringList cacheKeyList;
37+
bool cache = true;
38+
39+
QgsAccessControl *accessControl = serverIface->accessControls();
40+
if ( accessControl )
41+
cache = accessControl->fillCacheKey( cacheKeyList );
42+
43+
QString cacheKey = cacheKeyList.join( QStringLiteral( "-" ) );
44+
QgsServerCacheManager *cacheManager = serverIface->cacheManager();
45+
if ( cacheManager && cache )
46+
{
47+
QString contentType = params.value( QStringLiteral( "FORMAT" ) );
48+
QString saveFormat;
49+
std::unique_ptr<QImage> image;
50+
if ( contentType == "image/jpeg" )
51+
{
52+
saveFormat = "JPEG";
53+
image = qgis::make_unique<QImage>( 256, 256, QImage::Format_RGB32 );
54+
}
55+
else
56+
{
57+
saveFormat = "PNG";
58+
image = qgis::make_unique<QImage>( 256, 256, QImage::Format_ARGB32_Premultiplied );
59+
}
60+
61+
QByteArray content = cacheManager->getCachedImage( project, request, cacheKey );
62+
if ( !content.isEmpty() && image->loadFromData( content ) )
63+
{
64+
response.setHeader( "Content-Type", contentType );
65+
image->save( response.io(), qPrintable( saveFormat ) );
66+
return;
67+
}
68+
}
69+
70+
3671
QgsServerParameters wmsParams( query );
3772
QgsServerRequest wmsRequest( "?" + query.query( QUrl::FullyDecoded ) );
3873
QgsService *service = serverIface->serviceRegistry()->getService( wmsParams.service(), wmsParams.version() );
3974
service->executeRequest( wmsRequest, response, project );
75+
if ( cache && cacheManager )
76+
{
77+
QByteArray content = response.data();
78+
if ( !content.isEmpty() )
79+
cacheManager->setCachedImage( &content, project, request, cacheKey );
80+
}
4081
}
4182

4283
} // namespace QgsWmts

0 commit comments

Comments
 (0)