2022-03-02 12:25:52 +01:00
|
|
|
//
|
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
|
|
|
|
//
|
|
|
|
// 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/DownloadManagerCallback.h"
|
|
|
|
|
|
|
|
#include "td/telegram/FileReferenceManager.h"
|
|
|
|
#include "td/telegram/files/FileManager.h"
|
|
|
|
#include "td/telegram/Td.h"
|
|
|
|
|
|
|
|
#include "td/actor/actor.h"
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
2022-06-27 12:30:18 +02:00
|
|
|
#include "td/utils/Promise.h"
|
2022-03-02 12:25:52 +01:00
|
|
|
#include "td/utils/Status.h"
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
void DownloadManagerCallback::update_counters(DownloadManager::Counters counters) {
|
|
|
|
send_closure(td_->actor_id(td_), &Td::send_update, counters.get_update_file_downloads_object());
|
|
|
|
}
|
|
|
|
|
2022-03-05 01:14:31 +01:00
|
|
|
void DownloadManagerCallback::update_file_added(FileId file_id, FileSourceId file_source_id, int32 add_date,
|
2022-03-06 01:20:18 +01:00
|
|
|
int32 complete_date, bool is_paused,
|
|
|
|
DownloadManager::FileCounters counters) {
|
2022-03-05 01:14:31 +01:00
|
|
|
send_closure(td_->actor_id(td_), &Td::send_update,
|
|
|
|
td_api::make_object<td_api::updateFileAddedToDownloads>(
|
2022-03-06 01:20:18 +01:00
|
|
|
get_file_download_object(file_id, file_source_id, add_date, complete_date, is_paused),
|
|
|
|
counters.get_downloaded_file_counts_object()));
|
2022-03-05 01:14:31 +01:00
|
|
|
}
|
|
|
|
|
2022-03-06 01:20:18 +01:00
|
|
|
void DownloadManagerCallback::update_file_changed(FileId file_id, int32 complete_date, bool is_paused,
|
|
|
|
DownloadManager::FileCounters counters) {
|
2022-03-05 01:14:31 +01:00
|
|
|
send_closure(td_->actor_id(td_), &Td::send_update,
|
2022-03-06 01:20:18 +01:00
|
|
|
td_api::make_object<td_api::updateFileDownload>(file_id.get(), complete_date, is_paused,
|
|
|
|
counters.get_downloaded_file_counts_object()));
|
2022-03-05 01:14:31 +01:00
|
|
|
}
|
|
|
|
|
2022-03-06 01:20:18 +01:00
|
|
|
void DownloadManagerCallback::update_file_removed(FileId file_id, DownloadManager::FileCounters counters) {
|
2022-03-02 12:25:52 +01:00
|
|
|
send_closure(td_->actor_id(td_), &Td::send_update,
|
2022-03-06 01:20:18 +01:00
|
|
|
td_api::make_object<td_api::updateFileRemovedFromDownloads>(
|
|
|
|
file_id.get(), counters.get_downloaded_file_counts_object()));
|
2022-03-02 12:25:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadManagerCallback::start_file(FileId file_id, int8 priority, ActorShared<DownloadManager> download_manager) {
|
2022-03-06 00:50:57 +01:00
|
|
|
send_closure_later(td_->file_manager_actor_, &FileManager::download, file_id,
|
|
|
|
make_download_file_callback(td_, std::move(download_manager)), priority,
|
2022-10-10 14:40:39 +02:00
|
|
|
FileManager::KEEP_DOWNLOAD_OFFSET, FileManager::IGNORE_DOWNLOAD_LIMIT,
|
|
|
|
Promise<td_api::object_ptr<td_api::file>>());
|
2022-03-02 12:25:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadManagerCallback::pause_file(FileId file_id) {
|
2022-03-06 00:50:57 +01:00
|
|
|
send_closure_later(td_->file_manager_actor_, &FileManager::download, file_id, nullptr, 0,
|
2022-10-10 14:40:39 +02:00
|
|
|
FileManager::KEEP_DOWNLOAD_OFFSET, FileManager::KEEP_DOWNLOAD_LIMIT,
|
|
|
|
Promise<td_api::object_ptr<td_api::file>>());
|
2022-03-02 12:25:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadManagerCallback::delete_file(FileId file_id) {
|
2022-03-06 00:50:57 +01:00
|
|
|
send_closure_later(td_->file_manager_actor_, &FileManager::delete_file, file_id, Promise<Unit>(),
|
|
|
|
"download manager callback");
|
2022-03-02 12:25:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
FileId DownloadManagerCallback::dup_file_id(FileId file_id) {
|
|
|
|
return td_->file_manager_->dup_file_id(file_id);
|
|
|
|
}
|
|
|
|
|
2022-07-23 16:01:27 +02:00
|
|
|
void DownloadManagerCallback::get_file_search_text(FileId file_id, FileSourceId file_source_id,
|
|
|
|
Promise<string> &&promise) {
|
|
|
|
send_closure(td_->file_reference_manager_actor_, &FileReferenceManager::get_file_search_text, file_source_id,
|
|
|
|
get_file_view(file_id).get_unique_file_id(), std::move(promise));
|
|
|
|
}
|
|
|
|
|
2022-03-02 12:25:52 +01:00
|
|
|
FileView DownloadManagerCallback::get_file_view(FileId file_id) {
|
|
|
|
return td_->file_manager_->get_file_view(file_id);
|
|
|
|
}
|
|
|
|
|
2022-03-03 19:11:34 +01:00
|
|
|
FileView DownloadManagerCallback::get_sync_file_view(FileId file_id) {
|
2022-10-09 20:20:57 +02:00
|
|
|
td_->file_manager_->check_local_location(file_id, true);
|
2022-07-20 15:46:55 +02:00
|
|
|
return get_file_view(file_id);
|
2022-03-03 19:11:34 +01:00
|
|
|
}
|
|
|
|
|
2022-07-23 16:11:25 +02:00
|
|
|
td_api::object_ptr<td_api::file> DownloadManagerCallback::get_file_object(FileId file_id) {
|
|
|
|
return td_->file_manager_->get_file_object(file_id);
|
|
|
|
}
|
|
|
|
|
2022-03-02 12:25:52 +01:00
|
|
|
td_api::object_ptr<td_api::fileDownload> DownloadManagerCallback::get_file_download_object(
|
|
|
|
FileId file_id, FileSourceId file_source_id, int32 add_date, int32 complete_date, bool is_paused) {
|
2022-07-03 11:01:08 +02:00
|
|
|
return td_api::make_object<td_api::fileDownload>(td_->file_manager_->get_file_view(file_id).get_main_file_id().get(),
|
2022-03-02 12:25:52 +01:00
|
|
|
td_->file_reference_manager_->get_message_object(file_source_id),
|
|
|
|
add_date, complete_date, is_paused);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<FileManager::DownloadCallback> DownloadManagerCallback::make_download_file_callback(
|
|
|
|
Td *td, ActorShared<DownloadManager> download_manager) {
|
|
|
|
class Impl final : public FileManager::DownloadCallback {
|
|
|
|
public:
|
|
|
|
Impl(Td *td, ActorShared<DownloadManager> download_manager)
|
|
|
|
: td_(td), download_manager_(std::move(download_manager)) {
|
|
|
|
}
|
|
|
|
void on_progress(FileId file_id) final {
|
|
|
|
send_update(file_id, false);
|
|
|
|
}
|
|
|
|
void on_download_ok(FileId file_id) final {
|
2022-03-05 01:14:31 +01:00
|
|
|
send_update(file_id, false);
|
2022-03-02 12:25:52 +01:00
|
|
|
}
|
|
|
|
void on_download_error(FileId file_id, Status error) final {
|
|
|
|
send_update(file_id, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Td *td_;
|
|
|
|
ActorShared<DownloadManager> download_manager_;
|
|
|
|
|
|
|
|
void send_update(FileId file_id, bool is_paused) const {
|
2022-03-09 01:27:03 +01:00
|
|
|
auto file_view = td_->file_manager_->get_file_view(file_id);
|
2022-03-06 00:50:57 +01:00
|
|
|
send_closure_later(download_manager_, &DownloadManager::update_file_download_state, file_id,
|
|
|
|
file_view.local_total_size(), file_view.size(), file_view.expected_size(), is_paused);
|
2022-03-02 12:25:52 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
return std::make_shared<Impl>(td, std::move(download_manager));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|