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"
|
|
|
|
#include "mtproto/mtproto_auth_key.h"
|
|
|
|
#include "base/openssl_help.h"
|
|
|
|
#include "facades.h"
|
|
|
|
|
|
|
|
mtpFileLoader::mtpFileLoader(
|
|
|
|
const StorageFileLocation &location,
|
|
|
|
Data::FileOrigin origin,
|
|
|
|
LocationType type,
|
|
|
|
const QString &to,
|
|
|
|
int32 size,
|
|
|
|
LoadToCacheSetting toCache,
|
|
|
|
LoadFromCloudSetting fromCloud,
|
|
|
|
bool autoLoading,
|
|
|
|
uint8 cacheTag)
|
|
|
|
: FileLoader(
|
|
|
|
to,
|
|
|
|
size,
|
|
|
|
type,
|
|
|
|
toCache,
|
|
|
|
fromCloud,
|
|
|
|
autoLoading,
|
|
|
|
cacheTag)
|
2019-12-05 08:32:33 +00:00
|
|
|
, DownloadMtprotoTask(&session().downloader(), location, origin) {
|
2019-12-04 12:15:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mtpFileLoader::mtpFileLoader(
|
|
|
|
const WebFileLocation &location,
|
|
|
|
int32 size,
|
|
|
|
LoadFromCloudSetting fromCloud,
|
|
|
|
bool autoLoading,
|
|
|
|
uint8 cacheTag)
|
|
|
|
: FileLoader(
|
|
|
|
QString(),
|
|
|
|
size,
|
|
|
|
UnknownFileLocation,
|
|
|
|
LoadToCacheAsWell,
|
|
|
|
fromCloud,
|
|
|
|
autoLoading,
|
|
|
|
cacheTag)
|
2019-12-05 08:32:33 +00:00
|
|
|
, DownloadMtprotoTask(
|
|
|
|
&session().downloader(),
|
|
|
|
Global::WebFileDcId(),
|
|
|
|
{ location }) {
|
2019-12-04 12:15:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mtpFileLoader::mtpFileLoader(
|
|
|
|
const GeoPointLocation &location,
|
|
|
|
int32 size,
|
|
|
|
LoadFromCloudSetting fromCloud,
|
|
|
|
bool autoLoading,
|
|
|
|
uint8 cacheTag)
|
|
|
|
: FileLoader(
|
|
|
|
QString(),
|
|
|
|
size,
|
|
|
|
UnknownFileLocation,
|
|
|
|
LoadToCacheAsWell,
|
|
|
|
fromCloud,
|
|
|
|
autoLoading,
|
|
|
|
cacheTag)
|
2019-12-05 08:32:33 +00:00
|
|
|
, DownloadMtprotoTask(
|
|
|
|
&session().downloader(),
|
|
|
|
Global::WebFileDcId(),
|
|
|
|
{ location }) {
|
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
|
2019-12-05 08:32:33 +00:00
|
|
|
&& (_size != 0 || !haveSentRequests())
|
2019-12-04 12:15:58 +00:00
|
|
|
&& (!_size || _nextRequestOffset < _size);
|
|
|
|
}
|
|
|
|
|
2019-12-05 08:32:33 +00:00
|
|
|
int 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;
|
|
|
|
}
|
|
|
|
|
2019-12-05 08:32:33 +00:00
|
|
|
bool mtpFileLoader::feedPart(int offset, const QByteArray &bytes) {
|
|
|
|
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 weak = QPointer<mtpFileLoader>(this);
|
|
|
|
const auto finished = !haveSentRequests()
|
2019-12-04 12:15:58 +00:00
|
|
|
&& (_lastComplete || (_size && _nextRequestOffset >= _size));
|
|
|
|
if (finished) {
|
2019-12-05 08:32:33 +00:00
|
|
|
removeFromQueue();
|
2019-12-04 12:15:58 +00:00
|
|
|
if (!finalizeResult()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2019-12-05 08:32:33 +00:00
|
|
|
if (weak) {
|
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() {
|
2019-12-04 12:15:58 +00:00
|
|
|
cancel(true);
|
|
|
|
}
|
|
|
|
|
2019-12-05 08:32:33 +00:00
|
|
|
bool mtpFileLoader::setWebFileSizeHook(int size) {
|
|
|
|
if (!_size || _size == size) {
|
|
|
|
_size = 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"
|
|
|
|
).arg(_size
|
|
|
|
).arg(size));
|
|
|
|
cancel(true);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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 {
|
2019-12-05 08:32:33 +00:00
|
|
|
return location().data.match([&](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();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<MediaKey> mtpFileLoader::fileLocationKey() const {
|
|
|
|
if (_locationType != UnknownFileLocation) {
|
|
|
|
return mediaKey(_locationType, dcId(), objId());
|
|
|
|
}
|
|
|
|
return std::nullopt;
|
|
|
|
}
|