Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API for obtaining list of directories #20314

Merged
merged 1 commit into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/webui/api/appcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

#include <QCoreApplication>
#include <QDebug>
#include <QDir>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
Expand All @@ -62,6 +63,7 @@
#include "base/utils/password.h"
#include "base/utils/string.h"
#include "base/version.h"
#include "apierror.h"
#include "../webapplication.h"

using namespace std::chrono_literals;
Expand Down Expand Up @@ -1112,6 +1114,38 @@ void AppController::sendTestEmailAction()
app()->sendTestEmail();
}


void AppController::getDirectoryContentAction()
{
requireParams({u"dirPath"_s});

const QString dirPath = params().value(u"dirPath"_s);
if (dirPath.isEmpty() || dirPath.startsWith(u':'))
throw APIError(APIErrorType::BadParams, tr("Invalid directory path"));

const QDir dir {dirPath};
if (!dir.isAbsolute())
throw APIError(APIErrorType::BadParams, tr("Invalid directory path"));
if (!dir.exists())
throw APIError(APIErrorType::NotFound, tr("Directory does not exist"));

const QString visibility = params().value(u"mode"_s, u"all"_s);

const auto parseDirectoryContentMode = [](const QString &visibility) -> QDir::Filters
{
if (visibility == u"dirs")
return QDir::Dirs;
if (visibility == u"files")
return QDir::Files;
if (visibility == u"all")
return (QDir::Dirs | QDir::Files);
throw APIError(APIErrorType::BadParams, tr("Invalid mode, allowed values: %1").arg(u"all, dirs, files"_s));
};

const QStringList dirs = dir.entryList(QDir::NoDotAndDotDot | parseDirectoryContentMode(visibility));
setResult(QJsonArray::fromStringList(dirs));
}

void AppController::networkInterfaceListAction()
{
QJsonArray ifaceList;
Expand Down
1 change: 1 addition & 0 deletions src/webui/api/appcontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ private slots:
void setPreferencesAction();
void defaultSavePathAction();
void sendTestEmailAction();
void getDirectoryContentAction();

void networkInterfaceListAction();
void networkInterfaceAddressListAction();
Expand Down