From 719c03b0a237c83dec309dddf2fcd4d13adab603 Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 24 Jun 2024 18:57:32 +0300 Subject: [PATCH] Add and use FileManager::extract_file_references. --- td/telegram/MessageContent.cpp | 30 +++++++++++++----------- td/telegram/MessagesManager.cpp | 9 +++---- td/telegram/files/FileManager.cpp | 39 +++++++++++++++++++++++++------ td/telegram/files/FileManager.h | 16 +++++++------ 4 files changed, 63 insertions(+), 31 deletions(-) diff --git a/td/telegram/MessageContent.cpp b/td/telegram/MessageContent.cpp index 57c9c56fc..1882ea9dc 100644 --- a/td/telegram/MessageContent.cpp +++ b/td/telegram/MessageContent.cpp @@ -3517,13 +3517,15 @@ tl_object_ptr get_input_media(const MessageContent *co CHECK(!had_input_thumbnail); } if (!was_uploaded) { - auto file_reference = FileManager::extract_file_reference(input_media); - if (file_reference == FileReferenceView::invalid_file_reference()) { - if (!force) { - LOG(INFO) << "File " << file_id << " has invalid file reference"; - return nullptr; + auto file_references = FileManager::extract_file_references(input_media); + for (auto &file_reference : file_references) { + if (file_reference == FileReferenceView::invalid_file_reference()) { + if (!force) { + LOG(INFO) << "File " << file_id << " has invalid file reference"; + return nullptr; + } + LOG(ERROR) << "File " << file_id << " has invalid file reference, but we are forced to use it"; } - LOG(ERROR) << "File " << file_id << " has invalid file reference, but we forced to use it"; } } return input_media; @@ -3532,14 +3534,16 @@ tl_object_ptr get_input_media(const MessageContent *co tl_object_ptr get_input_media(const MessageContent *content, Td *td, MessageSelfDestructType ttl, const string &emoji, bool force) { auto input_media = get_input_media_impl(content, td, nullptr, nullptr, ttl, emoji); - auto file_reference = FileManager::extract_file_reference(input_media); - if (file_reference == FileReferenceView::invalid_file_reference()) { - auto file_id = get_message_content_any_file_id(content); - if (!force) { - LOG(INFO) << "File " << file_id << " has invalid file reference"; - return nullptr; + auto file_references = FileManager::extract_file_references(input_media); + for (size_t i = 0; i < file_references.size(); i++) { + if (file_references[i] == FileReferenceView::invalid_file_reference()) { + auto file_id = get_message_content_any_file_id(content); + if (!force) { + LOG(INFO) << "File " << file_id << " has invalid file reference"; + return nullptr; + } + LOG(ERROR) << "File " << file_id << " has invalid file reference, but we are forced to use it"; } - LOG(ERROR) << "File " << file_id << " has invalid file reference, but we forced to use it"; } return input_media; } diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 41b3c576d..b8f2880f3 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -3197,7 +3197,7 @@ class SendMediaQuery final : public Td::ResultHandler { FileId file_id_; FileId thumbnail_file_id_; DialogId dialog_id_; - string file_reference_; + vector file_references_; bool was_uploaded_ = false; bool was_thumbnail_uploaded_ = false; @@ -3213,7 +3213,7 @@ class SendMediaQuery final : public Td::ResultHandler { file_id_ = file_id; thumbnail_file_id_ = thumbnail_file_id; dialog_id_ = dialog_id; - file_reference_ = FileManager::extract_file_reference(input_media); + file_references_ = FileManager::extract_file_references(input_media); was_uploaded_ = FileManager::extract_was_uploaded(input_media); was_thumbnail_uploaded_ = FileManager::extract_was_thumbnail_uploaded(input_media); @@ -3292,9 +3292,10 @@ class SendMediaQuery final : public Td::ResultHandler { td_->file_manager_->delete_partial_remote_location_if_needed(file_id_, status); } } else if (!td_->auth_manager_->is_bot() && FileReferenceManager::is_file_reference_error(status)) { - if (file_id_.is_valid() && !was_uploaded_) { + auto pos = FileReferenceManager::get_file_reference_error_pos(status); + if (file_id_.is_valid() && !was_uploaded_ && pos == 0 && !file_references_.empty()) { VLOG(file_references) << "Receive " << status << " for " << file_id_; - td_->file_manager_->delete_file_reference(file_id_, file_reference_); + td_->file_manager_->delete_file_reference(file_id_, file_references_[0]); td_->messages_manager_->on_send_message_file_reference_error(random_id_); return; } else { diff --git a/td/telegram/files/FileManager.cpp b/td/telegram/files/FileManager.cpp index 557dd4c55..8f595af97 100644 --- a/td/telegram/files/FileManager.cpp +++ b/td/telegram/files/FileManager.cpp @@ -3604,7 +3604,7 @@ vector> FileManager::get_input_docume return result; } -bool FileManager::extract_was_uploaded(const tl_object_ptr &input_media) { +bool FileManager::extract_was_uploaded(const telegram_api::object_ptr &input_media) { if (input_media == nullptr) { return false; } @@ -3614,7 +3614,8 @@ bool FileManager::extract_was_uploaded(const tl_object_ptr &input_media) { +bool FileManager::extract_was_thumbnail_uploaded( + const telegram_api::object_ptr &input_media) { if (input_media == nullptr || input_media->get_id() != telegram_api::inputMediaUploadedDocument::ID) { return false; } @@ -3622,7 +3623,7 @@ bool FileManager::extract_was_thumbnail_uploaded(const tl_object_ptr(input_media.get())->thumb_ != nullptr; } -string FileManager::extract_file_reference(const tl_object_ptr &input_media) { +string FileManager::extract_file_reference(const telegram_api::object_ptr &input_media) { if (input_media == nullptr) { return string(); } @@ -3632,12 +3633,35 @@ string FileManager::extract_file_reference(const tl_object_ptr(input_media.get())->id_); case telegram_api::inputMediaPhoto::ID: return extract_file_reference(static_cast(input_media.get())->id_); + case telegram_api::inputMediaPaidMedia::ID: + UNREACHABLE(); + break; default: return string(); } } -string FileManager::extract_file_reference(const tl_object_ptr &input_document) { +vector FileManager::extract_file_references( + const telegram_api::object_ptr &input_media) { + if (input_media == nullptr) { + return {}; + } + switch (input_media->get_id()) { + case telegram_api::inputMediaDocument::ID: + case telegram_api::inputMediaPhoto::ID: + return {extract_file_reference(input_media)}; + case telegram_api::inputMediaPaidMedia::ID: + return transform(static_cast(input_media.get())->extended_media_, + [](const telegram_api::object_ptr &media) { + return extract_file_reference(media); + }); + default: + return {}; + } +} + +string FileManager::extract_file_reference( + const telegram_api::object_ptr &input_document) { if (input_document == nullptr || input_document->get_id() != telegram_api::inputDocument::ID) { return string(); } @@ -3645,7 +3669,7 @@ string FileManager::extract_file_reference(const tl_object_ptr(input_document.get())->file_reference_.as_slice().str(); } -string FileManager::extract_file_reference(const tl_object_ptr &input_photo) { +string FileManager::extract_file_reference(const telegram_api::object_ptr &input_photo) { if (input_photo == nullptr || input_photo->get_id() != telegram_api::inputPhoto::ID) { return string(); } @@ -3653,11 +3677,12 @@ string FileManager::extract_file_reference(const tl_object_ptr(input_photo.get())->file_reference_.as_slice().str(); } -bool FileManager::extract_was_uploaded(const tl_object_ptr &input_chat_photo) { +bool FileManager::extract_was_uploaded(const telegram_api::object_ptr &input_chat_photo) { return input_chat_photo != nullptr && input_chat_photo->get_id() == telegram_api::inputChatUploadedPhoto::ID; } -string FileManager::extract_file_reference(const tl_object_ptr &input_chat_photo) { +string FileManager::extract_file_reference( + const telegram_api::object_ptr &input_chat_photo) { if (input_chat_photo == nullptr || input_chat_photo->get_id() != telegram_api::inputChatPhoto::ID) { return string(); } diff --git a/td/telegram/files/FileManager.h b/td/telegram/files/FileManager.h index 4b69cee60..e63df75ff 100644 --- a/td/telegram/files/FileManager.h +++ b/td/telegram/files/FileManager.h @@ -536,19 +536,21 @@ class FileManager final : public FileLoadManager::Callback { vector> get_input_documents(const vector &file_ids); - static bool extract_was_uploaded(const tl_object_ptr &input_media); + static bool extract_was_uploaded(const telegram_api::object_ptr &input_media); - static bool extract_was_thumbnail_uploaded(const tl_object_ptr &input_media); + static bool extract_was_thumbnail_uploaded(const telegram_api::object_ptr &input_media); - static string extract_file_reference(const tl_object_ptr &input_media); + static string extract_file_reference(const telegram_api::object_ptr &input_media); - static string extract_file_reference(const tl_object_ptr &input_document); + static vector extract_file_references(const telegram_api::object_ptr &input_media); - static string extract_file_reference(const tl_object_ptr &input_photo); + static string extract_file_reference(const telegram_api::object_ptr &input_document); - static bool extract_was_uploaded(const tl_object_ptr &input_chat_photo); + static string extract_file_reference(const telegram_api::object_ptr &input_photo); - static string extract_file_reference(const tl_object_ptr &input_chat_photo); + static bool extract_was_uploaded(const telegram_api::object_ptr &input_chat_photo); + + static string extract_file_reference(const telegram_api::object_ptr &input_chat_photo); template void store_file(FileId file_id, StorerT &storer, int32 ttl = 5) const;