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)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/telegram/files/FileDownloader.h"
|
2018-12-27 20:24:44 +01:00
|
|
|
#include "td/telegram/files/FileEncryptionKey.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/files/FileFromBytes.h"
|
|
|
|
#include "td/telegram/files/FileHashUploader.h"
|
2022-10-10 13:19:35 +02:00
|
|
|
#include "td/telegram/files/FileLoaderUtils.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/files/FileLocation.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/files/FileUploader.h"
|
|
|
|
#include "td/telegram/files/ResourceManager.h"
|
2018-06-26 01:43:11 +02:00
|
|
|
#include "td/telegram/net/DcId.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-09-18 23:47:05 +02:00
|
|
|
#include "td/actor/actor.h"
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/buffer.h"
|
2022-06-21 01:55:53 +02:00
|
|
|
#include "td/utils/common.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Container.h"
|
2022-06-27 12:30:18 +02:00
|
|
|
#include "td/utils/Promise.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Status.h"
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
class FileLoadManager final : public Actor {
|
|
|
|
public:
|
|
|
|
using QueryId = uint64;
|
|
|
|
class Callback : public Actor {
|
|
|
|
public:
|
2018-01-09 12:57:11 +01:00
|
|
|
virtual void on_start_download(QueryId id) = 0;
|
2021-10-19 17:11:16 +02:00
|
|
|
virtual void on_partial_download(QueryId id, PartialLocalFileLocation partial_local, int64 ready_size,
|
2018-12-26 22:42:26 +01:00
|
|
|
int64 size) = 0;
|
2021-10-19 17:11:16 +02:00
|
|
|
virtual void on_partial_upload(QueryId id, PartialRemoteFileLocation partial_remote, int64 ready_size) = 0;
|
2018-04-03 19:49:07 +02:00
|
|
|
virtual void on_hash(QueryId id, string hash) = 0;
|
2021-10-19 17:11:16 +02:00
|
|
|
virtual void on_upload_ok(QueryId id, FileType file_type, PartialRemoteFileLocation remtoe, int64 size) = 0;
|
|
|
|
virtual void on_upload_full_ok(QueryId id, FullRemoteFileLocation remote) = 0;
|
|
|
|
virtual void on_download_ok(QueryId id, FullLocalFileLocation local, int64 size, bool is_new) = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
virtual void on_error(QueryId id, Status status) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
explicit FileLoadManager(ActorShared<Callback> callback, ActorShared<> parent);
|
2021-10-19 17:11:16 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void download(QueryId id, const FullRemoteFileLocation &remote_location, const LocalFileLocation &local, int64 size,
|
2019-02-18 20:08:05 +01:00
|
|
|
string name, const FileEncryptionKey &encryption_key, bool search_file, int64 offset, int64 limit,
|
|
|
|
int8 priority);
|
2018-12-31 20:04:05 +01:00
|
|
|
void upload(QueryId id, const LocalFileLocation &local_location, const RemoteFileLocation &remote_location,
|
2018-12-27 09:34:36 +01:00
|
|
|
int64 expected_size, const FileEncryptionKey &encryption_key, int8 priority, vector<int> bad_parts);
|
2018-01-20 12:47:53 +01:00
|
|
|
void upload_by_hash(QueryId id, const FullLocalFileLocation &local_location, int64 size, int8 priority);
|
|
|
|
void update_priority(QueryId id, int8 priority);
|
2018-12-31 20:04:05 +01:00
|
|
|
void from_bytes(QueryId id, FileType type, BufferSlice bytes, string name);
|
|
|
|
void cancel(QueryId id);
|
|
|
|
void update_local_file_location(QueryId id, const LocalFileLocation &local);
|
2020-08-25 17:58:37 +02:00
|
|
|
void update_downloaded_part(QueryId id, int64 offset, int64 limit);
|
2021-10-19 17:11:16 +02:00
|
|
|
|
2022-06-15 20:40:23 +02:00
|
|
|
void get_content(string file_path, Promise<BufferSlice> promise);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-06-15 23:25:47 +02:00
|
|
|
void read_file_part(string file_path, int64 offset, int64 count, Promise<string> promise);
|
|
|
|
|
2022-06-16 12:32:14 +02:00
|
|
|
void unlink_file(string file_path, Promise<Unit> promise);
|
|
|
|
|
2022-10-10 13:19:35 +02:00
|
|
|
void check_full_local_location(FullLocalLocationInfo local_info, bool skip_file_size_checks,
|
|
|
|
Promise<FullLocalLocationInfo> promise);
|
|
|
|
|
|
|
|
void check_partial_local_location(PartialLocalFileLocation partial, Promise<Unit> promise);
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
private:
|
|
|
|
struct Node {
|
|
|
|
QueryId query_id_;
|
|
|
|
ActorOwn<FileLoaderActor> loader_;
|
|
|
|
ResourceState resource_state_;
|
|
|
|
};
|
|
|
|
using NodeId = uint64;
|
|
|
|
|
2018-02-27 20:47:14 +01:00
|
|
|
std::map<DcId, ActorOwn<ResourceManager>> download_resource_manager_map_;
|
|
|
|
std::map<DcId, ActorOwn<ResourceManager>> download_small_resource_manager_map_;
|
2018-12-31 20:04:05 +01:00
|
|
|
ActorOwn<ResourceManager> upload_resource_manager_;
|
|
|
|
|
|
|
|
Container<Node> nodes_container_;
|
|
|
|
ActorShared<Callback> callback_;
|
|
|
|
ActorShared<> parent_;
|
|
|
|
std::map<QueryId, NodeId> query_id_to_node_id_;
|
2022-07-28 16:34:29 +02:00
|
|
|
int64 max_download_resource_limit_ = 1 << 21;
|
2018-12-31 20:04:05 +01:00
|
|
|
bool stop_flag_ = false;
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final;
|
|
|
|
void loop() final;
|
|
|
|
void hangup() final;
|
|
|
|
void hangup_shared() final;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
void close_node(NodeId node_id);
|
2018-02-27 20:47:14 +01:00
|
|
|
ActorOwn<ResourceManager> &get_download_resource_manager(bool is_small, DcId dc_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-01-09 12:57:11 +01:00
|
|
|
void on_start_download();
|
2021-10-19 17:11:16 +02:00
|
|
|
void on_partial_download(PartialLocalFileLocation partial_local, int64 ready_size, int64 size);
|
|
|
|
void on_partial_upload(PartialRemoteFileLocation partial_remote, int64 ready_size);
|
2018-04-03 19:49:07 +02:00
|
|
|
void on_hash(string hash);
|
2021-10-19 17:11:16 +02:00
|
|
|
void on_ok_download(FullLocalFileLocation local, int64 size, bool is_new);
|
|
|
|
void on_ok_upload(FileType file_type, PartialRemoteFileLocation remote, int64 size);
|
|
|
|
void on_ok_upload_full(FullRemoteFileLocation remote);
|
2018-12-31 20:04:05 +01:00
|
|
|
void on_error(Status status);
|
|
|
|
void on_error_impl(NodeId node_id, Status status);
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class FileDownloaderCallback final : public FileDownloader::Callback {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
explicit FileDownloaderCallback(ActorShared<FileLoadManager> actor_id) : actor_id_(std::move(actor_id)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ActorShared<FileLoadManager> actor_id_;
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_start_download() final {
|
2018-01-09 12:57:11 +01:00
|
|
|
send_closure(actor_id_, &FileLoadManager::on_start_download);
|
|
|
|
}
|
2021-10-19 17:11:16 +02:00
|
|
|
void on_partial_download(PartialLocalFileLocation partial_local, int64 ready_size, int64 size) final {
|
|
|
|
send_closure(actor_id_, &FileLoadManager::on_partial_download, std::move(partial_local), ready_size, size);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-10-19 17:11:16 +02:00
|
|
|
void on_ok(FullLocalFileLocation full_local, int64 size, bool is_new) final {
|
|
|
|
send_closure(std::move(actor_id_), &FileLoadManager::on_ok_download, std::move(full_local), size, is_new);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_error(Status status) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure(std::move(actor_id_), &FileLoadManager::on_error, std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class FileUploaderCallback final : public FileUploader::Callback {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
explicit FileUploaderCallback(ActorShared<FileLoadManager> actor_id) : actor_id_(std::move(actor_id)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ActorShared<FileLoadManager> actor_id_;
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_hash(string hash) final {
|
2018-04-03 19:49:07 +02:00
|
|
|
send_closure(actor_id_, &FileLoadManager::on_hash, std::move(hash));
|
|
|
|
}
|
2021-10-19 17:11:16 +02:00
|
|
|
void on_partial_upload(PartialRemoteFileLocation partial_remote, int64 ready_size) final {
|
|
|
|
send_closure(actor_id_, &FileLoadManager::on_partial_upload, std::move(partial_remote), ready_size);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-10-19 17:11:16 +02:00
|
|
|
void on_ok(FileType file_type, PartialRemoteFileLocation partial_remote, int64 size) final {
|
|
|
|
send_closure(std::move(actor_id_), &FileLoadManager::on_ok_upload, file_type, std::move(partial_remote), size);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_error(Status status) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure(std::move(actor_id_), &FileLoadManager::on_error, std::move(status));
|
|
|
|
}
|
|
|
|
};
|
2021-07-04 04:58:54 +02:00
|
|
|
class FileHashUploaderCallback final : public FileHashUploader::Callback {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
explicit FileHashUploaderCallback(ActorShared<FileLoadManager> actor_id) : actor_id_(std::move(actor_id)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ActorShared<FileLoadManager> actor_id_;
|
|
|
|
|
2021-10-19 17:11:16 +02:00
|
|
|
void on_ok(FullRemoteFileLocation remote) final {
|
|
|
|
send_closure(std::move(actor_id_), &FileLoadManager::on_ok_upload_full, std::move(remote));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_error(Status status) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure(std::move(actor_id_), &FileLoadManager::on_error, std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class FileFromBytesCallback final : public FileFromBytes::Callback {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
explicit FileFromBytesCallback(ActorShared<FileLoadManager> actor_id) : actor_id_(std::move(actor_id)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ActorShared<FileLoadManager> actor_id_;
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_ok(const FullLocalFileLocation &full_local, int64 size) final {
|
2019-01-11 18:08:56 +01:00
|
|
|
send_closure(std::move(actor_id_), &FileLoadManager::on_ok_download, full_local, size, true);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_error(Status status) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure(std::move(actor_id_), &FileLoadManager::on_error, std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace td
|