mirror of
https://github.com/Nheko-Reborn/nheko.git
synced 2024-11-25 20:48:52 +03:00
parent
cfaecaab17
commit
6f07ab433e
6 changed files with 67 additions and 46 deletions
|
@ -93,7 +93,8 @@ signals:
|
|||
const QString &filename,
|
||||
const QString &url,
|
||||
const QString &mime,
|
||||
qint64 dsize);
|
||||
qint64 dsize,
|
||||
const QSize &dimensions);
|
||||
void fileUploaded(const QString &roomid,
|
||||
const QString &filename,
|
||||
const QString &url,
|
||||
|
|
|
@ -69,6 +69,7 @@ struct PendingMessage
|
|||
uint64_t media_size;
|
||||
QString event_id;
|
||||
TimelineItem *widget;
|
||||
QSize dimensions;
|
||||
bool is_encrypted = false;
|
||||
};
|
||||
|
||||
|
@ -125,7 +126,8 @@ public:
|
|||
void addUserMessage(const QString &url,
|
||||
const QString &filename,
|
||||
const QString &mime,
|
||||
uint64_t size);
|
||||
uint64_t size,
|
||||
const QSize &dimensions = QSize());
|
||||
void updatePendingMessage(const std::string &txn_id, const QString &event_id);
|
||||
void scrollDown();
|
||||
|
||||
|
@ -321,7 +323,8 @@ void
|
|||
TimelineView::addUserMessage(const QString &url,
|
||||
const QString &filename,
|
||||
const QString &mime,
|
||||
uint64_t size)
|
||||
uint64_t size,
|
||||
const QSize &dimensions)
|
||||
{
|
||||
auto with_sender = (lastSender_ != local_user_) || isDateDifference(lastMsgTimestamp_);
|
||||
auto trimmed = QFileInfo{filename}.fileName(); // Trim file path.
|
||||
|
@ -346,6 +349,7 @@ TimelineView::addUserMessage(const QString &url,
|
|||
message.mime = mime;
|
||||
message.media_size = size;
|
||||
message.widget = view_item;
|
||||
message.dimensions = dimensions;
|
||||
|
||||
handleNewUserMessage(message);
|
||||
}
|
||||
|
|
|
@ -67,7 +67,8 @@ public slots:
|
|||
const QString &filename,
|
||||
const QString &url,
|
||||
const QString &mime,
|
||||
uint64_t dsize);
|
||||
uint64_t dsize,
|
||||
const QSize &dimensions);
|
||||
void queueFileMessage(const QString &roomid,
|
||||
const QString &filename,
|
||||
const QString &url,
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
*/
|
||||
|
||||
#include <QApplication>
|
||||
#include <QImageReader>
|
||||
#include <QSettings>
|
||||
#include <QtConcurrent>
|
||||
|
||||
|
@ -283,48 +284,52 @@ ChatPage::ChatPage(QSharedPointer<UserSettings> userSettings, QWidget *parent)
|
|||
|
||||
connect(text_input_, &TextInputWidget::sendJoinRoomRequest, this, &ChatPage::joinRoom);
|
||||
|
||||
connect(text_input_,
|
||||
&TextInputWidget::uploadImage,
|
||||
this,
|
||||
[this](QSharedPointer<QIODevice> dev, const QString &fn) {
|
||||
QMimeDatabase db;
|
||||
QMimeType mime = db.mimeTypeForData(dev.data());
|
||||
connect(
|
||||
text_input_,
|
||||
&TextInputWidget::uploadImage,
|
||||
this,
|
||||
[this](QSharedPointer<QIODevice> dev, const QString &fn) {
|
||||
QMimeDatabase db;
|
||||
QMimeType mime = db.mimeTypeForData(dev.data());
|
||||
|
||||
if (!dev->open(QIODevice::ReadOnly)) {
|
||||
emit uploadFailed(
|
||||
QString("Error while reading media: %1").arg(dev->errorString()));
|
||||
return;
|
||||
}
|
||||
if (!dev->open(QIODevice::ReadOnly)) {
|
||||
emit uploadFailed(
|
||||
QString("Error while reading media: %1").arg(dev->errorString()));
|
||||
return;
|
||||
}
|
||||
|
||||
auto bin = dev->readAll();
|
||||
auto payload = std::string(bin.data(), bin.size());
|
||||
auto bin = dev->peek(dev->size());
|
||||
auto payload = std::string(bin.data(), bin.size());
|
||||
auto dimensions = QImageReader(dev.get()).size();
|
||||
|
||||
http::v2::client()->upload(
|
||||
payload,
|
||||
mime.name().toStdString(),
|
||||
QFileInfo(fn).fileName().toStdString(),
|
||||
[this,
|
||||
room_id = current_room_,
|
||||
filename = fn,
|
||||
mime = mime.name(),
|
||||
size = payload.size()](const mtx::responses::ContentURI &res,
|
||||
mtx::http::RequestErr err) {
|
||||
if (err) {
|
||||
emit uploadFailed(
|
||||
tr("Failed to upload image. Please try again."));
|
||||
nhlog::net()->warn("failed to upload image: {} ({})",
|
||||
err->matrix_error.error,
|
||||
static_cast<int>(err->status_code));
|
||||
return;
|
||||
}
|
||||
http::v2::client()->upload(
|
||||
payload,
|
||||
mime.name().toStdString(),
|
||||
QFileInfo(fn).fileName().toStdString(),
|
||||
[this,
|
||||
room_id = current_room_,
|
||||
filename = fn,
|
||||
mime = mime.name(),
|
||||
size = payload.size(),
|
||||
dimensions](const mtx::responses::ContentURI &res, mtx::http::RequestErr err) {
|
||||
if (err) {
|
||||
emit uploadFailed(
|
||||
tr("Failed to upload image. Please try again."));
|
||||
nhlog::net()->warn("failed to upload image: {} {} ({})",
|
||||
err->matrix_error.error,
|
||||
to_string(err->matrix_error.errcode),
|
||||
static_cast<int>(err->status_code));
|
||||
return;
|
||||
}
|
||||
|
||||
emit imageUploaded(room_id,
|
||||
filename,
|
||||
QString::fromStdString(res.content_uri),
|
||||
mime,
|
||||
size);
|
||||
});
|
||||
});
|
||||
emit imageUploaded(room_id,
|
||||
filename,
|
||||
QString::fromStdString(res.content_uri),
|
||||
mime,
|
||||
size,
|
||||
dimensions);
|
||||
});
|
||||
});
|
||||
|
||||
connect(text_input_,
|
||||
&TextInputWidget::uploadFile,
|
||||
|
@ -461,9 +466,15 @@ ChatPage::ChatPage(QSharedPointer<UserSettings> userSettings, QWidget *parent)
|
|||
connect(this,
|
||||
&ChatPage::imageUploaded,
|
||||
this,
|
||||
[this](QString roomid, QString filename, QString url, QString mime, qint64 dsize) {
|
||||
[this](QString roomid,
|
||||
QString filename,
|
||||
QString url,
|
||||
QString mime,
|
||||
qint64 dsize,
|
||||
QSize dimensions) {
|
||||
text_input_->hideUploadSpinner();
|
||||
view_manager_->queueImageMessage(roomid, filename, url, mime, dsize);
|
||||
view_manager_->queueImageMessage(
|
||||
roomid, filename, url, mime, dsize, dimensions);
|
||||
});
|
||||
connect(this,
|
||||
&ChatPage::fileUploaded,
|
||||
|
|
|
@ -1097,6 +1097,8 @@ toRoomMessage<mtx::events::msg::Image>(const PendingMessage &m)
|
|||
image.info.size = m.media_size;
|
||||
image.body = m.filename.toStdString();
|
||||
image.url = m.body.toStdString();
|
||||
image.info.h = m.dimensions.height();
|
||||
image.info.w = m.dimensions.width();
|
||||
return image;
|
||||
}
|
||||
|
||||
|
|
|
@ -74,7 +74,8 @@ TimelineViewManager::queueImageMessage(const QString &roomid,
|
|||
const QString &filename,
|
||||
const QString &url,
|
||||
const QString &mime,
|
||||
uint64_t size)
|
||||
uint64_t size,
|
||||
const QSize &dimensions)
|
||||
{
|
||||
if (!timelineViewExists(roomid)) {
|
||||
nhlog::ui()->warn("Cannot send m.image message to a non-managed view");
|
||||
|
@ -83,7 +84,8 @@ TimelineViewManager::queueImageMessage(const QString &roomid,
|
|||
|
||||
auto view = views_[roomid];
|
||||
|
||||
view->addUserMessage<ImageItem, mtx::events::MessageType::Image>(url, filename, mime, size);
|
||||
view->addUserMessage<ImageItem, mtx::events::MessageType::Image>(
|
||||
url, filename, mime, size, dimensions);
|
||||
}
|
||||
|
||||
void
|
||||
|
|
Loading…
Reference in a new issue