2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
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/FileLoadManager.h"
|
|
|
|
|
|
|
|
#include "td/telegram/Global.h"
|
2018-06-25 23:10:53 +02:00
|
|
|
#include "td/telegram/net/DcId.h"
|
2021-09-18 23:47:05 +02:00
|
|
|
#include "td/telegram/TdParameters.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
#include "td/utils/filesystem.h"
|
2018-03-06 17:27:52 +01:00
|
|
|
#include "td/utils/format.h"
|
2022-06-16 12:32:14 +02:00
|
|
|
#include "td/utils/port/path.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
namespace td {
|
2018-05-30 19:50:11 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
FileLoadManager::FileLoadManager(ActorShared<Callback> callback, ActorShared<> parent)
|
|
|
|
: callback_(std::move(callback)), parent_(std::move(parent)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileLoadManager::start_up() {
|
2022-07-28 16:34:29 +02:00
|
|
|
constexpr int64 MAX_UPLOAD_RESOURCE_LIMIT = 4 << 20;
|
|
|
|
upload_resource_manager_ = create_actor<ResourceManager>("UploadResourceManager", MAX_UPLOAD_RESOURCE_LIMIT,
|
2022-05-20 15:11:31 +02:00
|
|
|
!G()->parameters().use_file_db /*tdlib_engine*/
|
|
|
|
? ResourceManager::Mode::Greedy
|
|
|
|
: ResourceManager::Mode::Baseline);
|
2022-08-17 15:11:13 +02:00
|
|
|
if (G()->get_option_boolean("is_premium")) {
|
2022-07-28 16:34:29 +02:00
|
|
|
max_download_resource_limit_ *= 8;
|
|
|
|
}
|
2018-02-27 20:47:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ActorOwn<ResourceManager> &FileLoadManager::get_download_resource_manager(bool is_small, DcId dc_id) {
|
|
|
|
auto &actor = is_small ? download_small_resource_manager_map_[dc_id] : download_resource_manager_map_[dc_id];
|
|
|
|
if (actor.empty()) {
|
|
|
|
actor = create_actor<ResourceManager>(
|
2022-07-28 16:34:29 +02:00
|
|
|
PSLICE() << "DownloadResourceManager " << tag("is_small", is_small) << tag("dc_id", dc_id),
|
|
|
|
max_download_resource_limit_, ResourceManager::Mode::Baseline);
|
2018-02-27 20:47:14 +01:00
|
|
|
}
|
|
|
|
return actor;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2023-01-16 11:30:03 +01:00
|
|
|
void FileLoadManager::download(QueryId query_id, const FullRemoteFileLocation &remote_location,
|
2018-12-31 20:04:05 +01:00
|
|
|
const LocalFileLocation &local, int64 size, string name,
|
2019-02-18 20:08:05 +01:00
|
|
|
const FileEncryptionKey &encryption_key, bool search_file, int64 offset, int64 limit,
|
|
|
|
int8 priority) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (stop_flag_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
NodeId node_id = nodes_container_.create(Node());
|
|
|
|
Node *node = nodes_container_.get(node_id);
|
|
|
|
CHECK(node);
|
2023-01-16 11:30:03 +01:00
|
|
|
node->query_id_ = query_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
auto callback = make_unique<FileDownloaderCallback>(actor_shared(this, node_id));
|
|
|
|
bool is_small = size < 20 * 1024;
|
2019-02-18 20:08:05 +01:00
|
|
|
node->loader_ =
|
|
|
|
create_actor<FileDownloader>("Downloader", remote_location, local, size, std::move(name), encryption_key,
|
|
|
|
is_small, search_file, offset, limit, std::move(callback));
|
2018-06-25 23:10:53 +02:00
|
|
|
DcId dc_id = remote_location.is_web() ? G()->get_webfile_dc_id() : remote_location.get_dc_id();
|
|
|
|
auto &resource_manager = get_download_resource_manager(is_small, dc_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure(resource_manager, &ResourceManager::register_worker,
|
|
|
|
ActorShared<FileLoaderActor>(node->loader_.get(), static_cast<uint64>(-1)), priority);
|
2023-01-16 11:30:03 +01:00
|
|
|
bool is_inserted = query_id_to_node_id_.emplace(query_id, node_id).second;
|
2022-06-02 15:19:16 +02:00
|
|
|
CHECK(is_inserted);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2023-01-16 11:30:03 +01:00
|
|
|
void FileLoadManager::upload(QueryId query_id, const LocalFileLocation &local_location,
|
2018-12-27 09:34:36 +01:00
|
|
|
const RemoteFileLocation &remote_location, int64 expected_size,
|
2018-01-20 12:47:53 +01:00
|
|
|
const FileEncryptionKey &encryption_key, int8 priority, vector<int> bad_parts) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (stop_flag_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
NodeId node_id = nodes_container_.create(Node());
|
|
|
|
Node *node = nodes_container_.get(node_id);
|
|
|
|
CHECK(node);
|
2023-01-16 11:30:03 +01:00
|
|
|
node->query_id_ = query_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
auto callback = make_unique<FileUploaderCallback>(actor_shared(this, node_id));
|
2018-12-27 09:34:36 +01:00
|
|
|
node->loader_ = create_actor<FileUploader>("Uploader", local_location, remote_location, expected_size, encryption_key,
|
2018-12-31 20:04:05 +01:00
|
|
|
std::move(bad_parts), std::move(callback));
|
|
|
|
send_closure(upload_resource_manager_, &ResourceManager::register_worker,
|
|
|
|
ActorShared<FileLoaderActor>(node->loader_.get(), static_cast<uint64>(-1)), priority);
|
2023-01-16 11:30:03 +01:00
|
|
|
bool is_inserted = query_id_to_node_id_.emplace(query_id, node_id).second;
|
2022-06-02 15:19:16 +02:00
|
|
|
CHECK(is_inserted);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2023-01-16 11:30:03 +01:00
|
|
|
void FileLoadManager::upload_by_hash(QueryId query_id, const FullLocalFileLocation &local_location, int64 size,
|
2018-01-20 12:47:53 +01:00
|
|
|
int8 priority) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (stop_flag_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
NodeId node_id = nodes_container_.create(Node());
|
|
|
|
Node *node = nodes_container_.get(node_id);
|
|
|
|
CHECK(node);
|
2023-01-16 11:30:03 +01:00
|
|
|
node->query_id_ = query_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
auto callback = make_unique<FileHashUploaderCallback>(actor_shared(this, node_id));
|
|
|
|
node->loader_ = create_actor<FileHashUploader>("HashUploader", local_location, size, std::move(callback));
|
|
|
|
send_closure(upload_resource_manager_, &ResourceManager::register_worker,
|
|
|
|
ActorShared<FileLoaderActor>(node->loader_.get(), static_cast<uint64>(-1)), priority);
|
2023-01-16 11:30:03 +01:00
|
|
|
bool is_inserted = query_id_to_node_id_.emplace(query_id, node_id).second;
|
2022-06-02 15:19:16 +02:00
|
|
|
CHECK(is_inserted);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2023-01-16 11:30:03 +01:00
|
|
|
void FileLoadManager::update_priority(QueryId query_id, int8 priority) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (stop_flag_) {
|
|
|
|
return;
|
|
|
|
}
|
2023-01-16 11:30:03 +01:00
|
|
|
auto it = query_id_to_node_id_.find(query_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (it == query_id_to_node_id_.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto node = nodes_container_.get(it->second);
|
|
|
|
if (node == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
send_closure(node->loader_, &FileLoaderActor::update_priority, priority);
|
|
|
|
}
|
|
|
|
|
2023-01-16 11:30:03 +01:00
|
|
|
void FileLoadManager::from_bytes(QueryId query_id, FileType type, BufferSlice bytes, string name) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (stop_flag_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
NodeId node_id = nodes_container_.create(Node());
|
|
|
|
Node *node = nodes_container_.get(node_id);
|
|
|
|
CHECK(node);
|
2023-01-16 11:30:03 +01:00
|
|
|
node->query_id_ = query_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
auto callback = make_unique<FileFromBytesCallback>(actor_shared(this, node_id));
|
|
|
|
node->loader_ =
|
|
|
|
create_actor<FileFromBytes>("FromBytes", type, std::move(bytes), std::move(name), std::move(callback));
|
2023-01-16 11:30:03 +01:00
|
|
|
bool is_inserted = query_id_to_node_id_.emplace(query_id, node_id).second;
|
2022-06-02 15:19:16 +02:00
|
|
|
CHECK(is_inserted);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-06-15 20:40:23 +02:00
|
|
|
void FileLoadManager::get_content(string file_path, Promise<BufferSlice> promise) {
|
|
|
|
promise.set_result(read_file(file_path));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-06-15 23:25:47 +02:00
|
|
|
void FileLoadManager::read_file_part(string file_path, int64 offset, int64 count, Promise<string> promise) {
|
|
|
|
promise.set_result(read_file_str(file_path, count, offset));
|
|
|
|
}
|
|
|
|
|
2022-06-16 12:32:14 +02:00
|
|
|
void FileLoadManager::unlink_file(string file_path, Promise<Unit> promise) {
|
|
|
|
unlink(file_path).ignore();
|
|
|
|
promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
2022-10-10 13:19:35 +02:00
|
|
|
void FileLoadManager::check_full_local_location(FullLocalLocationInfo local_info, bool skip_file_size_checks,
|
|
|
|
Promise<FullLocalLocationInfo> promise) {
|
|
|
|
promise.set_result(::td::check_full_local_location(std::move(local_info), skip_file_size_checks));
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileLoadManager::check_partial_local_location(PartialLocalFileLocation partial, Promise<Unit> promise) {
|
|
|
|
auto status = ::td::check_partial_local_location(partial);
|
|
|
|
if (status.is_error()) {
|
|
|
|
promise.set_error(std::move(status));
|
|
|
|
} else {
|
|
|
|
promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-16 11:30:03 +01:00
|
|
|
void FileLoadManager::cancel(QueryId query_id) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (stop_flag_) {
|
|
|
|
return;
|
|
|
|
}
|
2023-01-16 11:30:03 +01:00
|
|
|
auto it = query_id_to_node_id_.find(query_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (it == query_id_to_node_id_.end()) {
|
|
|
|
return;
|
|
|
|
}
|
2022-09-20 01:02:28 +02:00
|
|
|
on_error_impl(it->second, Status::Error(-1, "Canceled"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2023-01-16 11:30:03 +01:00
|
|
|
void FileLoadManager::update_local_file_location(QueryId query_id, const LocalFileLocation &local) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (stop_flag_) {
|
|
|
|
return;
|
|
|
|
}
|
2023-01-16 11:30:03 +01:00
|
|
|
auto it = query_id_to_node_id_.find(query_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (it == query_id_to_node_id_.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto node = nodes_container_.get(it->second);
|
|
|
|
if (node == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
send_closure(node->loader_, &FileLoaderActor::update_local_file_location, local);
|
|
|
|
}
|
2020-08-25 17:58:37 +02:00
|
|
|
|
2023-01-16 11:30:03 +01:00
|
|
|
void FileLoadManager::update_downloaded_part(QueryId query_id, int64 offset, int64 limit) {
|
2019-02-18 20:08:05 +01:00
|
|
|
if (stop_flag_) {
|
|
|
|
return;
|
|
|
|
}
|
2023-01-16 11:30:03 +01:00
|
|
|
auto it = query_id_to_node_id_.find(query_id);
|
2019-02-18 20:08:05 +01:00
|
|
|
if (it == query_id_to_node_id_.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto node = nodes_container_.get(it->second);
|
|
|
|
if (node == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2022-07-28 16:34:29 +02:00
|
|
|
send_closure(node->loader_, &FileLoaderActor::update_downloaded_part, offset, limit, max_download_resource_limit_);
|
2019-02-18 20:08:05 +01:00
|
|
|
}
|
2020-08-25 17:58:37 +02:00
|
|
|
|
2018-06-07 13:37:42 +02:00
|
|
|
void FileLoadManager::hangup() {
|
2023-01-16 11:30:03 +01:00
|
|
|
nodes_container_.for_each([](auto query_id, auto &node) { node.loader_.reset(); });
|
2018-12-31 20:04:05 +01:00
|
|
|
stop_flag_ = true;
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
2018-01-09 12:57:11 +01:00
|
|
|
void FileLoadManager::on_start_download() {
|
|
|
|
auto node_id = get_link_token();
|
|
|
|
auto node = nodes_container_.get(node_id);
|
|
|
|
if (node == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!stop_flag_) {
|
|
|
|
send_closure(callback_, &Callback::on_start_download, node->query_id_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-19 17:11:16 +02:00
|
|
|
void FileLoadManager::on_partial_download(PartialLocalFileLocation partial_local, int64 ready_size, int64 size) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto node_id = get_link_token();
|
|
|
|
auto node = nodes_container_.get(node_id);
|
|
|
|
if (node == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!stop_flag_) {
|
2021-10-19 17:11:16 +02:00
|
|
|
send_closure(callback_, &Callback::on_partial_download, node->query_id_, std::move(partial_local), ready_size,
|
|
|
|
size);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
2018-01-09 12:57:11 +01:00
|
|
|
|
2018-04-03 19:49:07 +02:00
|
|
|
void FileLoadManager::on_hash(string hash) {
|
|
|
|
auto node_id = get_link_token();
|
|
|
|
auto node = nodes_container_.get(node_id);
|
|
|
|
if (node == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!stop_flag_) {
|
|
|
|
send_closure(callback_, &Callback::on_hash, node->query_id_, std::move(hash));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-19 17:11:16 +02:00
|
|
|
void FileLoadManager::on_partial_upload(PartialRemoteFileLocation partial_remote, int64 ready_size) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto node_id = get_link_token();
|
|
|
|
auto node = nodes_container_.get(node_id);
|
|
|
|
if (node == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!stop_flag_) {
|
2021-10-19 17:11:16 +02:00
|
|
|
send_closure(callback_, &Callback::on_partial_upload, node->query_id_, std::move(partial_remote), ready_size);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
2018-01-09 12:57:11 +01:00
|
|
|
|
2021-10-19 17:11:16 +02:00
|
|
|
void FileLoadManager::on_ok_download(FullLocalFileLocation local, int64 size, bool is_new) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto node_id = get_link_token();
|
|
|
|
auto node = nodes_container_.get(node_id);
|
|
|
|
if (node == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!stop_flag_) {
|
2021-10-19 17:11:16 +02:00
|
|
|
send_closure(callback_, &Callback::on_download_ok, node->query_id_, std::move(local), size, is_new);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
close_node(node_id);
|
|
|
|
loop();
|
|
|
|
}
|
2018-01-09 12:57:11 +01:00
|
|
|
|
2021-10-19 17:11:16 +02:00
|
|
|
void FileLoadManager::on_ok_upload(FileType file_type, PartialRemoteFileLocation remote, int64 size) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto node_id = get_link_token();
|
|
|
|
auto node = nodes_container_.get(node_id);
|
|
|
|
if (node == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!stop_flag_) {
|
2021-10-19 17:11:16 +02:00
|
|
|
send_closure(callback_, &Callback::on_upload_ok, node->query_id_, file_type, std::move(remote), size);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
close_node(node_id);
|
|
|
|
loop();
|
|
|
|
}
|
2018-01-09 12:57:11 +01:00
|
|
|
|
2021-10-19 17:11:16 +02:00
|
|
|
void FileLoadManager::on_ok_upload_full(FullRemoteFileLocation remote) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto node_id = get_link_token();
|
|
|
|
auto node = nodes_container_.get(node_id);
|
|
|
|
if (node == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!stop_flag_) {
|
2021-10-19 17:11:16 +02:00
|
|
|
send_closure(callback_, &Callback::on_upload_full_ok, node->query_id_, std::move(remote));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
close_node(node_id);
|
|
|
|
loop();
|
|
|
|
}
|
2018-01-09 12:57:11 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void FileLoadManager::on_error(Status status) {
|
|
|
|
auto node_id = get_link_token();
|
|
|
|
on_error_impl(node_id, std::move(status));
|
|
|
|
}
|
2018-01-09 12:57:11 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void FileLoadManager::on_error_impl(NodeId node_id, Status status) {
|
|
|
|
auto node = nodes_container_.get(node_id);
|
|
|
|
if (node == nullptr) {
|
|
|
|
status.ignore();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!stop_flag_) {
|
|
|
|
send_closure(callback_, &Callback::on_error, node->query_id_, std::move(status));
|
|
|
|
}
|
|
|
|
close_node(node_id);
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileLoadManager::hangup_shared() {
|
|
|
|
auto node_id = get_link_token();
|
2022-09-20 01:02:28 +02:00
|
|
|
on_error_impl(node_id, Status::Error(-1, "Canceled"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileLoadManager::loop() {
|
2018-05-24 00:30:47 +02:00
|
|
|
if (stop_flag_ && nodes_container_.empty()) {
|
|
|
|
stop();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileLoadManager::close_node(NodeId node_id) {
|
|
|
|
auto node = nodes_container_.get(node_id);
|
|
|
|
CHECK(node);
|
|
|
|
query_id_to_node_id_.erase(node->query_id_);
|
|
|
|
nodes_container_.erase(node_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|