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"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/files/FileLoader.h"
|
|
|
|
#include "td/telegram/files/FileLocation.h"
|
2018-06-26 01:43:11 +02:00
|
|
|
#include "td/telegram/net/DcId.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/net/NetQuery.h"
|
2021-10-27 16:32:09 +02:00
|
|
|
#include "td/telegram/telegram_api.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
#include "td/utils/port/FileFd.h"
|
|
|
|
#include "td/utils/Status.h"
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <set>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace td {
|
2024-07-12 23:48:47 +02:00
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class FileDownloader final : public FileLoader {
|
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-01-09 12:57:11 +01:00
|
|
|
virtual void on_start_download() = 0;
|
2021-10-19 17:11:16 +02:00
|
|
|
virtual void on_partial_download(PartialLocalFileLocation partial_local, int64 ready_size, int64 size) = 0;
|
|
|
|
virtual void on_ok(FullLocalFileLocation full_local, int64 size, bool is_new) = 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
|
|
|
};
|
|
|
|
|
|
|
|
FileDownloader(const FullRemoteFileLocation &remote, const LocalFileLocation &local, int64 size, string name,
|
2022-10-12 14:59:58 +02:00
|
|
|
const FileEncryptionKey &encryption_key, bool is_small, bool need_search_file, int64 offset,
|
|
|
|
int64 limit, unique_ptr<Callback> callback);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
// Should just implement all parent pure virtual methods.
|
|
|
|
// Must not call any of them...
|
|
|
|
private:
|
|
|
|
enum class QueryType : uint8 { Default = 1, CDN, ReuploadCDN };
|
|
|
|
FullRemoteFileLocation remote_;
|
|
|
|
LocalFileLocation local_;
|
|
|
|
int64 size_;
|
|
|
|
string name_;
|
|
|
|
FileEncryptionKey encryption_key_;
|
2018-09-27 03:19:03 +02:00
|
|
|
unique_ptr<Callback> callback_;
|
2018-02-26 11:05:14 +01:00
|
|
|
bool only_check_{false};
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
string path_;
|
|
|
|
FileFd fd_;
|
|
|
|
|
|
|
|
int32 next_part_ = 0;
|
|
|
|
bool next_part_stop_ = false;
|
|
|
|
bool is_small_;
|
2022-10-12 14:59:58 +02:00
|
|
|
bool need_search_file_{false};
|
2018-11-11 12:38:04 +01:00
|
|
|
int64 offset_;
|
2019-02-18 20:08:05 +01:00
|
|
|
int64 limit_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
bool use_cdn_ = false;
|
|
|
|
DcId cdn_dc_id_;
|
|
|
|
string cdn_encryption_key_;
|
|
|
|
string cdn_encryption_iv_;
|
|
|
|
string cdn_file_token_;
|
|
|
|
int32 cdn_file_token_generation_{0};
|
|
|
|
std::map<int32, string> cdn_part_reupload_token_;
|
|
|
|
std::map<int32, int32> cdn_part_file_token_generation_;
|
|
|
|
|
|
|
|
bool need_check_{false};
|
|
|
|
struct HashInfo {
|
|
|
|
int64 offset;
|
|
|
|
size_t size;
|
|
|
|
string hash;
|
|
|
|
bool operator<(const HashInfo &other) const {
|
|
|
|
return offset < other.offset;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
std::set<HashInfo> hash_info_;
|
|
|
|
bool has_hash_query_ = false;
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
Result<FileInfo> init() final TD_WARN_UNUSED_RESULT;
|
|
|
|
Status on_ok(int64 size) final TD_WARN_UNUSED_RESULT;
|
|
|
|
void on_error(Status status) final;
|
2023-09-07 16:31:08 +02:00
|
|
|
Result<bool> should_restart_part(Part part, const NetQueryPtr &net_query) final TD_WARN_UNUSED_RESULT;
|
2024-07-12 23:48:47 +02:00
|
|
|
Result<NetQueryPtr> start_part(Part part, int32 part_count, int64 streaming_offset) final TD_WARN_UNUSED_RESULT;
|
2021-07-03 22:51:36 +02:00
|
|
|
Result<size_t> process_part(Part part, NetQueryPtr net_query) final TD_WARN_UNUSED_RESULT;
|
|
|
|
void on_progress(Progress progress) final;
|
|
|
|
Status process_check_query(NetQueryPtr net_query) final;
|
|
|
|
Result<CheckInfo> check_loop(int64 checked_prefix_size, int64 ready_prefix_size, bool is_ready) final;
|
2018-02-25 19:56:16 +01:00
|
|
|
void add_hash_info(const std::vector<telegram_api::object_ptr<telegram_api::fileHash>> &hashes);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
bool keep_fd_ = false;
|
2021-07-03 22:51:36 +02:00
|
|
|
void keep_fd_flag(bool keep_fd) final;
|
2018-12-31 20:04:05 +01:00
|
|
|
void try_release_fd();
|
|
|
|
Status acquire_fd() TD_WARN_UNUSED_RESULT;
|
2018-12-05 10:32:31 +01:00
|
|
|
|
|
|
|
Status check_net_query(NetQueryPtr &net_query);
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
2024-07-12 23:48:47 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|