2018-12-31 20:04:05 +01:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
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
|
|
|
|
|
2018-12-27 20:24:44 +01:00
|
|
|
#include "td/telegram/files/FileEncryptionKey.h"
|
2024-07-12 23:59:24 +02:00
|
|
|
#include "td/telegram/files/FileLoaderActor.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"
|
2024-07-12 23:59:24 +02:00
|
|
|
#include "td/telegram/files/PartsManager.h"
|
|
|
|
#include "td/telegram/files/ResourceManager.h"
|
|
|
|
#include "td/telegram/files/ResourceState.h"
|
|
|
|
#include "td/telegram/net/NetQuery.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2024-07-25 13:51:50 +02:00
|
|
|
#include "td/actor/actor.h"
|
|
|
|
|
2023-08-05 15:09:36 +02:00
|
|
|
#include "td/utils/common.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/port/FileFd.h"
|
|
|
|
#include "td/utils/Status.h"
|
2018-12-19 22:18:53 +01:00
|
|
|
#include "td/utils/UInt.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2024-07-12 23:59:24 +02:00
|
|
|
#include <map>
|
2018-12-31 20:04:05 +01:00
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace td {
|
2018-12-26 17:11:15 +01:00
|
|
|
|
2024-07-12 23:59:24 +02:00
|
|
|
class FileUploader final : public FileLoaderActor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2024-07-12 14:38:10 +02:00
|
|
|
class Callback {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2024-07-12 14:38:10 +02:00
|
|
|
Callback() = default;
|
|
|
|
Callback(const Callback &) = delete;
|
|
|
|
Callback &operator=(const Callback &) = delete;
|
2018-04-03 19:49:07 +02:00
|
|
|
virtual void on_hash(string hash) = 0;
|
2021-10-19 17:11:16 +02:00
|
|
|
virtual void on_partial_upload(PartialRemoteFileLocation partial_remote, int64 ready_size) = 0;
|
|
|
|
virtual void on_ok(FileType file_type, PartialRemoteFileLocation partial_remote, int64 size) = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
virtual void on_error(Status status) = 0;
|
2024-07-12 14:38:10 +02:00
|
|
|
virtual ~Callback() = default;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
FileUploader(const LocalFileLocation &local, const RemoteFileLocation &remote, int64 expected_size,
|
2018-09-27 03:19:03 +02:00
|
|
|
const FileEncryptionKey &encryption_key, std::vector<int> bad_parts, unique_ptr<Callback> callback);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2024-07-15 13:44:36 +02:00
|
|
|
void update_local_file_location(const LocalFileLocation &local);
|
2024-07-12 23:59:24 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
private:
|
|
|
|
LocalFileLocation local_;
|
|
|
|
RemoteFileLocation remote_;
|
|
|
|
int64 expected_size_;
|
|
|
|
FileEncryptionKey encryption_key_;
|
2024-07-12 23:59:24 +02:00
|
|
|
vector<int> bad_parts_;
|
2018-09-27 03:19:03 +02:00
|
|
|
unique_ptr<Callback> callback_;
|
2018-12-31 20:04:05 +01:00
|
|
|
int64 local_size_ = 0;
|
|
|
|
bool local_is_ready_ = false;
|
|
|
|
FileType file_type_ = FileType::Temp;
|
|
|
|
|
2024-07-12 23:59:24 +02:00
|
|
|
vector<UInt256> iv_map_;
|
2021-12-01 14:01:27 +01:00
|
|
|
UInt256 iv_;
|
2018-12-31 20:04:05 +01:00
|
|
|
string generate_iv_;
|
|
|
|
int64 generate_offset_ = 0;
|
|
|
|
int64 next_offset_ = 0;
|
|
|
|
|
|
|
|
FileFd fd_;
|
|
|
|
string fd_path_;
|
2021-11-11 15:39:09 +01:00
|
|
|
int64 file_id_ = 0;
|
2024-07-12 23:59:24 +02:00
|
|
|
bool is_temp_ = false;
|
2021-11-11 15:39:09 +01:00
|
|
|
bool big_flag_ = false;
|
2024-07-12 23:59:24 +02:00
|
|
|
bool keep_fd_ = false;
|
|
|
|
bool stop_flag_ = false;
|
|
|
|
|
|
|
|
ActorShared<ResourceManager> resource_manager_;
|
|
|
|
ResourceState resource_state_;
|
|
|
|
PartsManager parts_manager_;
|
|
|
|
std::map<uint64, std::pair<Part, ActorShared<>>> part_map_;
|
|
|
|
|
2024-07-16 12:58:48 +02:00
|
|
|
void set_resource_manager(ActorShared<ResourceManager> resource_manager) final;
|
|
|
|
|
|
|
|
void update_priority(int8 priority) final;
|
|
|
|
|
|
|
|
void update_resources(const ResourceState &other) final;
|
|
|
|
|
2024-07-12 23:59:24 +02:00
|
|
|
void on_error(Status status);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2024-07-12 23:59:24 +02:00
|
|
|
Result<NetQueryPtr> start_part(Part part, int32 part_count) TD_WARN_UNUSED_RESULT;
|
|
|
|
|
|
|
|
Result<size_t> process_part(Part part, NetQueryPtr net_query) TD_WARN_UNUSED_RESULT;
|
|
|
|
|
|
|
|
void on_progress();
|
|
|
|
|
|
|
|
struct PrefixInfo {
|
|
|
|
int64 size = -1;
|
|
|
|
bool is_ready = false;
|
|
|
|
};
|
|
|
|
Result<PrefixInfo> on_update_local_location(const LocalFileLocation &location, int64 file_size) TD_WARN_UNUSED_RESULT;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
Status generate_iv_map();
|
|
|
|
|
|
|
|
void try_release_fd();
|
2024-07-12 23:59:24 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Status acquire_fd() TD_WARN_UNUSED_RESULT;
|
2024-07-12 23:59:24 +02:00
|
|
|
|
|
|
|
void start_up() final;
|
|
|
|
|
|
|
|
void loop() final;
|
|
|
|
|
|
|
|
Status do_loop();
|
|
|
|
|
|
|
|
void tear_down() final;
|
|
|
|
|
|
|
|
void update_estimated_limit();
|
|
|
|
|
|
|
|
void on_result(NetQueryPtr query) final;
|
|
|
|
|
|
|
|
void on_part_query(Part part, NetQueryPtr query);
|
|
|
|
|
|
|
|
void on_common_query(NetQueryPtr query);
|
|
|
|
|
|
|
|
Status try_on_part_query(Part part, NetQueryPtr query);
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
2018-12-26 17:11:15 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|