From 07b514b05ad4a8fe3d0c18bb3ac0ad67c4363614 Mon Sep 17 00:00:00 2001 From: Thomas Piccirello Date: Wed, 2 Oct 2024 10:57:45 -0700 Subject: [PATCH] Add concept of configurable WebUI URL base path This provides the underlying support for having a configurable base path. Future commit(s) will expose this functionality to users. --- src/base/preferences.cpp | 13 +++++++++++++ src/base/preferences.h | 2 ++ src/webui/webapplication.cpp | 30 +++++++++++++++++++++--------- src/webui/webapplication.h | 1 + 4 files changed, 37 insertions(+), 9 deletions(-) diff --git a/src/base/preferences.cpp b/src/base/preferences.cpp index ed9ecd316dc1..782126b64443 100644 --- a/src/base/preferences.cpp +++ b/src/base/preferences.cpp @@ -1026,6 +1026,19 @@ void Preferences::setWebUITrustedReverseProxiesList(const QString &addr) setValue(u"Preferences/WebUI/TrustedReverseProxiesList"_s, addr); } +QString Preferences::getWebUIBasePath() const +{ + return value(u"Preferences/WebUI/BasePath"_s, u"/"_s); +} + +void Preferences::setWebUIBasePath(const QString &path) +{ + if (path == getWebUIBasePath()) + return; + + setValue(u"Preferences/WebUI/BasePath"_s, path); +} + bool Preferences::isDynDNSEnabled() const { return value(u"Preferences/DynDNS/Enabled"_s, false); diff --git a/src/base/preferences.h b/src/base/preferences.h index b1a912477bda..8ad17f5fb9cd 100644 --- a/src/base/preferences.h +++ b/src/base/preferences.h @@ -237,6 +237,8 @@ class Preferences final : public QObject void setWebUIReverseProxySupportEnabled(bool enabled); QString getWebUITrustedReverseProxiesList() const; void setWebUITrustedReverseProxiesList(const QString &addr); + QString getWebUIBasePath() const; + void setWebUIBasePath(const QString &path); // Dynamic DNS bool isDynDNSEnabled() const; diff --git a/src/webui/webapplication.cpp b/src/webui/webapplication.cpp index 4d991371e7dc..245bbc06e91b 100644 --- a/src/webui/webapplication.cpp +++ b/src/webui/webapplication.cpp @@ -495,6 +495,13 @@ void WebApplication::configure() } m_isReverseProxySupportEnabled = pref->isWebUIReverseProxySupportEnabled(); + const QString newBasePath = m_isReverseProxySupportEnabled ? pref->getWebUIBasePath() : u"/"_s; + if (m_basePath != newBasePath) + { + m_cachedFiles.clear(); + m_basePath = newBasePath; + } + if (m_isReverseProxySupportEnabled) { const QStringList proxyList = pref->getWebUITrustedReverseProxiesList().split(u';', Qt::SkipEmptyParts); @@ -576,20 +583,25 @@ void WebApplication::sendFile(const Path &path) QByteArray data = readResult.value(); const QMimeType mimeType = QMimeDatabase().mimeTypeForFileNameAndData(path.data(), data); - const bool isTranslatable = !m_isAltUIUsed && mimeType.inherits(u"text/plain"_s); - - if (isTranslatable) + const bool isTextFile = mimeType.inherits(u"text/plain"_s); + if (isTextFile) { auto dataStr = QString::fromUtf8(data); - // Translate the file - translateDocument(dataStr); + dataStr.replace(u"${BASE_PATH}"_s, m_basePath); + + const bool isTranslatable = !m_isAltUIUsed; + if (isTranslatable) + { + // Translate the file + translateDocument(dataStr); - // Add the language options - if (path == (m_rootFolder / Path(PRIVATE_FOLDER) / Path(u"views/preferences.html"_s))) - dataStr.replace(u"${LANGUAGE_OPTIONS}"_s, createLanguagesOptionsHtml()); + // Add the language options + if (path == (m_rootFolder / Path(PRIVATE_FOLDER) / Path(u"views/preferences.html"_s))) + dataStr.replace(u"${LANGUAGE_OPTIONS}"_s, createLanguagesOptionsHtml()); + } data = dataStr.toUtf8(); - m_cachedFiles[path] = {data, mimeType.name(), lastModified}; // caching translated file + m_cachedFiles[path] = {data, mimeType.name(), lastModified}; } print(data, mimeType.name()); diff --git a/src/webui/webapplication.h b/src/webui/webapplication.h index a6b7c464d45b..49b69d2cbfa5 100644 --- a/src/webui/webapplication.h +++ b/src/webui/webapplication.h @@ -225,6 +225,7 @@ class WebApplication final : public ApplicationComponent }; bool m_isAltUIUsed = false; Path m_rootFolder; + QString m_basePath; struct CachedFile {