9#include <Cutelyst/Application>
10#include <Cutelyst/Request>
11#include <Cutelyst/Response>
15#include <QLoggingCategory>
17Q_LOGGING_CATEGORY(C_SERVER_SM,
"cutelyst.server.staticmap", QtWarningMsg)
20using namespace Qt::Literals::StringLiterals;
34void StaticMap::addStaticMap(
const QString &mountPoint,
const QString &path,
bool append)
36 QString mp = mountPoint;
37 if (!mp.startsWith(u
'/')) {
41 qCInfo(C_SERVER_SM) <<
"added mapping for" << mp <<
"=>" << path;
43 m_staticMaps.push_back({mp, path, append});
44 std::sort(m_staticMaps.begin(),
46 [](
const MountPoint &a,
const MountPoint &b) ->
bool {
47 return a.mountPoint.size() < b.mountPoint.size();
57 const QString path = c->
req()->path();
58 for (
const MountPoint &mp : m_staticMaps) {
59 if (path.startsWith(mp.mountPoint)) {
60 if (tryToServeFile(c, mp, path)) {
70 QString localPath = path;
72 localPath = path.mid(mp.mountPoint.size());
74 while (localPath.startsWith(u
'/')) {
75 localPath.remove(0, 1);
79 QString absFilePath = dir.absoluteFilePath(localPath);
80 if (!QFile::exists(absFilePath)) {
84 return serveFile(c, absFilePath);
90 const QDateTime currentDateTime = QFileInfo(filename).lastModified();
92 res->setStatus(Response::NotModified);
96 auto file =
new QFile(filename);
97 if (file->open(QFile::ReadOnly)) {
98 qCDebug(C_SERVER_SM) <<
"Serving" << filename;
99 Headers &headers = res->headers();
105 QMimeType mimeType = m_db.mimeTypeForFile(filename, QMimeDatabase::MatchExtension);
106 if (mimeType.isValid()) {
112 headers.
setHeader(
"Cache-Control"_ba,
"public"_ba);
117 qCWarning(C_SERVER_SM) <<
"Could not serve" << filename << file->errorString();
122#include "moc_staticmap.cpp"
The Cutelyst application.
void beforePrepareAction(Cutelyst::Context *c, bool *skipMethod)
Response * response() const noexcept
Base class for Cutelyst Plugins.
Headers headers() const noexcept
virtual bool setup(Cutelyst::Application *app) override
The Cutelyst namespace holds all public Cutelyst API.