Use postFile from libqmatrixclient.

Remove crappy json code.
This commit is contained in:
Black Hat 2019-01-18 09:09:11 +08:00
parent 635dbbff39
commit 1c76f2653b
2 changed files with 30 additions and 64 deletions

View File

@ -26,6 +26,10 @@ SpectralRoom::SpectralRoom(Connection* connection, QString roomId,
connect(this, &SpectralRoom::highlightCountChanged, this, connect(this, &SpectralRoom::highlightCountChanged, this,
&SpectralRoom::countChanged); &SpectralRoom::countChanged);
connect(this, &Room::addedMessages, this, [=] { setBusy(false); }); connect(this, &Room::addedMessages, this, [=] { setBusy(false); });
connect(this, &Room::fileTransferCompleted, this, [=] {
setFileUploadingProgress(0);
setHasFileUploading(false);
});
} }
inline QString getMIME(const QUrl& fileUrl) { inline QString getMIME(const QUrl& fileUrl) {
@ -37,71 +41,35 @@ inline QSize getImageSize(const QUrl& imageUrl) {
return reader.size(); return reader.size();
} }
inline int getFileSize(const QUrl& url) {
QFileInfo info(url.toLocalFile());
return int(info.size());
}
void SpectralRoom::chooseAndUploadFile() { void SpectralRoom::chooseAndUploadFile() {
auto localFile = QFileDialog::getOpenFileUrl(Q_NULLPTR, tr("Save File as")); auto localFile = QFileDialog::getOpenFileUrl(Q_NULLPTR, tr("Save File as"));
if (!localFile.isEmpty()) { if (!localFile.isEmpty()) {
UploadContentJob* job = QString txnID = postFile(localFile.fileName(), localFile, false);
connection()->uploadFile(localFile.toLocalFile(), getMIME(localFile)); setHasFileUploading(true);
if (isJobRunning(job)) { connect(this, &Room::fileTransferCompleted,
setHasFileUploading(true); [=](QString id, QUrl localFile, QUrl mxcUrl) {
connect(job, &BaseJob::uploadProgress, this, if (id == txnID) {
[=](qint64 bytesSent, qint64 bytesTotal) { setFileUploadingProgress(0);
if (bytesTotal != 0) { setHasFileUploading(false);
setFileUploadingProgress(bytesSent * 100 / bytesTotal); }
} });
}); connect(this, &Room::fileTransferFailed, [=](QString id, QString error) {
connect(job, &BaseJob::success, this, if (id == txnID) {
[=] { postFile(localFile, job->contentUri()); });
connect(job, &BaseJob::finished, this, [=] {
setHasFileUploading(false);
setFileUploadingProgress(0); setFileUploadingProgress(0);
}); setHasFileUploading(false);
} else { }
qDebug() << "Failed transfer."; });
} connect(
this, &Room::fileTransferProgress,
[=](QString id, qint64 progress, qint64 total) {
if (id == txnID) {
qDebug() << "Progress:" << progress << total;
setFileUploadingProgress(int(float(progress) / float(total) * 100));
}
});
} }
} }
void SpectralRoom::postFile(const QUrl& localFile, const QUrl& mxcUrl) {
const QString mime = getMIME(localFile);
const QString fileName = localFile.fileName();
QString msgType = "m.file";
int fileSize = getFileSize(localFile);
QJsonObject json;
if (mime.startsWith("image")) {
msgType = "m.image";
QSize imageSize = getImageSize(localFile);
json = {{"msgtype", msgType},
{"body", fileName},
{"filename", fileName},
{"url", mxcUrl.url()},
{"info", QJsonObject{{"h", imageSize.height()},
{"w", imageSize.width()},
{"size", fileSize},
{"mimetype", mime},
{"thumbnail_url", mxcUrl.url()},
{"thumbnail_info",
QJsonObject{{"h", imageSize.height()},
{"w", imageSize.width()},
{"size", fileSize},
{"mimetype", mime}}}}}};
} else {
if (mime.startsWith("video")) msgType = "m.video";
if (mime.startsWith("audio")) msgType = "m.audio";
json = {{"msgtype", msgType},
{"body", fileName},
{"filename", fileName},
{"url", mxcUrl.url()},
{"info", QJsonObject{{"size", fileSize}, {"mimetype", mime}}}};
}
postJson("m.room.message", json);
}
void SpectralRoom::saveFileAs(QString eventId) { void SpectralRoom::saveFileAs(QString eventId) {
auto fileName = QFileDialog::getSaveFileName(Q_NULLPTR, tr("Save File as"), auto fileName = QFileDialog::getSaveFileName(Q_NULLPTR, tr("Save File as"),
fileNameToDownload(eventId)); fileNameToDownload(eventId));
@ -237,8 +205,8 @@ QVariantList SpectralRoom::getUsers(const QString& prefix) {
} }
QString SpectralRoom::postMarkdownText(const QString& markdown) { QString SpectralRoom::postMarkdownText(const QString& markdown) {
QByteArray local = markdown.toLocal8Bit(); QByteArray local = markdown.toLocal8Bit();
const char* data = local.data(); const char* data = local.data();
QString html = cmark_markdown_to_html(data, local.length(), 0); QString html = cmark_markdown_to_html(data, local.length(), 0);
return postHtmlText(markdown, html); return postHtmlText(markdown, html);
} }

View File

@ -85,8 +85,6 @@ class SpectralRoom : public Room {
bool m_busy = false; bool m_busy = false;
void postFile(const QUrl& localFile, const QUrl& mxcUrl);
void checkForHighlights(const QMatrixClient::TimelineItem& ti); void checkForHighlights(const QMatrixClient::TimelineItem& ti);
void onAddNewTimelineEvents(timeline_iter_t from) override; void onAddNewTimelineEvents(timeline_iter_t from) override;