QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
qgsserverstatichandler.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsserverstatichandler.cpp - QgsServerStaticHandler
3
4 ---------------------
5 begin : 30.7.2020
6 copyright : (C) 2020 by Alessandro Pasotti
7 email : elpaso at itopen dot it
8 ***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
17#include "qgsmessagelog.h"
18#include "qgsserverresponse.h"
19
20#include <QFile>
21#include <QMimeDatabase>
22
23
24QgsServerStaticHandler::QgsServerStaticHandler( const QString &pathRegExp, const QString &staticPathSuffix )
25 : mPathRegExp( pathRegExp )
26 , mStaticPathSuffix( staticPathSuffix )
27{
29}
30
32{
33 const QRegularExpressionMatch match { path().match( context.request()->url().path( ) ) };
34 if ( ! match.hasMatch() )
35 {
36 throw QgsServerApiNotFoundError( QStringLiteral( "Static file was not found" ) );
37 }
38
39 const QString staticFilePath { match.captured( QStringLiteral( "staticFilePath" ) ) };
40 // Calculate real path
41 QString filePath { staticPath( context ) };
42 if ( ! mStaticPathSuffix.isEmpty() )
43 {
44 filePath += '/' + mStaticPathSuffix;
45 }
46 filePath += '/' + staticFilePath;
47 if ( ! QFile::exists( filePath ) )
48 {
49 QgsMessageLog::logMessage( QStringLiteral( "Static file was not found: %1" ).arg( filePath ), QStringLiteral( "Server" ), Qgis::MessageLevel::Info );
50 throw QgsServerApiNotFoundError( QStringLiteral( "Static file %1 was not found" ).arg( staticFilePath ) );
51 }
52
53 QFile f( filePath );
54 if ( ! f.open( QIODevice::ReadOnly ) )
55 {
56 throw QgsServerApiInternalServerError( QStringLiteral( "Could not open static file %1" ).arg( staticFilePath ) );
57 }
58
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 ) );
64 context.response()->write( content );
65}
@ Info
Information message.
Definition qgis.h:155
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.