Commit f922fd8e authored by kaniini's avatar kaniini

account: add Account::upload()

parent 7ebbf5c9
......@@ -173,6 +173,7 @@ void Account::post(QUrl url, QHttpMultiPart *message, bool authenticated, std::f
}
QNetworkReply *reply = m_qnam->post (request, message);
message->setParent (reply);
if (reply_cb != nullptr)
{
......@@ -185,6 +186,36 @@ void Account::post(QUrl url, QHttpMultiPart *message, bool authenticated, std::f
}
}
// assumes file is already opened and named
void Account::upload(Post *p, QFile *file)
{
QHttpMultiPart *mp = new QHttpMultiPart;
QHttpPart filePart;
filePart.setHeader (QNetworkRequest::ContentDispositionHeader, "form-data; name=\"file\"");
filePart.setBodyDevice (file);
file->setParent (mp);
mp->append (filePart);
auto upload_url = apiUrl("/api/v1/media");
post(upload_url, mp, true, [&] (QNetworkReply *reply) {
auto data = reply->readAll ();
auto doc = QJsonDocument::fromJson (data);
if (! doc.isObject ())
return;
auto obj = doc.object ();
auto att = std::make_shared<Attachment> (p, obj);
if (att->m_url.isEmpty ())
return;
emit attachmentUploaded (p, att);
});
}
const std::shared_ptr<Identity> Account::identityLookup(QString &acct, QJsonObject doc)
{
auto id = m_identity_cache[acct];
......
......@@ -12,6 +12,7 @@
#include <QMap>
#include <QHttpPart>
#include <QHttpMultiPart>
#include <QFile>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkReply>
......@@ -64,7 +65,7 @@ public:
QUrl getAuthorizeUrl();
QUrl getTokenUrl();
void setToken(const QString &authcode);
bool haveToken() { return m_token != ""; }
bool haveToken() { return ! m_token.isEmpty (); }
void validateToken();
// name
......@@ -102,6 +103,9 @@ public:
void repeat(std::shared_ptr<Post> p);
void unrepeat(std::shared_ptr<Post> p);
// uploading media
void upload(Post *p, QFile *file);
// post refresh
void invalidatePost(Post *p);
......@@ -126,6 +130,7 @@ signals:
void invalidated();
void fetchedInstanceMetadata();
void invalidatedPost(Post *p);
void attachmentUploaded(Post *p, std::shared_ptr<Attachment> att);
private:
QString m_name;
......
Markdown is supported
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