Add encrypted file download

This commit is contained in:
Nicolas Werner 2019-12-03 02:26:41 +01:00
parent 6c2ec3fe67
commit b8f6e4ce64
9 changed files with 209 additions and 171 deletions

View file

@ -1,11 +1,8 @@
#include "TimelineViewManager.h"
#include <QFileDialog>
#include <QMetaType>
#include <QMimeDatabase>
#include <QPalette>
#include <QQmlContext>
#include <QStandardPaths>
#include "ChatPage.h"
#include "ColorImageProvider.h"
@ -124,146 +121,24 @@ TimelineViewManager::setHistoryView(const QString &room_id)
}
void
TimelineViewManager::openImageOverlay(QString mxcUrl,
QString originalFilename,
QString mimeType,
qml_mtx_events::EventType eventType) const
TimelineViewManager::openImageOverlay(QString mxcUrl, QString eventId) const
{
QQuickImageResponse *imgResponse =
imgProvider->requestImageResponse(mxcUrl.remove("mxc://"), QSize());
connect(imgResponse,
&QQuickImageResponse::finished,
this,
[this, mxcUrl, originalFilename, mimeType, eventType, imgResponse]() {
if (!imgResponse->errorString().isEmpty()) {
nhlog::ui()->error("Error when retrieving image for overlay: {}",
imgResponse->errorString().toStdString());
return;
}
auto pixmap = QPixmap::fromImage(imgResponse->textureFactory()->image());
connect(imgResponse, &QQuickImageResponse::finished, this, [this, eventId, imgResponse]() {
if (!imgResponse->errorString().isEmpty()) {
nhlog::ui()->error("Error when retrieving image for overlay: {}",
imgResponse->errorString().toStdString());
return;
}
auto pixmap = QPixmap::fromImage(imgResponse->textureFactory()->image());
auto imgDialog = new dialogs::ImageOverlay(pixmap);
imgDialog->show();
connect(imgDialog,
&dialogs::ImageOverlay::saving,
this,
[this, mxcUrl, originalFilename, mimeType, eventType]() {
saveMedia(mxcUrl, originalFilename, mimeType, eventType);
});
auto imgDialog = new dialogs::ImageOverlay(pixmap);
imgDialog->show();
connect(imgDialog, &dialogs::ImageOverlay::saving, timeline_, [this, eventId]() {
timeline_->saveMedia(eventId);
});
}
void
TimelineViewManager::saveMedia(QString mxcUrl,
QString originalFilename,
QString mimeType,
qml_mtx_events::EventType eventType) const
{
QString dialogTitle;
if (eventType == qml_mtx_events::EventType::ImageMessage) {
dialogTitle = tr("Save image");
} else if (eventType == qml_mtx_events::EventType::VideoMessage) {
dialogTitle = tr("Save video");
} else if (eventType == qml_mtx_events::EventType::AudioMessage) {
dialogTitle = tr("Save audio");
} else {
dialogTitle = tr("Save file");
}
QString filterString = QMimeDatabase().mimeTypeForName(mimeType).filterString();
auto filename =
QFileDialog::getSaveFileName(container, dialogTitle, originalFilename, filterString);
if (filename.isEmpty())
return;
const auto url = mxcUrl.toStdString();
http::client()->download(
url,
[filename, url](const std::string &data,
const std::string &,
const std::string &,
mtx::http::RequestErr err) {
if (err) {
nhlog::net()->warn("failed to retrieve image {}: {} {}",
url,
err->matrix_error.error,
static_cast<int>(err->status_code));
return;
}
try {
QFile file(filename);
if (!file.open(QIODevice::WriteOnly))
return;
file.write(QByteArray(data.data(), (int)data.size()));
file.close();
} catch (const std::exception &e) {
nhlog::ui()->warn("Error while saving file to: {}", e.what());
}
});
}
void
TimelineViewManager::cacheMedia(QString mxcUrl, QString mimeType)
{
// If the message is a link to a non mxcUrl, don't download it
if (!mxcUrl.startsWith("mxc://")) {
emit mediaCached(mxcUrl, mxcUrl);
return;
}
QString suffix = QMimeDatabase().mimeTypeForName(mimeType).preferredSuffix();
const auto url = mxcUrl.toStdString();
QFileInfo filename(QString("%1/media_cache/%2.%3")
.arg(QStandardPaths::writableLocation(QStandardPaths::CacheLocation))
.arg(QString(mxcUrl).remove("mxc://"))
.arg(suffix));
if (QDir::cleanPath(filename.path()) != filename.path()) {
nhlog::net()->warn("mxcUrl '{}' is not safe, not downloading file", url);
return;
}
QDir().mkpath(filename.path());
if (filename.isReadable()) {
emit mediaCached(mxcUrl, filename.filePath());
return;
}
http::client()->download(
url,
[this, mxcUrl, filename, url](const std::string &data,
const std::string &,
const std::string &,
mtx::http::RequestErr err) {
if (err) {
nhlog::net()->warn("failed to retrieve image {}: {} {}",
url,
err->matrix_error.error,
static_cast<int>(err->status_code));
return;
}
try {
QFile file(filename.filePath());
if (!file.open(QIODevice::WriteOnly))
return;
file.write(QByteArray(data.data(), data.size()));
file.close();
} catch (const std::exception &e) {
nhlog::ui()->warn("Error while saving file to: {}", e.what());
}
emit mediaCached(mxcUrl, filename.filePath());
});
});
}
void
@ -401,3 +276,4 @@ TimelineViewManager::queueVideoMessage(const QString &roomid,
video.url = url.toStdString();
models.value(roomid)->sendMessage(video);
}