@@ -27,16 +27,57 @@ namespace QgsWmts
27
27
QgsServerResponse &response )
28
28
{
29
29
Q_UNUSED ( version );
30
-
31
30
QgsServerRequest::Parameters params = request.parameters ();
32
31
33
32
// WMS query
34
33
QUrlQuery query = translateWmtsParamToWmsQueryItem ( QStringLiteral ( " GetMap" ), params, project, serverIface );
35
34
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
+
36
71
QgsServerParameters wmsParams ( query );
37
72
QgsServerRequest wmsRequest ( " ?" + query.query ( QUrl::FullyDecoded ) );
38
73
QgsService *service = serverIface->serviceRegistry ()->getService ( wmsParams.service (), wmsParams.version () );
39
74
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
+ }
40
81
}
41
82
42
83
} // namespace QgsWmts
0 commit comments