Skip to content

Commit

Permalink
Add concept of configurable WebUI URL base path
Browse files Browse the repository at this point in the history
This provides the underlying support for having a configurable base path. Future commit(s) will expose this functionality to users.
  • Loading branch information
Piccirello committed Oct 4, 2024
1 parent ba82d61 commit 583ecdd
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
13 changes: 13 additions & 0 deletions src/base/preferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,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);
Expand Down
2 changes: 2 additions & 0 deletions src/base/preferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,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;
Expand Down
30 changes: 21 additions & 9 deletions src/webui/webapplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,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);
Expand Down Expand Up @@ -574,20 +581,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());
Expand Down
1 change: 1 addition & 0 deletions src/webui/webapplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ class WebApplication final : public ApplicationComponent<QObject>
};
bool m_isAltUIUsed = false;
Path m_rootFolder;
QString m_basePath;

struct CachedFile
{
Expand Down

0 comments on commit 583ecdd

Please sign in to comment.