2018-12-31 20:04:05 +01:00
|
|
|
//
|
2021-01-01 13:57:46 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#include "td/telegram/files/FileDownloader.h"
|
|
|
|
|
2019-01-23 17:00:56 +01:00
|
|
|
#include "td/telegram/FileReferenceManager.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/files/FileLoaderUtils.h"
|
2019-01-19 22:54:29 +01:00
|
|
|
#include "td/telegram/files/FileType.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/Global.h"
|
2018-06-26 01:43:11 +02:00
|
|
|
#include "td/telegram/net/DcId.h"
|
2018-12-27 19:38:43 +01:00
|
|
|
#include "td/telegram/SecureStorage.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/UniqueId.h"
|
|
|
|
|
2018-12-19 22:57:56 +01:00
|
|
|
#include "td/utils/as.h"
|
2018-12-05 10:32:31 +01:00
|
|
|
#include "td/utils/base64.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/buffer.h"
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
#include "td/utils/crypto.h"
|
|
|
|
#include "td/utils/format.h"
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/misc.h"
|
2018-06-05 19:44:41 +02:00
|
|
|
#include "td/utils/port/path.h"
|
2018-04-07 01:38:28 +02:00
|
|
|
#include "td/utils/port/Stat.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/ScopeGuard.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2018-12-19 22:18:53 +01:00
|
|
|
#include "td/utils/UInt.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include <tuple>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
FileDownloader::FileDownloader(const FullRemoteFileLocation &remote, const LocalFileLocation &local, int64 size,
|
2018-02-26 11:05:14 +01:00
|
|
|
string name, const FileEncryptionKey &encryption_key, bool is_small, bool search_file,
|
2019-02-18 20:08:05 +01:00
|
|
|
int64 offset, int64 limit, unique_ptr<Callback> callback)
|
2018-12-31 20:04:05 +01:00
|
|
|
: remote_(remote)
|
|
|
|
, local_(local)
|
|
|
|
, size_(size)
|
|
|
|
, name_(std::move(name))
|
|
|
|
, encryption_key_(encryption_key)
|
|
|
|
, callback_(std::move(callback))
|
2018-02-26 11:05:14 +01:00
|
|
|
, is_small_(is_small)
|
2018-11-11 12:38:04 +01:00
|
|
|
, search_file_(search_file)
|
2019-02-18 20:08:05 +01:00
|
|
|
, offset_(offset)
|
|
|
|
, limit_(limit) {
|
2018-06-08 22:15:04 +02:00
|
|
|
if (encryption_key.is_secret()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
set_ordered_flag(true);
|
|
|
|
}
|
2018-11-11 12:38:04 +01:00
|
|
|
if (!encryption_key.empty()) {
|
|
|
|
CHECK(offset_ == 0);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result<FileLoader::FileInfo> FileDownloader::init() {
|
|
|
|
SCOPE_EXIT {
|
|
|
|
try_release_fd();
|
|
|
|
};
|
2018-01-20 15:57:52 +01:00
|
|
|
if (local_.type() == LocalFileLocation::Type::Full) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::Error("File is already downloaded");
|
|
|
|
}
|
2018-04-03 19:49:07 +02:00
|
|
|
if (encryption_key_.is_secure() && !encryption_key_.has_value_hash()) {
|
|
|
|
LOG(ERROR) << "Can't download Secure file with unknown value_hash";
|
|
|
|
}
|
|
|
|
if (remote_.file_type_ == FileType::Secure) {
|
|
|
|
size_ = 0;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
int32 part_size = 0;
|
2018-11-11 12:38:04 +01:00
|
|
|
Bitmask bitmask{Bitmask::Ones{}, 0};
|
2018-01-20 15:57:52 +01:00
|
|
|
if (local_.type() == LocalFileLocation::Type::Partial) {
|
2018-12-31 20:04:05 +01:00
|
|
|
const auto &partial = local_.partial();
|
|
|
|
path_ = partial.path_;
|
|
|
|
auto result_fd = FileFd::open(path_, FileFd::Write | FileFd::Read);
|
|
|
|
// TODO: check timestamps..
|
2018-02-26 11:05:14 +01:00
|
|
|
if (result_fd.is_ok()) {
|
2018-11-11 12:38:04 +01:00
|
|
|
bitmask = Bitmask(Bitmask::Decode{}, partial.ready_bitmask_);
|
2018-04-03 19:49:07 +02:00
|
|
|
if (encryption_key_.is_secret()) {
|
2019-02-12 17:17:20 +01:00
|
|
|
LOG_CHECK(partial.iv_.size() == 32) << partial.iv_.size();
|
2018-12-31 20:04:05 +01:00
|
|
|
encryption_key_.mutable_iv() = as<UInt256>(partial.iv_.data());
|
2018-11-11 12:38:04 +01:00
|
|
|
next_part_ = narrow_cast<int32>(bitmask.get_ready_parts(0));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
fd_ = result_fd.move_as_ok();
|
|
|
|
part_size = partial.part_size_;
|
|
|
|
}
|
|
|
|
}
|
2018-03-06 19:31:20 +01:00
|
|
|
if (search_file_ && fd_.empty() && size_ > 0 && size_ < 1000 * (1 << 20) && encryption_key_.empty() &&
|
2018-02-26 11:05:14 +01:00
|
|
|
!remote_.is_web()) {
|
|
|
|
[&] {
|
|
|
|
TRY_RESULT(path, search_file(get_files_dir(remote_.file_type_), name_, size_));
|
|
|
|
TRY_RESULT(fd, FileFd::open(path, FileFd::Read));
|
|
|
|
LOG(INFO) << "Check hash of local file " << path;
|
|
|
|
path_ = std::move(path);
|
|
|
|
fd_ = std::move(fd);
|
|
|
|
need_check_ = true;
|
|
|
|
only_check_ = true;
|
|
|
|
part_size = 32 * (1 << 10);
|
2018-11-11 12:38:04 +01:00
|
|
|
bitmask = Bitmask{Bitmask::Ones{}, (size_ + part_size - 1) / part_size};
|
2018-02-26 11:05:14 +01:00
|
|
|
return Status::OK();
|
|
|
|
}();
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
FileInfo res;
|
|
|
|
res.size = size_;
|
|
|
|
res.is_size_final = true;
|
|
|
|
res.part_size = part_size;
|
2018-11-11 12:38:04 +01:00
|
|
|
res.ready_parts = bitmask.as_vector();
|
2018-12-31 20:04:05 +01:00
|
|
|
res.use_part_count_limit = false;
|
2018-03-16 10:31:23 +01:00
|
|
|
res.only_check = only_check_;
|
2020-06-22 01:02:21 +02:00
|
|
|
auto file_type = remote_.file_type_;
|
2018-12-26 17:11:15 +01:00
|
|
|
res.need_delay =
|
2020-06-22 01:02:21 +02:00
|
|
|
!is_small_ &&
|
|
|
|
(file_type == FileType::VideoNote || file_type == FileType::Document || file_type == FileType::DocumentAsFile ||
|
|
|
|
file_type == FileType::VoiceNote || file_type == FileType::Audio || file_type == FileType::Video ||
|
|
|
|
file_type == FileType::Animation || (file_type == FileType::Encrypted && size_ > (1 << 20)));
|
2018-11-11 12:38:04 +01:00
|
|
|
res.offset = offset_;
|
2019-02-18 20:08:05 +01:00
|
|
|
res.limit = limit_;
|
2018-12-31 20:04:05 +01:00
|
|
|
return res;
|
|
|
|
}
|
2020-01-02 01:38:47 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Status FileDownloader::on_ok(int64 size) {
|
2018-01-20 15:57:52 +01:00
|
|
|
auto dir = get_files_dir(remote_.file_type_);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-02-26 11:05:14 +01:00
|
|
|
std::string path;
|
2018-04-03 19:49:07 +02:00
|
|
|
fd_.close();
|
|
|
|
if (encryption_key_.is_secure()) {
|
|
|
|
TRY_RESULT(file_path, open_temp_file(remote_.file_type_));
|
|
|
|
string tmp_path;
|
|
|
|
std::tie(std::ignore, tmp_path) = std::move(file_path);
|
|
|
|
TRY_STATUS(secure_storage::decrypt_file(encryption_key_.secret(), encryption_key_.value_hash(), path_, tmp_path));
|
2018-05-30 18:57:49 +02:00
|
|
|
unlink(path_).ignore();
|
2018-04-03 19:49:07 +02:00
|
|
|
path_ = std::move(tmp_path);
|
|
|
|
TRY_RESULT(path_stat, stat(path_));
|
|
|
|
size = path_stat.size_;
|
|
|
|
}
|
2018-02-26 11:05:14 +01:00
|
|
|
if (only_check_) {
|
|
|
|
path = path_;
|
|
|
|
} else {
|
2019-12-08 07:57:33 +01:00
|
|
|
TRY_RESULT_ASSIGN(path, create_from_temp(path_, dir, name_));
|
2018-02-26 11:05:14 +01:00
|
|
|
}
|
2019-01-11 18:08:56 +01:00
|
|
|
callback_->on_ok(FullLocalFileLocation(remote_.file_type_, std::move(path), 0), size, !only_check_);
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::OK();
|
|
|
|
}
|
2020-01-02 01:38:47 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void FileDownloader::on_error(Status status) {
|
|
|
|
fd_.close();
|
|
|
|
callback_->on_error(std::move(status));
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<bool> FileDownloader::should_restart_part(Part part, NetQueryPtr &net_query) {
|
|
|
|
// Check if we should use CDN or reupload file to CDN
|
|
|
|
|
|
|
|
if (net_query->is_error()) {
|
|
|
|
if (net_query->error().message() == "FILE_TOKEN_INVALID") {
|
|
|
|
use_cdn_ = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (net_query->error().message() == "REQUEST_TOKEN_INVALID") {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (narrow_cast<QueryType>(UniqueId::extract_key(net_query->id()))) {
|
|
|
|
case QueryType::Default: {
|
|
|
|
if (net_query->ok_tl_constructor() == telegram_api::upload_fileCdnRedirect::ID) {
|
|
|
|
TRY_RESULT(file_base, fetch_result<telegram_api::upload_getFile>(net_query->ok()));
|
|
|
|
CHECK(file_base->get_id() == telegram_api::upload_fileCdnRedirect::ID);
|
|
|
|
auto file = move_tl_object_as<telegram_api::upload_fileCdnRedirect>(file_base);
|
2019-01-15 18:57:09 +01:00
|
|
|
LOG(DEBUG) << part.id << " got REDIRECT " << to_string(file);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
auto new_cdn_file_token = file->file_token_.as_slice();
|
|
|
|
if (cdn_file_token_ == new_cdn_file_token) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
use_cdn_ = true;
|
|
|
|
need_check_ = true;
|
|
|
|
cdn_file_token_generation_++;
|
|
|
|
cdn_file_token_ = new_cdn_file_token.str();
|
|
|
|
cdn_dc_id_ = DcId::external(file->dc_id_);
|
|
|
|
cdn_encryption_key_ = file->encryption_key_.as_slice().str();
|
|
|
|
cdn_encryption_iv_ = file->encryption_iv_.as_slice().str();
|
2018-02-25 19:56:16 +01:00
|
|
|
add_hash_info(file->file_hashes_);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (cdn_encryption_iv_.size() != 16 || cdn_encryption_key_.size() != 32) {
|
|
|
|
return Status::Error("Wrong ctr key or iv size");
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
case QueryType::ReuploadCDN: {
|
|
|
|
TRY_RESULT(file_hashes, fetch_result<telegram_api::upload_reuploadCdnFile>(net_query->ok()));
|
|
|
|
add_hash_info(file_hashes);
|
|
|
|
LOG(DEBUG) << part.id << " got REUPLOAD_OK";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case QueryType::CDN: {
|
|
|
|
if (net_query->ok_tl_constructor() == telegram_api::upload_cdnFileReuploadNeeded::ID) {
|
|
|
|
TRY_RESULT(file_base, fetch_result<telegram_api::upload_getCdnFile>(net_query->ok()));
|
|
|
|
CHECK(file_base->get_id() == telegram_api::upload_cdnFileReuploadNeeded::ID);
|
|
|
|
auto file = move_tl_object_as<telegram_api::upload_cdnFileReuploadNeeded>(file_base);
|
2019-01-15 18:57:09 +01:00
|
|
|
LOG(DEBUG) << part.id << " got REUPLOAD " << to_string(file);
|
2018-12-31 20:04:05 +01:00
|
|
|
cdn_part_reupload_token_[part.id] = file->request_token_.as_slice().str();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
auto it = cdn_part_file_token_generation_.find(part.id);
|
|
|
|
CHECK(it != cdn_part_file_token_generation_.end());
|
|
|
|
if (it->second != cdn_file_token_generation_) {
|
|
|
|
LOG(DEBUG) << part.id << " got part with old file_token";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2020-01-02 01:38:47 +01:00
|
|
|
|
2020-01-08 18:15:49 +01:00
|
|
|
Result<std::pair<NetQueryPtr, bool>> FileDownloader::start_part(Part part, int32 part_count, int64 streaming_offset) {
|
2018-04-03 19:49:07 +02:00
|
|
|
if (encryption_key_.is_secret()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
part.size = (part.size + 15) & ~15; // fix for last part
|
|
|
|
}
|
|
|
|
// auto size = part.size;
|
|
|
|
//// sometimes we can ask more than server has, just to check size
|
|
|
|
// if (size < get_part_size()) {
|
2018-02-12 11:37:54 +01:00
|
|
|
// size = min(size + 16, get_part_size());
|
2018-12-31 20:04:05 +01:00
|
|
|
// LOG(INFO) << "Ask " << size << " instead of " << part.size;
|
|
|
|
//}
|
|
|
|
auto size = get_part_size();
|
|
|
|
CHECK(part.size <= size);
|
|
|
|
|
2018-01-09 12:57:11 +01:00
|
|
|
callback_->on_start_download();
|
|
|
|
|
2020-03-15 02:15:54 +01:00
|
|
|
auto net_query_type = is_small_ ? NetQuery::Type::DownloadSmall : NetQuery::Type::Download;
|
2018-12-31 20:04:05 +01:00
|
|
|
NetQueryPtr net_query;
|
|
|
|
if (!use_cdn_) {
|
2020-01-03 00:14:02 +01:00
|
|
|
int32 flags = 0;
|
|
|
|
#if !TD_EMSCRIPTEN
|
|
|
|
// CDN is supported, unless we use domains instead of IPs from a browser
|
2020-01-08 18:15:49 +01:00
|
|
|
if (streaming_offset == 0) {
|
|
|
|
flags |= telegram_api::upload_getFile::CDN_SUPPORTED_MASK;
|
|
|
|
}
|
2020-01-03 00:14:02 +01:00
|
|
|
#endif
|
2018-06-25 23:10:53 +02:00
|
|
|
DcId dc_id = remote_.is_web() ? G()->get_webfile_dc_id() : remote_.get_dc_id();
|
2020-03-15 23:01:14 +01:00
|
|
|
auto id = UniqueId::next(UniqueId::Type::Default, static_cast<uint8>(QueryType::Default));
|
|
|
|
net_query = remote_.is_web()
|
|
|
|
? G()->net_query_creator().create(
|
|
|
|
id,
|
|
|
|
telegram_api::upload_getWebFile(remote_.as_input_web_file_location(),
|
|
|
|
static_cast<int32>(part.offset), static_cast<int32>(size)),
|
|
|
|
dc_id, net_query_type, NetQuery::AuthFlag::On)
|
|
|
|
: G()->net_query_creator().create(
|
|
|
|
id,
|
|
|
|
telegram_api::upload_getFile(flags, false /*ignored*/, false /*ignored*/,
|
|
|
|
remote_.as_input_file_location(),
|
|
|
|
static_cast<int32>(part.offset), static_cast<int32>(size)),
|
|
|
|
dc_id, net_query_type, NetQuery::AuthFlag::On);
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
|
|
|
if (remote_.is_web()) {
|
|
|
|
return Status::Error("Can't download web file from CDN");
|
|
|
|
}
|
|
|
|
auto it = cdn_part_reupload_token_.find(part.id);
|
|
|
|
if (it == cdn_part_reupload_token_.end()) {
|
|
|
|
auto query = telegram_api::upload_getCdnFile(BufferSlice(cdn_file_token_), static_cast<int32>(part.offset),
|
|
|
|
static_cast<int32>(size));
|
|
|
|
cdn_part_file_token_generation_[part.id] = cdn_file_token_generation_;
|
|
|
|
LOG(DEBUG) << part.id << " " << to_string(query);
|
2020-03-15 02:15:54 +01:00
|
|
|
net_query =
|
|
|
|
G()->net_query_creator().create(UniqueId::next(UniqueId::Type::Default, static_cast<uint8>(QueryType::CDN)),
|
2020-03-15 23:01:14 +01:00
|
|
|
query, cdn_dc_id_, net_query_type, NetQuery::AuthFlag::Off);
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
|
|
|
auto query = telegram_api::upload_reuploadCdnFile(BufferSlice(cdn_file_token_), BufferSlice(it->second));
|
|
|
|
LOG(DEBUG) << part.id << " " << to_string(query);
|
|
|
|
net_query = G()->net_query_creator().create(
|
2020-03-15 23:01:14 +01:00
|
|
|
UniqueId::next(UniqueId::Type::Default, static_cast<uint8>(QueryType::ReuploadCDN)), query,
|
2020-03-15 02:15:54 +01:00
|
|
|
remote_.get_dc_id(), net_query_type, NetQuery::AuthFlag::On);
|
2018-12-31 20:04:05 +01:00
|
|
|
cdn_part_reupload_token_.erase(it);
|
|
|
|
}
|
|
|
|
}
|
2018-01-20 15:57:52 +01:00
|
|
|
net_query->file_type_ = narrow_cast<int32>(remote_.file_type_);
|
2018-12-31 20:04:05 +01:00
|
|
|
return std::make_pair(std::move(net_query), false);
|
|
|
|
}
|
|
|
|
|
2018-12-05 10:32:31 +01:00
|
|
|
Status FileDownloader::check_net_query(NetQueryPtr &net_query) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (net_query->is_error()) {
|
2018-12-05 10:32:31 +01:00
|
|
|
auto error = net_query->move_as_error();
|
2019-01-23 17:00:56 +01:00
|
|
|
if (FileReferenceManager::is_file_reference_error(error)) {
|
2019-04-19 00:03:33 +02:00
|
|
|
VLOG(file_references) << "Receive " << error << " for being downloaded file";
|
2019-06-17 04:37:57 +02:00
|
|
|
error = Status::Error(error.code(),
|
|
|
|
PSLICE() << error.message() << "#BASE64" << base64_encode(remote_.get_file_reference()));
|
2018-12-05 10:32:31 +01:00
|
|
|
}
|
|
|
|
return error;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-12-05 10:32:31 +01:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<size_t> FileDownloader::process_part(Part part, NetQueryPtr net_query) {
|
|
|
|
TRY_STATUS(check_net_query(net_query));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
BufferSlice bytes;
|
|
|
|
bool need_cdn_decrypt = false;
|
|
|
|
auto query_type = narrow_cast<QueryType>(UniqueId::extract_key(net_query->id()));
|
|
|
|
switch (query_type) {
|
|
|
|
case QueryType::Default: {
|
|
|
|
if (remote_.is_web()) {
|
|
|
|
TRY_RESULT(file, fetch_result<telegram_api::upload_getWebFile>(net_query->ok()));
|
|
|
|
bytes = std::move(file->bytes_);
|
|
|
|
} else {
|
|
|
|
TRY_RESULT(file_base, fetch_result<telegram_api::upload_getFile>(net_query->ok()));
|
|
|
|
CHECK(file_base->get_id() == telegram_api::upload_file::ID);
|
|
|
|
auto file = move_tl_object_as<telegram_api::upload_file>(file_base);
|
2019-01-15 18:57:09 +01:00
|
|
|
LOG(DEBUG) << part.id << " upload_getFile result " << to_string(file);
|
2018-12-31 20:04:05 +01:00
|
|
|
bytes = std::move(file->bytes_);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case QueryType::CDN: {
|
|
|
|
TRY_RESULT(file_base, fetch_result<telegram_api::upload_getCdnFile>(net_query->ok()));
|
|
|
|
CHECK(file_base->get_id() == telegram_api::upload_cdnFile::ID);
|
|
|
|
auto file = move_tl_object_as<telegram_api::upload_cdnFile>(file_base);
|
2019-01-15 18:57:09 +01:00
|
|
|
LOG(DEBUG) << part.id << " upload_getCdnFile result " << to_string(file);
|
2018-12-31 20:04:05 +01:00
|
|
|
bytes = std::move(file->bytes_);
|
|
|
|
need_cdn_decrypt = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto padded_size = part.size;
|
2018-04-03 19:49:07 +02:00
|
|
|
if (encryption_key_.is_secret()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
padded_size = (part.size + 15) & ~15;
|
|
|
|
}
|
|
|
|
if (bytes.size() > padded_size) {
|
|
|
|
return Status::Error("Part size is more than requested");
|
|
|
|
}
|
|
|
|
if (bytes.empty()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encryption
|
|
|
|
if (need_cdn_decrypt) {
|
|
|
|
CHECK(part.offset % 16 == 0);
|
|
|
|
auto offset = narrow_cast<uint32>(part.offset / 16);
|
|
|
|
offset =
|
|
|
|
((offset & 0xff) << 24) | ((offset & 0xff00) << 8) | ((offset & 0xff0000) >> 8) | ((offset & 0xff000000) >> 24);
|
|
|
|
|
|
|
|
AesCtrState ctr_state;
|
2019-08-13 22:52:54 +02:00
|
|
|
string iv = cdn_encryption_iv_;
|
|
|
|
as<uint32>(&iv[12]) = offset;
|
|
|
|
ctr_state.init(cdn_encryption_key_, iv);
|
2018-12-31 20:04:05 +01:00
|
|
|
ctr_state.decrypt(bytes.as_slice(), bytes.as_slice());
|
|
|
|
}
|
2018-04-03 19:49:07 +02:00
|
|
|
if (encryption_key_.is_secret()) {
|
2019-02-12 17:17:20 +01:00
|
|
|
LOG_CHECK(next_part_ == part.id) << tag("expected part.id", next_part_) << "!=" << tag("part.id", part.id);
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(!next_part_stop_);
|
|
|
|
next_part_++;
|
|
|
|
if (part.size % 16 != 0) {
|
|
|
|
next_part_stop_ = true;
|
|
|
|
}
|
2019-08-12 15:53:17 +02:00
|
|
|
aes_ige_decrypt(as_slice(encryption_key_.key()), as_slice(encryption_key_.mutable_iv()), bytes.as_slice(),
|
|
|
|
bytes.as_slice());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-02-04 13:54:12 +01:00
|
|
|
auto slice = bytes.as_slice().substr(0, part.size);
|
2018-12-31 20:04:05 +01:00
|
|
|
TRY_STATUS(acquire_fd());
|
2019-02-20 02:23:02 +01:00
|
|
|
LOG(INFO) << "Got " << slice.size() << " bytes at offset " << part.offset << " for \"" << path_ << '"';
|
2018-12-31 20:04:05 +01:00
|
|
|
TRY_RESULT(written, fd_.pwrite(slice, part.offset));
|
2020-01-02 01:38:47 +01:00
|
|
|
LOG(INFO) << "Written " << written << " bytes";
|
2018-12-31 20:04:05 +01:00
|
|
|
// may write less than part.size, when size of downloadable file is unknown
|
|
|
|
if (written != slice.size()) {
|
|
|
|
return Status::Error("Failed to save file part to the file");
|
|
|
|
}
|
|
|
|
return written;
|
|
|
|
}
|
2020-01-02 01:38:47 +01:00
|
|
|
|
2018-12-26 22:42:26 +01:00
|
|
|
void FileDownloader::on_progress(Progress progress) {
|
|
|
|
if (progress.is_ready) {
|
2018-12-31 20:04:05 +01:00
|
|
|
// do not send partial location. will lead to wrong local_size
|
|
|
|
return;
|
|
|
|
}
|
2018-12-26 22:42:26 +01:00
|
|
|
if (progress.ready_size == 0 || path_.empty()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-04-03 19:49:07 +02:00
|
|
|
if (encryption_key_.empty() || encryption_key_.is_secure()) {
|
2018-12-26 22:42:26 +01:00
|
|
|
callback_->on_partial_download(
|
|
|
|
PartialLocalFileLocation{remote_.file_type_, progress.part_size, path_, "", std::move(progress.ready_bitmask)},
|
|
|
|
progress.ready_size, progress.size);
|
2018-04-03 19:49:07 +02:00
|
|
|
} else if (encryption_key_.is_secret()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
UInt256 iv;
|
2018-12-26 22:42:26 +01:00
|
|
|
if (progress.ready_part_count == next_part_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
iv = encryption_key_.mutable_iv();
|
|
|
|
} else {
|
2018-12-26 22:42:26 +01:00
|
|
|
LOG(FATAL) << tag("ready_part_count", progress.ready_part_count) << tag("next_part", next_part_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-12-26 22:42:26 +01:00
|
|
|
callback_->on_partial_download(PartialLocalFileLocation{remote_.file_type_, progress.part_size, path_,
|
|
|
|
as_slice(iv).str(), std::move(progress.ready_bitmask)},
|
|
|
|
progress.ready_size, progress.size);
|
2018-04-03 19:49:07 +02:00
|
|
|
} else {
|
|
|
|
UNREACHABLE();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FileLoader::Callback *FileDownloader::get_callback() {
|
|
|
|
return static_cast<FileLoader::Callback *>(callback_.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
Status FileDownloader::process_check_query(NetQueryPtr net_query) {
|
|
|
|
has_hash_query_ = false;
|
2018-12-05 10:32:31 +01:00
|
|
|
TRY_STATUS(check_net_query(net_query));
|
2018-01-29 13:27:28 +01:00
|
|
|
TRY_RESULT(file_hashes, fetch_result<telegram_api::upload_getCdnFileHashes>(std::move(net_query)));
|
2018-12-31 20:04:05 +01:00
|
|
|
add_hash_info(file_hashes);
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2018-06-25 23:10:53 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Result<FileLoader::CheckInfo> FileDownloader::check_loop(int64 checked_prefix_size, int64 ready_prefix_size,
|
|
|
|
bool is_ready) {
|
|
|
|
if (!need_check_) {
|
|
|
|
return CheckInfo{};
|
|
|
|
}
|
|
|
|
SCOPE_EXIT {
|
|
|
|
try_release_fd();
|
|
|
|
};
|
|
|
|
CheckInfo info;
|
|
|
|
while (checked_prefix_size < ready_prefix_size) {
|
|
|
|
//LOG(ERROR) << "NEED TO CHECK: " << checked_prefix_size << "->" << ready_prefix_size - checked_prefix_size;
|
|
|
|
HashInfo search_info;
|
|
|
|
search_info.offset = checked_prefix_size;
|
|
|
|
auto it = hash_info_.upper_bound(search_info);
|
|
|
|
if (it != hash_info_.begin()) {
|
2018-04-23 00:14:49 +02:00
|
|
|
--it;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (it != hash_info_.end() && it->offset <= checked_prefix_size &&
|
|
|
|
it->offset + narrow_cast<int64>(it->size) > checked_prefix_size) {
|
|
|
|
int64 begin_offset = it->offset;
|
|
|
|
int64 end_offset = it->offset + narrow_cast<int64>(it->size);
|
|
|
|
if (ready_prefix_size < end_offset) {
|
|
|
|
if (!is_ready) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
end_offset = ready_prefix_size;
|
|
|
|
}
|
2021-10-19 17:11:16 +02:00
|
|
|
auto size = narrow_cast<size_t>(end_offset - begin_offset);
|
2018-12-31 20:04:05 +01:00
|
|
|
auto slice = BufferSlice(size);
|
|
|
|
TRY_STATUS(acquire_fd());
|
|
|
|
TRY_RESULT(read_size, fd_.pread(slice.as_slice(), begin_offset));
|
|
|
|
if (size != read_size) {
|
|
|
|
return Status::Error("Failed to read file to check hash");
|
|
|
|
}
|
|
|
|
string hash(32, ' ');
|
|
|
|
sha256(slice.as_slice(), hash);
|
|
|
|
|
|
|
|
if (hash != it->hash) {
|
2018-02-26 11:05:14 +01:00
|
|
|
if (only_check_) {
|
|
|
|
return Status::Error("FILE_DOWNLOAD_RESTART");
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::Error("Hash mismatch");
|
|
|
|
}
|
|
|
|
|
|
|
|
checked_prefix_size = end_offset;
|
2018-02-26 11:05:14 +01:00
|
|
|
info.changed = true;
|
2018-12-31 20:04:05 +01:00
|
|
|
continue;
|
|
|
|
}
|
2018-02-26 11:05:14 +01:00
|
|
|
if (!has_hash_query_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
has_hash_query_ = true;
|
|
|
|
auto query =
|
2018-02-26 11:05:14 +01:00
|
|
|
telegram_api::upload_getFileHashes(remote_.as_input_file_location(), narrow_cast<int32>(checked_prefix_size));
|
2020-03-15 02:15:54 +01:00
|
|
|
auto net_query_type = is_small_ ? NetQuery::Type::DownloadSmall : NetQuery::Type::Download;
|
2020-03-15 23:01:14 +01:00
|
|
|
auto net_query = G()->net_query_creator().create(query, remote_.get_dc_id(), net_query_type);
|
2018-12-31 20:04:05 +01:00
|
|
|
info.queries.push_back(std::move(net_query));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Should fail?
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
info.need_check = need_check_;
|
|
|
|
info.checked_prefix_size = checked_prefix_size;
|
|
|
|
return std::move(info);
|
|
|
|
}
|
2018-06-25 23:10:53 +02:00
|
|
|
|
2018-02-25 19:56:16 +01:00
|
|
|
void FileDownloader::add_hash_info(const std::vector<telegram_api::object_ptr<telegram_api::fileHash>> &hashes) {
|
2018-12-31 20:04:05 +01:00
|
|
|
for (auto &hash : hashes) {
|
|
|
|
//LOG(ERROR) << "ADD HASH " << hash->offset_ << "->" << hash->limit_;
|
|
|
|
HashInfo hash_info;
|
|
|
|
hash_info.size = hash->limit_;
|
|
|
|
hash_info.offset = hash->offset_;
|
|
|
|
hash_info.hash = hash->hash_.as_slice().str();
|
|
|
|
hash_info_.insert(std::move(hash_info));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileDownloader::keep_fd_flag(bool keep_fd) {
|
|
|
|
keep_fd_ = keep_fd;
|
|
|
|
try_release_fd();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileDownloader::try_release_fd() {
|
|
|
|
if (!keep_fd_ && !fd_.empty()) {
|
|
|
|
fd_.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Status FileDownloader::acquire_fd() {
|
|
|
|
if (fd_.empty()) {
|
|
|
|
if (path_.empty()) {
|
2019-12-08 07:57:33 +01:00
|
|
|
TRY_RESULT_ASSIGN(std::tie(fd_, path_), open_temp_file(remote_.file_type_));
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2019-12-08 07:57:33 +01:00
|
|
|
TRY_RESULT_ASSIGN(fd_, FileFd::open(path_, (only_check_ ? 0 : FileFd::Write) | FileFd::Read));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|