Skip to content

Commit

Permalink
Add API for listing directory content
Browse files Browse the repository at this point in the history
  • Loading branch information
pktiuk committed Apr 23, 2024
1 parent c06817f commit 469f411
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
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

0 comments on commit 469f411

Please sign in to comment.