Commit 46a3714b authored by John Tourtellott's avatar John Tourtellott
Browse files

Change list-folders request from ls "command" endpoint to "file"

Using the file endpoint should be faster
parent 76f7b88b
......@@ -21,15 +21,16 @@
#include <QDebug>
#include <QDialog>
#include <QGuiApplication>
#include <QListIterator>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>
#include <QMenu>
#include <QMessageBox>
#include <QNetworkReply>
#include <QPlainTextEdit>
#include <QPushButton>
#include <QStandardItem>
#include <QStringList>
#include <QTextDocument>
#include <QVBoxLayout>
#include <Qt>
#include <QtGlobal>
......@@ -64,35 +65,9 @@ public:
FileSystemItem::Type m_type;
QString m_date;
FileSystemItem(const QString& text)
FileSystemItem()
: m_type(FileSystemItem::UNKNOWN)
{
// Parse text from ls command
#if (QT_VERSION < QT_VERSION_CHECK(5, 14, 0))
QStringList split = text.split(' ', QString::SkipEmptyParts);
#else
QStringList split = text.split(' ', Qt::SkipEmptyParts);
#endif
QChar typeChar = split[0][0];
if (typeChar == 'l')
{
m_type = FileSystemItem::LINK;
m_name = QString("%1 -> %2").arg(split[4], split[6]);
m_size = split[2];
m_date = split[3];
}
else if ((typeChar == '-') || (typeChar == 'd'))
{
m_type = typeChar == '-' ? FileSystemItem::FILE : FileSystemItem::FOLDER;
m_name = split[4];
m_size = split[2];
m_date = split[3];
}
else
{
qWarning() << "Unrecognized file type" << text;
}
}
{}
bool isHidden() const { return m_name[0] == '.'; }
......@@ -211,26 +186,39 @@ void qtNewtFileBrowserWidget::onListFolderReply()
this->handleNetworkError(reply);
}
QString text;
if (!this->getCommandReply(reply, text))
{
return;
}
QByteArray bytes = reply->readAll();
QJsonDocument jsonDocument = QJsonDocument::fromJson(bytes.constData());
const QJsonArray& jsonArray = jsonDocument.array();
// Parse list folder reply to create list of FileSystemItem instances
QList<FileSystemItem> fsList;
QStringList lines = text.split("\n");
QListIterator<QString> lineIter(lines);
lineIter.next(); // skip first line ("Total size")
while (lineIter.hasNext())
for (const QJsonValue& jsonValue : jsonArray)
{
QString line = lineIter.next();
// qDebug() << line;
FileSystemItem fsItem(line);
FileSystemItem fsItem;
QJsonObject jsonObject = jsonValue.toObject();
fsItem.m_name = jsonObject.value("name").toString();
fsItem.m_size = jsonObject.value("size").toString();
fsItem.m_date = jsonObject.value("date").toString();
QString perms = jsonObject.value("perms").toString();
if (perms[0] == 'd')
{
fsItem.m_type = FileSystemItem::FOLDER;
}
else if (perms[0] == 'l')
{
fsItem.m_type = FileSystemItem::LINK;
}
else
{
fsItem.m_type = FileSystemItem::FILE;
}
if (!fsItem.isHidden())
{
fsList.push_back(fsItem);
}
}
std::sort(fsList.begin(), fsList.end(), FileSystemItem::lessThan);
......@@ -449,15 +437,7 @@ void qtNewtFileBrowserWidget::gotoPath(const QString& path)
emit this->beginGotoPath(path);
m_requestPath = path;
this->setCursor(Qt::BusyCursor);
// Make sure path ends with forward slash
QString sep = path.endsWith('/') ? "" : "/";
// Use ls command to get files
QString command = QString("ls -Gghpt --time-style=\"+%Y-%m-%d\" %1%2").arg(path, sep);
// Substitutes "%2B" for "+" to avoid encoding problems
command.replace('+', "%2B");
QNetworkReply* reply = m_newt->sendCommand(command, MACHINE);
QNetworkReply* reply = m_newt->requestDirectoryList(MACHINE, path);
QObject::connect(
reply, &QNetworkReply::finished, this, &qtNewtFileBrowserWidget::onListFolderReply);
}
......
......@@ -128,6 +128,12 @@ void qtNewtInterface::getCommandReply(QNetworkReply* reply, QString& result, QSt
result = jsonObj.value("output").toString();
}
QNetworkReply* qtNewtInterface::requestDirectoryList(const QString& machine, const QString& path)
{
QString url = QString("%1/file/%2/%3").arg(NEWT_BASE_URL, machine, path);
return this->sendGetRequest(url);
}
QNetworkReply* qtNewtInterface::requestHomePath(const QString& machine)
{
QString command("echo $HOME");
......@@ -142,12 +148,8 @@ QNetworkReply* qtNewtInterface::requestScratchPath(const QString& machine)
QNetworkReply* qtNewtInterface::requestTextFile(const QString& machine, const QString& path)
{
#ifndef NDEBUG
qDebug() << "Requesting text file" << machine << path;
#endif
QString url = QString("%1/file/%2/%3?view=read").arg(NEWT_BASE_URL, machine, path);
QNetworkRequest request(url);
return m_networkManager->get(request);
return this->sendGetRequest(url);
}
QNetworkReply* qtNewtInterface::sendCommand(const QString& command, const QString& machine)
......@@ -233,4 +235,12 @@ QString qtNewtInterface::getErrorMessage(QNetworkReply* reply, const QByteArray&
return errorMessage;
}
QNetworkReply* qtNewtInterface::sendGetRequest(const QString& url)
{
QNetworkRequest request(url);
QNetworkReply* reply = m_networkManager->get(request);
QObject::connect(reply, &QNetworkReply::sslErrors, this, &qtNewtInterface::onSslErrors);
return reply;
}
} // namespace newt
......@@ -54,6 +54,8 @@ public:
QString newtSessionId() const { return m_newtSessionId; }
QNetworkReply* requestDirectoryList(const QString& machine, const QString& path);
// Start request to get user's $HOME path
QNetworkReply* requestHomePath(const QString& machine);
......@@ -81,6 +83,7 @@ protected:
qtNewtInterface(QObject* parent = nullptr);
QString getErrorMessage(QNetworkReply* reply, const QByteArray& bytes) const;
QNetworkReply* sendGetRequest(const QString& url);
bool m_isLoggedIn;
QNetworkAccessManager* m_networkManager;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment