Commit 2b1a5092 authored by kaniini's avatar kaniini

very initial upload support

parent e8093dd1
......@@ -162,8 +162,6 @@ void Account::post(QUrl url, QHttpMultiPart *message, bool authenticated, std::f
{
QNetworkRequest request = QNetworkRequest (url);
message->setContentType (QHttpMultiPart::FormDataType);
qDebug() << "POST" << url << "(multipart-message)";
if (authenticated && haveToken ())
......@@ -187,19 +185,20 @@ 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)
void Account::upload(std::shared_ptr<Post> p, QFile *file, QString filename)
{
QHttpMultiPart *mp = new QHttpMultiPart;
QHttpMultiPart *mp = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QHttpPart filePart;
filePart.setHeader (QNetworkRequest::ContentDispositionHeader, "form-data; name=\"file\"");
filePart.setHeader (QNetworkRequest::ContentTypeHeader, "application/octet-stream");
filePart.setHeader (QNetworkRequest::ContentDispositionHeader, QString("attachment; name=\"file\"; filename=\"%1\"").arg(filename));
filePart.setBodyDevice (file);
file->setParent (mp);
mp->append (filePart);
auto upload_url = apiUrl("/api/v1/media");
post(upload_url, mp, true, [&] (QNetworkReply *reply) {
post(upload_url, mp, true, [=] (QNetworkReply *reply) {
auto data = reply->readAll ();
auto doc = QJsonDocument::fromJson (data);
......@@ -208,7 +207,7 @@ void Account::upload(Post *p, QFile *file)
auto obj = doc.object ();
auto att = std::make_shared<Attachment> (p, obj);
auto att = std::make_shared<Attachment> (p.get (), obj);
if (att->m_url.isEmpty ())
return;
......
......@@ -104,7 +104,7 @@ public:
void unrepeat(std::shared_ptr<Post> p);
// uploading media
void upload(Post *p, QFile *file);
void upload(std::shared_ptr<Post> p, QFile *file, QString filename);
// post refresh
void invalidatePost(Post *p);
......@@ -130,7 +130,7 @@ signals:
void invalidated();
void fetchedInstanceMetadata();
void invalidatedPost(Post *p);
void attachmentUploaded(Post *p, std::shared_ptr<Attachment> att);
void attachmentUploaded(std::shared_ptr<Post> p, std::shared_ptr<Attachment> att);
private:
QString m_name;
......
#include "composeform.h"
#include <QTextCursor>
#include <QFileDialog>
ComposeForm::ComposeForm(Michabo::AccountManager *manager, QWidget *parent)
: QWidget(parent),
......@@ -13,6 +14,7 @@ ComposeForm::ComposeForm(Michabo::AccountManager *manager, QWidget *parent)
m_bottom_box(new QHBoxLayout()),
m_layout(new QVBoxLayout(this)),
m_content_type(new QComboBox(this)),
m_attach_button(new QPushButton(this)),
m_reply_to(nullptr),
m_scratch(std::make_shared<Michabo::Post>(m_account))
{
......@@ -64,12 +66,20 @@ ComposeForm::ComposeForm(Michabo::AccountManager *manager, QWidget *parent)
QObject::connect(m_content, &QTextEdit::textChanged, this, &ComposeForm::updateScratch);
QObject::connect(m_content_type, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &ComposeForm::updateContentType);
m_attach_button->setIcon (QIcon::fromTheme ("mail-attachment"));
m_attach_button->setText (tr("Attach File"));
QObject::connect(m_attach_button, &QPushButton::clicked, this, &ComposeForm::uploadAttachment);
m_bottom_box->addWidget (m_attach_button);
m_bottom_box->addWidget(m_post_length_counter, 1, Qt::AlignRight);
if (m_content_type->count() > 1)
m_bottom_box->addWidget(m_content_type);
m_bottom_box->addWidget(m_post_length_counter, 1, Qt::AlignRight);
m_bottom_box->addWidget(m_button_box);
m_layout->addLayout(m_bottom_box);
m_layout->addWidget(m_button_box);
}
void ComposeForm::updateScratch()
......@@ -122,3 +132,13 @@ void ComposeForm::setReplyTo(std::shared_ptr<Michabo::Post> p)
m_scratch->m_reply_target_id = m_reply_to->m_reply_target_id;
m_scratch->m_visibility = m_reply_to->m_visibility;
}
void ComposeForm::uploadAttachment()
{
QString filename = QFileDialog::getOpenFileName (this, tr("Attach File"));
QFile *file = new QFile (filename);
QFileInfo info (filename);
file->open(QFile::ReadOnly);
m_account->upload (m_scratch, file, info.fileName ());
}
......@@ -28,6 +28,8 @@ signals:
public slots:
void updateScratch();
void updateContentType();
void uploadAttachment();
private:
Michabo::Account *m_account;
QLineEdit *m_subject;
......@@ -40,6 +42,7 @@ private:
QVBoxLayout *m_layout;
QString m_visibility;
QComboBox *m_content_type;
QPushButton *m_attach_button;
std::shared_ptr<Michabo::Post> m_reply_to;
std::shared_ptr<Michabo::Post> m_scratch;
};
......
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