2020-02-05 10:29:27 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
|
|
|
#include "storage/storage_cloud_blob.h"
|
|
|
|
|
|
|
|
#include "base/zlib_help.h"
|
2020-02-05 14:28:22 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2020-09-30 11:32:02 +00:00
|
|
|
#include "ui/text/format_values.h"
|
2020-02-05 14:13:12 +00:00
|
|
|
#include "main/main_account.h"
|
2020-06-10 10:49:10 +00:00
|
|
|
#include "main/main_session.h"
|
2020-02-05 10:29:27 +00:00
|
|
|
|
2020-02-05 13:06:38 +00:00
|
|
|
namespace Storage::CloudBlob {
|
2020-02-05 10:29:27 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
QByteArray ReadFinalFile(const QString &path) {
|
|
|
|
constexpr auto kMaxZipSize = 10 * 1024 * 1024;
|
|
|
|
auto file = QFile(path);
|
|
|
|
if (file.size() > kMaxZipSize || !file.open(QIODevice::ReadOnly)) {
|
|
|
|
return QByteArray();
|
|
|
|
}
|
|
|
|
return file.readAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ExtractZipFile(zlib::FileToRead &zip, const QString path) {
|
|
|
|
constexpr auto kMaxSize = 25 * 1024 * 1024;
|
|
|
|
const auto content = zip.readCurrentFileContent(kMaxSize);
|
|
|
|
if (content.isEmpty() || zip.error() != UNZ_OK) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto file = QFile(path);
|
|
|
|
return file.open(QIODevice::WriteOnly)
|
|
|
|
&& (file.write(content) == content.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
bool UnpackBlob(
|
2020-05-07 15:06:44 +00:00
|
|
|
const QString &path,
|
|
|
|
const QString &folder,
|
|
|
|
Fn<bool(const QString &)> checkNameCallback) {
|
2020-02-05 10:29:27 +00:00
|
|
|
const auto bytes = ReadFinalFile(path);
|
|
|
|
if (bytes.isEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto zip = zlib::FileToRead(bytes);
|
|
|
|
if (zip.goToFirstFile() != UNZ_OK) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
const auto name = zip.getCurrentFileName();
|
|
|
|
const auto path = folder + '/' + name;
|
|
|
|
if (checkNameCallback(name) && !ExtractZipFile(zip, path)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto jump = zip.goToNextFile();
|
|
|
|
if (jump == UNZ_END_OF_LIST_OF_FILE) {
|
|
|
|
break;
|
|
|
|
} else if (jump != UNZ_OK) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} while (true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-02-05 14:28:22 +00:00
|
|
|
QString StateDescription(const BlobState &state, tr::phrase<> activeText) {
|
2020-08-31 08:14:53 +00:00
|
|
|
return v::match(state, [](const Available &data) {
|
2020-02-05 14:28:22 +00:00
|
|
|
return tr::lng_emoji_set_download(
|
|
|
|
tr::now,
|
|
|
|
lt_size,
|
2020-09-30 11:32:02 +00:00
|
|
|
Ui::FormatSizeText(data.size));
|
2020-02-05 14:28:22 +00:00
|
|
|
}, [](const Ready &data) -> QString {
|
|
|
|
return tr::lng_emoji_set_ready(tr::now);
|
|
|
|
}, [&](const Active &data) -> QString {
|
|
|
|
return activeText(tr::now);
|
|
|
|
}, [](const Loading &data) {
|
|
|
|
const auto percent = (data.size > 0)
|
2021-01-23 03:29:50 +00:00
|
|
|
? std::clamp((data.already * 100) / float64(data.size), 0., 100.)
|
2020-02-05 14:28:22 +00:00
|
|
|
: 0.;
|
|
|
|
return tr::lng_emoji_set_loading(
|
|
|
|
tr::now,
|
|
|
|
lt_percent,
|
|
|
|
QString::number(int(std::round(percent))) + '%',
|
|
|
|
lt_progress,
|
2020-09-30 11:32:02 +00:00
|
|
|
Ui::FormatDownloadText(data.already, data.size));
|
2020-02-05 14:28:22 +00:00
|
|
|
}, [](const Failed &data) {
|
|
|
|
return tr::lng_attach_failed(tr::now);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-05 14:13:12 +00:00
|
|
|
BlobLoader::BlobLoader(
|
|
|
|
QObject *parent,
|
2020-06-10 10:49:10 +00:00
|
|
|
not_null<Main::Session*> session,
|
2020-02-05 14:13:12 +00:00
|
|
|
int id,
|
|
|
|
MTP::DedicatedLoader::Location location,
|
|
|
|
const QString &folder,
|
|
|
|
int size)
|
|
|
|
: QObject(parent)
|
|
|
|
, _folder(folder)
|
|
|
|
, _id(id)
|
|
|
|
, _state(Loading{ 0, size })
|
2020-06-10 13:24:41 +00:00
|
|
|
, _mtproto(session.get()) {
|
2020-02-05 14:13:12 +00:00
|
|
|
const auto ready = [=](std::unique_ptr<MTP::DedicatedLoader> loader) {
|
|
|
|
if (loader) {
|
|
|
|
setImplementation(std::move(loader));
|
|
|
|
} else {
|
|
|
|
fail();
|
|
|
|
}
|
|
|
|
};
|
2020-06-10 13:24:41 +00:00
|
|
|
MTP::StartDedicatedLoader(&_mtproto, location, _folder, ready);
|
2020-02-05 14:13:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int BlobLoader::id() const {
|
|
|
|
return _id;
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<BlobState> BlobLoader::state() const {
|
|
|
|
return _state.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BlobLoader::setImplementation(
|
|
|
|
std::unique_ptr<MTP::DedicatedLoader> loader) {
|
|
|
|
_implementation = std::move(loader);
|
|
|
|
_state = _implementation->progress(
|
|
|
|
) | rpl::map([](const Loading &state) {
|
|
|
|
return BlobState(state);
|
|
|
|
});
|
|
|
|
_implementation->failed(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
fail();
|
|
|
|
}, _implementation->lifetime());
|
|
|
|
|
|
|
|
_implementation->ready(
|
|
|
|
) | rpl::start_with_next([=](const QString &filepath) {
|
|
|
|
unpack(filepath);
|
|
|
|
}, _implementation->lifetime());
|
|
|
|
|
|
|
|
QDir(_folder).removeRecursively();
|
|
|
|
_implementation->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BlobLoader::fail() {
|
|
|
|
_state = Failed();
|
|
|
|
}
|
|
|
|
|
2020-02-05 13:06:38 +00:00
|
|
|
} // namespace Storage::CloudBlob
|