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.
This commit is contained in:
Thomas Piccirello 2024-10-02 10:57:45 -07:00
commit 07b514b05a
No known key found for this signature in database
4 changed files with 37 additions and 9 deletions

View file

@ -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);

View file

@ -237,6 +237,8 @@ public:
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;

View file

@ -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);
// Add the language options
if (path == (m_rootFolder / Path(PRIVATE_FOLDER) / Path(u"views/preferences.html"_s)))
dataStr.replace(u"${LANGUAGE_OPTIONS}"_s, createLanguagesOptionsHtml());
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());
}
data = dataStr.toUtf8();
m_cachedFiles[path] = {data, mimeType.name(), lastModified}; // caching translated file
m_cachedFiles[path] = {data, mimeType.name(), lastModified};
}
print(data, mimeType.name());

View file

@ -225,6 +225,7 @@ private:
};
bool m_isAltUIUsed = false;
Path m_rootFolder;
QString m_basePath;
struct CachedFile
{