-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow WebAPI to specify filename and mime type for result data
PR #20377.
- Loading branch information
Showing
4 changed files
with
43 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
/* | ||
* Bittorrent Client using Qt and libtorrent. | ||
* Copyright (C) 2018, 2022 Vladimir Golovnev <[email protected]> | ||
* Copyright (C) 2018-2024 Vladimir Golovnev <[email protected]> | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU General Public License | ||
|
@@ -37,12 +37,19 @@ | |
|
||
#include "apierror.h" | ||
|
||
void APIResult::clear() | ||
{ | ||
data.clear(); | ||
mimeType.clear(); | ||
filename.clear(); | ||
} | ||
|
||
APIController::APIController(IApplication *app, QObject *parent) | ||
: ApplicationComponent(app, parent) | ||
{ | ||
} | ||
|
||
QVariant APIController::run(const QString &action, const StringMap ¶ms, const DataMap &data) | ||
APIResult APIController::run(const QString &action, const StringMap ¶ms, const DataMap &data) | ||
{ | ||
m_result.clear(); // clear result | ||
m_params = params; | ||
|
@@ -79,20 +86,22 @@ void APIController::requireParams(const QVector<QString> &requiredParams) const | |
|
||
void APIController::setResult(const QString &result) | ||
{ | ||
m_result = result; | ||
m_result.data = result; | ||
} | ||
|
||
void APIController::setResult(const QJsonArray &result) | ||
{ | ||
m_result = QJsonDocument(result); | ||
m_result.data = QJsonDocument(result); | ||
} | ||
|
||
void APIController::setResult(const QJsonObject &result) | ||
{ | ||
m_result = QJsonDocument(result); | ||
m_result.data = QJsonDocument(result); | ||
} | ||
|
||
void APIController::setResult(const QByteArray &result) | ||
void APIController::setResult(const QByteArray &result, const QString &mimeType, const QString &filename) | ||
{ | ||
m_result = result; | ||
m_result.data = result; | ||
m_result.mimeType = mimeType; | ||
m_result.filename = filename; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
/* | ||
* Bittorrent Client using Qt and libtorrent. | ||
* Copyright (C) 2018, 2022 Vladimir Golovnev <[email protected]> | ||
* Copyright (C) 2018-2024 Vladimir Golovnev <[email protected]> | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU General Public License | ||
|
@@ -30,15 +30,23 @@ | |
|
||
#include <QtContainerFwd> | ||
#include <QObject> | ||
#include <QString> | ||
#include <QVariant> | ||
|
||
#include "base/applicationcomponent.h" | ||
|
||
class QString; | ||
|
||
using DataMap = QHash<QString, QByteArray>; | ||
using StringMap = QHash<QString, QString>; | ||
|
||
struct APIResult | ||
{ | ||
QVariant data; | ||
QString mimeType; | ||
QString filename; | ||
|
||
void clear(); | ||
}; | ||
|
||
class APIController : public ApplicationComponent<QObject> | ||
{ | ||
Q_OBJECT | ||
|
@@ -47,7 +55,7 @@ class APIController : public ApplicationComponent<QObject> | |
public: | ||
explicit APIController(IApplication *app, QObject *parent = nullptr); | ||
|
||
QVariant run(const QString &action, const StringMap ¶ms, const DataMap &data = {}); | ||
APIResult run(const QString &action, const StringMap ¶ms, const DataMap &data = {}); | ||
|
||
protected: | ||
const StringMap ¶ms() const; | ||
|
@@ -57,10 +65,10 @@ class APIController : public ApplicationComponent<QObject> | |
void setResult(const QString &result); | ||
void setResult(const QJsonArray &result); | ||
void setResult(const QJsonObject &result); | ||
void setResult(const QByteArray &result); | ||
void setResult(const QByteArray &result, const QString &mimeType = {}, const QString &filename = {}); | ||
|
||
private: | ||
StringMap m_params; | ||
DataMap m_data; | ||
QVariant m_result; | ||
APIResult m_result; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters