Remove checks out of DownloadManager::remove_file_impl.

This commit is contained in:
levlam 2023-09-21 14:37:09 +03:00
parent 782fc0964f
commit d0d8d74c3e

View File

@ -110,7 +110,10 @@ class DownloadManagerImpl final : public DownloadManager {
}
void remove_file(FileId file_id, FileSourceId file_source_id, bool delete_from_cache, Promise<Unit> promise) final {
promise.set_result(remove_file_impl(file_id, file_source_id, delete_from_cache, "remove_file"));
TRY_STATUS_PROMISE(promise, check_is_active("remove_file"));
TRY_RESULT_PROMISE(promise, file_info_ptr, get_file_info_ptr(file_id, file_source_id));
remove_file_impl(*file_info_ptr, delete_from_cache, "remove_file");
promise.set_value(Unit());
}
void remove_file_if_finished(FileId file_id) final {
@ -119,7 +122,7 @@ class DownloadManagerImpl final : public DownloadManager {
void remove_all_files(bool only_active, bool only_completed, bool delete_from_cache, Promise<Unit> promise) final {
TRY_STATUS_PROMISE(promise, check_is_active("remove_all_files"));
vector<FileId> to_remove;
vector<const FileInfo *> to_remove;
for (auto &it : files_) {
FileInfo &file_info = *it.second;
if (only_active && is_completed(file_info)) {
@ -128,10 +131,10 @@ class DownloadManagerImpl final : public DownloadManager {
if (only_completed && !is_completed(file_info)) {
continue;
}
to_remove.push_back(file_info.file_id);
to_remove.push_back(&file_info);
}
for (auto file_id : to_remove) {
remove_file_impl(file_id, {}, delete_from_cache, "remove_all_files");
for (auto file_info_ptr : to_remove) {
remove_file_impl(*file_info_ptr, delete_from_cache, "remove_all_files");
}
promise.set_value(Unit());
}
@ -140,7 +143,10 @@ class DownloadManagerImpl final : public DownloadManager {
Promise<td_api::object_ptr<td_api::file>> promise) final {
TRY_STATUS_PROMISE(promise, check_is_active("add_file"));
remove_file_impl(file_id, {}, false, "add_file");
auto old_file_info_ptr = get_file_info_ptr(file_id);
if (old_file_info_ptr.is_ok()) {
remove_file_impl(*old_file_info_ptr.ok(), false, "add_file");
}
auto download_id = next_download_id();
@ -503,8 +509,8 @@ class DownloadManagerImpl final : public DownloadManager {
}
if (r_search_text.is_error()) {
if (!G()->close_flag()) {
remove_file_impl(it->second->file_id, {}, false, "add_download_to_hints");
if (!G()->close_flag() && check_is_active("add_download_to_hints").is_ok()) {
remove_file_impl(*it->second, false, "add_download_to_hints");
}
} else {
auto search_text = r_search_text.move_as_ok();
@ -559,11 +565,9 @@ class DownloadManagerImpl final : public DownloadManager {
}
}
Status remove_file_impl(FileId file_id, FileSourceId file_source_id, bool delete_from_cache, const char *source) {
LOG(INFO) << "Remove from downloads file " << file_id << " from " << file_source_id;
TRY_STATUS(check_is_active(source));
TRY_RESULT(file_info_ptr, get_file_info_ptr(file_id, file_source_id));
auto &file_info = *file_info_ptr;
void remove_file_impl(const FileInfo &file_info, bool delete_from_cache, const char *source) {
auto file_id = file_info.file_id;
LOG(INFO) << "Remove from downloads file " << file_id << " from " << source;
auto download_id = file_info.download_id;
if (!is_completed(file_info) && !file_info.is_paused) {
callback_->pause_file(file_info.internal_file_id);
@ -573,7 +577,7 @@ class DownloadManagerImpl final : public DownloadManager {
callback_->delete_file(file_info.internal_file_id);
}
by_internal_file_id_.erase(file_info.internal_file_id);
by_file_id_.erase(file_info.file_id);
by_file_id_.erase(file_id);
hints_.remove(download_id);
completed_download_ids_.erase(download_id);
@ -585,8 +589,6 @@ class DownloadManagerImpl final : public DownloadManager {
update_counters();
on_file_viewed(download_id);
return Status::OK();
}
Status remove_file_if_finished_impl(FileId file_id) {
@ -595,7 +597,8 @@ class DownloadManagerImpl final : public DownloadManager {
if (!is_completed(*file_info_ptr)) {
return Status::Error("File is active");
}
return remove_file_impl(file_id, {}, false, "remove_file_if_finished_impl");
remove_file_impl(*file_info_ptr, false, "remove_file_if_finished_impl");
return Status::OK();
}
void timeout_expired() final {
@ -762,7 +765,7 @@ class DownloadManagerImpl final : public DownloadManager {
}
void check_completed_downloads_size() {
if (!is_database_loaded_) {
if (!is_database_loaded_ || check_is_active("check_completed_downloads_size").is_error()) {
return;
}
@ -770,7 +773,7 @@ class DownloadManagerImpl final : public DownloadManager {
while (completed_download_ids_.size() > MAX_COMPLETED_DOWNLOADS) {
auto download_id = *completed_download_ids_.begin();
auto file_info_ptr = get_file_info_ptr(download_id).move_as_ok();
remove_file_impl(file_info_ptr->file_id, FileSourceId(), false, "check_completed_downloads_size");
remove_file_impl(*file_info_ptr, false, "check_completed_downloads_size");
}
}