From f1cf7bdc3fb7441c59775503fe7034fd5a067c34 Mon Sep 17 00:00:00 2001 From: Arseny Smirnov Date: Thu, 3 Mar 2022 16:58:51 +0100 Subject: [PATCH] DownloadManager: remove file when local file is removed --- td/telegram/DownloadManager.cpp | 29 +++++++++++++++++------------ td/telegram/DownloadManager.h | 1 + td/telegram/Global.h | 9 +++++++++ td/telegram/Td.cpp | 1 + td/telegram/files/FileManager.cpp | 4 ++++ 5 files changed, 32 insertions(+), 12 deletions(-) diff --git a/td/telegram/DownloadManager.cpp b/td/telegram/DownloadManager.cpp index 76ea19233..4adef5b6e 100644 --- a/td/telegram/DownloadManager.cpp +++ b/td/telegram/DownloadManager.cpp @@ -125,6 +125,23 @@ class DownloadManagerImpl final : public DownloadManager { return Status::OK(); } + Status remove_file_if_finished(FileId file_id) { + TRY_STATUS(check_is_active()); + TRY_RESULT(file_info_ptr, get_file_info(file_id, {})); + if (!is_completed(*file_info_ptr)) { + return Status::Error("File is active"); + } + return remove_file(file_id, {}, false); + } + + Status change_search_text(FileId file_id, FileSourceId file_source_id, string search_text) final { + TRY_STATUS(check_is_active()); + TRY_RESULT(file_info_ptr, get_file_info(file_id, file_source_id)); + auto &file_info = *file_info_ptr; + hints_.add(file_info.download_id, search_text.empty() ? string(" ") : search_text); + return Status::OK(); + } + Status remove_all_files(bool only_active, bool only_completed, bool delete_from_cache) final { TRY_STATUS(check_is_active()); vector to_remove; @@ -165,18 +182,6 @@ class DownloadManagerImpl final : public DownloadManager { return Status::OK(); } - Status change_search_text(FileId file_id, FileSourceId file_source_id, string search_text) final { - if (!is_search_inited_) { - return Status::OK(); - } - - TRY_STATUS(check_is_active()); - TRY_RESULT(file_info_ptr, get_file_info(file_id, file_source_id)); - auto &file_info = *file_info_ptr; - hints_.add(file_info.download_id, search_text.empty() ? string(" ") : search_text); - return Status::OK(); - } - void hints_synchronized(Result) { if (G()->close_flag()) { return; diff --git a/td/telegram/DownloadManager.h b/td/telegram/DownloadManager.h index 5257085cd..61e44fcdf 100644 --- a/td/telegram/DownloadManager.h +++ b/td/telegram/DownloadManager.h @@ -71,6 +71,7 @@ class DownloadManager : public Actor { virtual void search(string query, bool only_active, bool only_completed, string offset, int32 limit, Promise> promise) = 0; virtual Status remove_file(FileId file_id, FileSourceId file_source_id, bool delete_from_cache) = 0; + virtual Status remove_file_if_finished(FileId file_id) = 0; virtual Status remove_all_files(bool only_active, bool only_completed, bool delete_from_cache) = 0; // diff --git a/td/telegram/Global.h b/td/telegram/Global.h index 0beb72821..03c409650 100644 --- a/td/telegram/Global.h +++ b/td/telegram/Global.h @@ -36,6 +36,7 @@ class ConfigManager; class ConfigShared; class ConnectionCreator; class ContactsManager; +class DownloadManager; class FileManager; class FileReferenceManager; class GameManager; @@ -205,6 +206,13 @@ class Global final : public ActorContext { contacts_manager_ = contacts_manager; } + ActorId download_manager() const { + return download_manager_; + } + void set_download_manager(ActorId download_manager) { + download_manager_ = std::move(download_manager); + } + ActorId file_manager() const { return file_manager_; } @@ -437,6 +445,7 @@ class Global final : public ActorContext { ActorId call_manager_; ActorId config_manager_; ActorId contacts_manager_; + ActorId download_manager_; ActorId file_manager_; ActorId file_reference_manager_; ActorId game_manager_; diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index d2f4bbda7..8a09dd621 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -3973,6 +3973,7 @@ void Td::init_managers() { country_info_manager_actor_ = register_actor("CountryInfoManager", country_info_manager_.get()); download_manager_ = DownloadManager::create(td::make_unique(this, create_reference())); download_manager_actor_ = register_actor("DownloadManager", download_manager_.get()); + G()->set_download_manager(download_manager_actor_.get()); game_manager_ = make_unique(this, create_reference()); game_manager_actor_ = register_actor("GameManager", game_manager_.get()); G()->set_game_manager(game_manager_actor_.get()); diff --git a/td/telegram/files/FileManager.cpp b/td/telegram/files/FileManager.cpp index 0fc3d0bb1..a8663a262 100644 --- a/td/telegram/files/FileManager.cpp +++ b/td/telegram/files/FileManager.cpp @@ -7,6 +7,7 @@ #include "td/telegram/files/FileManager.h" #include "td/telegram/ConfigShared.h" +#include "td/telegram/DownloadManager.h" #include "td/telegram/FileReferenceManager.h" #include "td/telegram/files/FileData.h" #include "td/telegram/files/FileDb.h" @@ -1028,6 +1029,7 @@ Status FileManager::check_local_location(FileNodePtr node) { status = check_partial_local_location(node->local_.partial()); } if (status.is_error()) { + send_closure(G()->download_manager(), &DownloadManager::remove_file_if_finished, node->main_file_id_); node->drop_local_location(); try_flush_node(node, "check_local_location"); } @@ -1118,6 +1120,7 @@ void FileManager::on_file_unlink(const FullLocalFileLocation &location) { auto file_id = it->second; auto file_node = get_sync_file_node(file_id); CHECK(file_node); + send_closure(G()->download_manager(), &DownloadManager::remove_file, file_node->main_file_id_, FileSourceId{}, false); file_node->drop_local_location(); try_flush_node_info(file_node, "on_file_unlink"); } @@ -2143,6 +2146,7 @@ void FileManager::delete_file(FileId file_id, Promise promise, const char auto file_view = FileView(node); + send_closure(G()->download_manager(), &DownloadManager::remove_file, file_view.file_id(), FileSourceId{}, false); // TODO review delete condition if (file_view.has_local_location()) { if (begins_with(file_view.local_location().path_, get_files_dir(file_view.get_type()))) {