21 #include <QMimeDatabase>
25 : mPathRegExp( pathRegExp )
26 , mStaticPathSuffix( staticPathSuffix )
33 const QRegularExpressionMatch match {
path().match( context.
request()->
url().path( ) ) };
34 if ( ! match.hasMatch() )
39 const QString staticFilePath { match.captured( QStringLiteral(
"staticFilePath" ) ) };
42 if ( ! mStaticPathSuffix.isEmpty() )
44 filePath +=
'/' + mStaticPathSuffix;
46 filePath +=
'/' + staticFilePath;
47 if ( ! QFile::exists( filePath ) )
49 QgsMessageLog::logMessage( QStringLiteral(
"Static file was not found: %1" ).arg( filePath ), QStringLiteral(
"Server" ), Qgis::MessageLevel::Info );
54 if ( ! f.open( QIODevice::ReadOnly ) )
59 const qint64 size { f.size() };
60 const QByteArray content { f.readAll() };
61 const QMimeType mimeType { QMimeDatabase().mimeTypeForFile( filePath )};
62 context.
response()->
setHeader( QStringLiteral(
"Content-Type" ), mimeType.name() );
63 context.
response()->
setHeader( QStringLiteral(
"Content-Length" ), QString::number( size ) );
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
The QgsServerApiContext class encapsulates the resources for a particular client request: the request...
QgsServerResponse * response() const
Returns the server response object.
const QgsServerRequest * request() const
Returns the server request object.
Internal server error API exception.
Not found error API exception.
virtual const QString staticPath(const QgsServerApiContext &context) const
Returns the absolute path to the base directory where static resources for this handler are stored in...
void setContentTypes(const QList< QgsServerOgcApi::ContentType > &contentTypes)
Set the content types to contentTypes.
virtual void write(const QString &data)
Write string This is a convenient method that will write directly to the underlying I/O device.
virtual void setHeader(const QString &key, const QString &value)=0
Set Header entry Add Header entry to the response Note that it is usually an error to set Header afte...
QRegularExpression path() const override
URL pattern for this handler, named capture group are automatically extracted and returned by values(...
void handleRequest(const QgsServerApiContext &context) const override
Handles the request within its context.
QgsServerStaticHandler(const QString &pathRegExp=QStringLiteral("/static/(?<staticFilePath>.*)$"), const QString &staticPathSuffix=QString())
Creates QgsServerStaticHandler.