2019-12-04 12:15:58 +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/file_download_mtproto.h"
|
|
|
|
|
|
|
|
#include "data/data_document.h"
|
|
|
|
#include "data/data_file_origin.h"
|
|
|
|
#include "storage/cache/storage_cache_types.h"
|
|
|
|
#include "main/main_session.h"
|
|
|
|
#include "apiwrap.h"
|
|
|
|
#include "mtproto/mtp_instance.h"
|
2020-06-17 09:36:25 +00:00
|
|
|
#include "mtproto/mtproto_config.h"
|
2019-12-04 12:15:58 +00:00
|
|
|
#include "mtproto/mtproto_auth_key.h"
|
|
|
|
|
|
|
|
mtpFileLoader::mtpFileLoader(
|
2020-06-08 15:17:33 +00:00
|
|
|
not_null<Main::Session*> session,
|
2019-12-04 12:15:58 +00:00
|
|
|
const StorageFileLocation &location,
|
|
|
|
Data::FileOrigin origin,
|
|
|
|
LocationType type,
|
|
|
|
const QString &to,
|
2022-05-10 14:22:28 +00:00
|
|
|
int64 loadSize,
|
|
|
|
int64 fullSize,
|
2019-12-04 12:15:58 +00:00
|
|
|
LoadToCacheSetting toCache,
|
|
|
|
LoadFromCloudSetting fromCloud,
|
|
|
|
bool autoLoading,
|
|
|
|
uint8 cacheTag)
|
|
|
|
: FileLoader(
|
2020-06-08 15:17:33 +00:00
|
|
|
session,
|
2019-12-04 12:15:58 +00:00
|
|
|
to,
|
2020-08-26 13:28:56 +00:00
|
|
|
loadSize,
|
|
|
|
fullSize,
|
2019-12-04 12:15:58 +00:00
|
|
|
type,
|
|
|
|
toCache,
|
|
|
|
fromCloud,
|
|
|
|
autoLoading,
|
|
|
|
cacheTag)
|
2020-06-08 15:17:33 +00:00
|
|
|
, DownloadMtprotoTask(&session->downloader(), location, origin) {
|
2019-12-04 12:15:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mtpFileLoader::mtpFileLoader(
|
2020-06-08 15:17:33 +00:00
|
|
|
not_null<Main::Session*> session,
|
2019-12-04 12:15:58 +00:00
|
|
|
const WebFileLocation &location,
|
2022-05-10 14:22:28 +00:00
|
|
|
int64 loadSize,
|
|
|
|
int64 fullSize,
|
2019-12-04 12:15:58 +00:00
|
|
|
LoadFromCloudSetting fromCloud,
|
|
|
|
bool autoLoading,
|
|
|
|
uint8 cacheTag)
|
|
|
|
: FileLoader(
|
2020-06-08 15:17:33 +00:00
|
|
|
session,
|
2019-12-04 12:15:58 +00:00
|
|
|
QString(),
|
2020-08-26 13:28:56 +00:00
|
|
|
loadSize,
|
|
|
|
fullSize,
|
2019-12-04 12:15:58 +00:00
|
|
|
UnknownFileLocation,
|
|
|
|
LoadToCacheAsWell,
|
|
|
|
fromCloud,
|
|
|
|
autoLoading,
|
|
|
|
cacheTag)
|
2019-12-05 08:32:33 +00:00
|
|
|
, DownloadMtprotoTask(
|
2020-06-08 15:17:33 +00:00
|
|
|
&session->downloader(),
|
2020-06-17 09:36:25 +00:00
|
|
|
session->serverConfig().webFileDcId,
|
2019-12-05 08:32:33 +00:00
|
|
|
{ location }) {
|
2019-12-04 12:15:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mtpFileLoader::mtpFileLoader(
|
2020-06-08 15:17:33 +00:00
|
|
|
not_null<Main::Session*> session,
|
2019-12-04 12:15:58 +00:00
|
|
|
const GeoPointLocation &location,
|
2022-05-10 14:22:28 +00:00
|
|
|
int64 loadSize,
|
|
|
|
int64 fullSize,
|
2019-12-04 12:15:58 +00:00
|
|
|
LoadFromCloudSetting fromCloud,
|
|
|
|
bool autoLoading,
|
|
|
|
uint8 cacheTag)
|
|
|
|
: FileLoader(
|
2020-06-08 15:17:33 +00:00
|
|
|
session,
|
2019-12-04 12:15:58 +00:00
|
|
|
QString(),
|
2020-08-26 13:28:56 +00:00
|
|
|
loadSize,
|
|
|
|
fullSize,
|
2019-12-04 12:15:58 +00:00
|
|
|
UnknownFileLocation,
|
|
|
|
LoadToCacheAsWell,
|
|
|
|
fromCloud,
|
|
|
|
autoLoading,
|
|
|
|
cacheTag)
|
2019-12-05 08:32:33 +00:00
|
|
|
, DownloadMtprotoTask(
|
2020-06-08 15:17:33 +00:00
|
|
|
&session->downloader(),
|
2020-06-17 09:36:25 +00:00
|
|
|
session->serverConfig().webFileDcId,
|
2019-12-05 08:32:33 +00:00
|
|
|
{ location }) {
|
2019-12-04 12:15:58 +00:00
|
|
|
}
|
|
|
|
|
2022-08-01 12:14:37 +00:00
|
|
|
mtpFileLoader::mtpFileLoader(
|
|
|
|
not_null<Main::Session*> session,
|
|
|
|
const AudioAlbumThumbLocation &location,
|
|
|
|
int64 loadSize,
|
|
|
|
int64 fullSize,
|
|
|
|
LoadFromCloudSetting fromCloud,
|
|
|
|
bool autoLoading,
|
|
|
|
uint8 cacheTag)
|
|
|
|
: FileLoader(
|
|
|
|
session,
|
|
|
|
QString(),
|
|
|
|
loadSize,
|
|
|
|
fullSize,
|
|
|
|
UnknownFileLocation,
|
|
|
|
LoadToCacheAsWell,
|
|
|
|
fromCloud,
|
|
|
|
autoLoading,
|
|
|
|
cacheTag)
|
|
|
|
, DownloadMtprotoTask(
|
|
|
|
&session->downloader(),
|
|
|
|
session->serverConfig().webFileDcId,
|
|
|
|
{ location }) {
|
|
|
|
}
|
|
|
|
|
2020-04-15 14:06:34 +00:00
|
|
|
mtpFileLoader::~mtpFileLoader() {
|
|
|
|
if (!_finished) {
|
|
|
|
cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-04 12:15:58 +00:00
|
|
|
Data::FileOrigin mtpFileLoader::fileOrigin() const {
|
2019-12-05 08:32:33 +00:00
|
|
|
return DownloadMtprotoTask::fileOrigin();
|
2019-12-04 12:15:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64 mtpFileLoader::objId() const {
|
2019-12-05 08:32:33 +00:00
|
|
|
return DownloadMtprotoTask::objectId();
|
2019-12-04 12:15:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool mtpFileLoader::readyToRequest() const {
|
|
|
|
return !_finished
|
|
|
|
&& !_lastComplete
|
2020-08-26 13:28:56 +00:00
|
|
|
&& (_fullSize != 0 || !haveSentRequests())
|
|
|
|
&& (!_fullSize || _nextRequestOffset < _loadSize);
|
2019-12-04 12:15:58 +00:00
|
|
|
}
|
|
|
|
|
2022-05-10 14:22:28 +00:00
|
|
|
int64 mtpFileLoader::takeNextRequestOffset() {
|
2019-12-04 12:15:58 +00:00
|
|
|
Expects(readyToRequest());
|
|
|
|
|
2019-12-05 08:32:33 +00:00
|
|
|
const auto result = _nextRequestOffset;
|
2019-12-04 12:15:58 +00:00
|
|
|
_nextRequestOffset += Storage::kDownloadPartSize;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-05-10 14:22:28 +00:00
|
|
|
bool mtpFileLoader::feedPart(int64 offset, const QByteArray &bytes) {
|
2020-08-26 13:28:56 +00:00
|
|
|
const auto buffer = bytes::make_span(bytes);
|
2019-12-04 12:15:58 +00:00
|
|
|
if (!writeResultPart(offset, buffer)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (buffer.empty() || (buffer.size() % 1024)) { // bad next offset
|
|
|
|
_lastComplete = true;
|
|
|
|
}
|
2019-12-05 08:32:33 +00:00
|
|
|
const auto finished = !haveSentRequests()
|
2020-08-26 13:28:56 +00:00
|
|
|
&& (_lastComplete || (_fullSize && _nextRequestOffset >= _loadSize));
|
2019-12-04 12:15:58 +00:00
|
|
|
if (finished) {
|
2019-12-05 08:32:33 +00:00
|
|
|
removeFromQueue();
|
2019-12-04 12:15:58 +00:00
|
|
|
if (!finalizeResult()) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-10 11:17:10 +00:00
|
|
|
} else {
|
2019-12-04 12:15:58 +00:00
|
|
|
notifyAboutProgress();
|
|
|
|
}
|
2019-12-05 08:32:33 +00:00
|
|
|
return true;
|
2019-12-04 12:15:58 +00:00
|
|
|
}
|
|
|
|
|
2019-12-05 08:32:33 +00:00
|
|
|
void mtpFileLoader::cancelOnFail() {
|
2023-01-05 13:32:51 +00:00
|
|
|
cancel(FailureReason::OtherFailure);
|
2019-12-04 12:15:58 +00:00
|
|
|
}
|
|
|
|
|
2022-05-10 14:22:28 +00:00
|
|
|
bool mtpFileLoader::setWebFileSizeHook(int64 size) {
|
2020-08-26 13:28:56 +00:00
|
|
|
if (!_fullSize || _fullSize == size) {
|
|
|
|
_fullSize = _loadSize = size;
|
2019-12-04 12:15:58 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-12-05 08:32:33 +00:00
|
|
|
LOG(("MTP Error: "
|
|
|
|
"Bad size provided by bot for webDocument: %1, real: %2"
|
2020-08-26 13:28:56 +00:00
|
|
|
).arg(_fullSize
|
2019-12-05 08:32:33 +00:00
|
|
|
).arg(size));
|
2023-01-05 13:32:51 +00:00
|
|
|
cancel(FailureReason::OtherFailure);
|
2019-12-05 08:32:33 +00:00
|
|
|
return false;
|
2019-12-04 12:15:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void mtpFileLoader::startLoading() {
|
2019-12-05 08:32:33 +00:00
|
|
|
addToQueue();
|
2019-12-04 12:15:58 +00:00
|
|
|
}
|
|
|
|
|
2020-08-26 13:28:56 +00:00
|
|
|
void mtpFileLoader::startLoadingWithPartial(const QByteArray &data) {
|
|
|
|
Expects(data.startsWith("partial:"));
|
|
|
|
|
|
|
|
constexpr auto kPrefix = 8;
|
|
|
|
const auto parts = (data.size() - kPrefix) / Storage::kDownloadPartSize;
|
2022-05-10 14:22:28 +00:00
|
|
|
const auto use = parts * int64(Storage::kDownloadPartSize);
|
2020-08-25 18:03:41 +00:00
|
|
|
if (use > 0) {
|
|
|
|
_nextRequestOffset = use;
|
2020-08-26 13:28:56 +00:00
|
|
|
feedPart(0, QByteArray::fromRawData(data.data() + kPrefix, use));
|
2020-08-25 18:03:41 +00:00
|
|
|
}
|
|
|
|
startLoading();
|
|
|
|
}
|
|
|
|
|
2019-12-05 08:32:33 +00:00
|
|
|
void mtpFileLoader::cancelHook() {
|
|
|
|
cancelAllRequests();
|
2019-12-04 12:15:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Storage::Cache::Key mtpFileLoader::cacheKey() const {
|
2020-08-31 08:14:53 +00:00
|
|
|
return v::match(location().data, [&](const WebFileLocation &location) {
|
2019-12-04 12:15:58 +00:00
|
|
|
return Data::WebDocumentCacheKey(location);
|
|
|
|
}, [&](const GeoPointLocation &location) {
|
|
|
|
return Data::GeoPointCacheKey(location);
|
|
|
|
}, [&](const StorageFileLocation &location) {
|
|
|
|
return location.cacheKey();
|
2022-08-01 12:14:37 +00:00
|
|
|
}, [&](const AudioAlbumThumbLocation &location) {
|
|
|
|
return Data::AudioAlbumThumbCacheKey(location);
|
2019-12-04 12:15:58 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<MediaKey> mtpFileLoader::fileLocationKey() const {
|
|
|
|
if (_locationType != UnknownFileLocation) {
|
|
|
|
return mediaKey(_locationType, dcId(), objId());
|
|
|
|
}
|
|
|
|
return std::nullopt;
|
|
|
|
}
|