Add and use FileManager::extract_file_references.

This commit is contained in:
levlam 2024-06-24 18:57:32 +03:00
parent 96d29d141a
commit 719c03b0a2
4 changed files with 63 additions and 31 deletions

View File

@ -3517,13 +3517,15 @@ tl_object_ptr<telegram_api::InputMedia> get_input_media(const MessageContent *co
CHECK(!had_input_thumbnail);
}
if (!was_uploaded) {
auto file_reference = FileManager::extract_file_reference(input_media);
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 forced to use it";
LOG(ERROR) << "File " << file_id << " has invalid file reference, but we are forced to use it";
}
}
}
return input_media;
@ -3532,14 +3534,16 @@ tl_object_ptr<telegram_api::InputMedia> get_input_media(const MessageContent *co
tl_object_ptr<telegram_api::InputMedia> 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_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 forced to use it";
LOG(ERROR) << "File " << file_id << " has invalid file reference, but we are forced to use it";
}
}
return input_media;
}

View File

@ -3197,7 +3197,7 @@ class SendMediaQuery final : public Td::ResultHandler {
FileId file_id_;
FileId thumbnail_file_id_;
DialogId dialog_id_;
string file_reference_;
vector<string> 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 {

View File

@ -3604,7 +3604,7 @@ vector<tl_object_ptr<telegram_api::InputDocument>> FileManager::get_input_docume
return result;
}
bool FileManager::extract_was_uploaded(const tl_object_ptr<telegram_api::InputMedia> &input_media) {
bool FileManager::extract_was_uploaded(const telegram_api::object_ptr<telegram_api::InputMedia> &input_media) {
if (input_media == nullptr) {
return false;
}
@ -3614,7 +3614,8 @@ bool FileManager::extract_was_uploaded(const tl_object_ptr<telegram_api::InputMe
input_media_id == telegram_api::inputMediaUploadedDocument::ID;
}
bool FileManager::extract_was_thumbnail_uploaded(const tl_object_ptr<telegram_api::InputMedia> &input_media) {
bool FileManager::extract_was_thumbnail_uploaded(
const telegram_api::object_ptr<telegram_api::InputMedia> &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<telegram_ap
return static_cast<const telegram_api::inputMediaUploadedDocument *>(input_media.get())->thumb_ != nullptr;
}
string FileManager::extract_file_reference(const tl_object_ptr<telegram_api::InputMedia> &input_media) {
string FileManager::extract_file_reference(const telegram_api::object_ptr<telegram_api::InputMedia> &input_media) {
if (input_media == nullptr) {
return string();
}
@ -3632,12 +3633,35 @@ string FileManager::extract_file_reference(const tl_object_ptr<telegram_api::Inp
return extract_file_reference(static_cast<const telegram_api::inputMediaDocument *>(input_media.get())->id_);
case telegram_api::inputMediaPhoto::ID:
return extract_file_reference(static_cast<const telegram_api::inputMediaPhoto *>(input_media.get())->id_);
case telegram_api::inputMediaPaidMedia::ID:
UNREACHABLE();
break;
default:
return string();
}
}
string FileManager::extract_file_reference(const tl_object_ptr<telegram_api::InputDocument> &input_document) {
vector<string> FileManager::extract_file_references(
const telegram_api::object_ptr<telegram_api::InputMedia> &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<const telegram_api::inputMediaPaidMedia *>(input_media.get())->extended_media_,
[](const telegram_api::object_ptr<telegram_api::InputMedia> &media) {
return extract_file_reference(media);
});
default:
return {};
}
}
string FileManager::extract_file_reference(
const telegram_api::object_ptr<telegram_api::InputDocument> &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<telegram_api::Inp
return static_cast<const telegram_api::inputDocument *>(input_document.get())->file_reference_.as_slice().str();
}
string FileManager::extract_file_reference(const tl_object_ptr<telegram_api::InputPhoto> &input_photo) {
string FileManager::extract_file_reference(const telegram_api::object_ptr<telegram_api::InputPhoto> &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<telegram_api::Inp
return static_cast<const telegram_api::inputPhoto *>(input_photo.get())->file_reference_.as_slice().str();
}
bool FileManager::extract_was_uploaded(const tl_object_ptr<telegram_api::InputChatPhoto> &input_chat_photo) {
bool FileManager::extract_was_uploaded(const telegram_api::object_ptr<telegram_api::InputChatPhoto> &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<telegram_api::InputChatPhoto> &input_chat_photo) {
string FileManager::extract_file_reference(
const telegram_api::object_ptr<telegram_api::InputChatPhoto> &input_chat_photo) {
if (input_chat_photo == nullptr || input_chat_photo->get_id() != telegram_api::inputChatPhoto::ID) {
return string();
}

View File

@ -536,19 +536,21 @@ class FileManager final : public FileLoadManager::Callback {
vector<tl_object_ptr<telegram_api::InputDocument>> get_input_documents(const vector<FileId> &file_ids);
static bool extract_was_uploaded(const tl_object_ptr<telegram_api::InputMedia> &input_media);
static bool extract_was_uploaded(const telegram_api::object_ptr<telegram_api::InputMedia> &input_media);
static bool extract_was_thumbnail_uploaded(const tl_object_ptr<telegram_api::InputMedia> &input_media);
static bool extract_was_thumbnail_uploaded(const telegram_api::object_ptr<telegram_api::InputMedia> &input_media);
static string extract_file_reference(const tl_object_ptr<telegram_api::InputMedia> &input_media);
static string extract_file_reference(const telegram_api::object_ptr<telegram_api::InputMedia> &input_media);
static string extract_file_reference(const tl_object_ptr<telegram_api::InputDocument> &input_document);
static vector<string> extract_file_references(const telegram_api::object_ptr<telegram_api::InputMedia> &input_media);
static string extract_file_reference(const tl_object_ptr<telegram_api::InputPhoto> &input_photo);
static string extract_file_reference(const telegram_api::object_ptr<telegram_api::InputDocument> &input_document);
static bool extract_was_uploaded(const tl_object_ptr<telegram_api::InputChatPhoto> &input_chat_photo);
static string extract_file_reference(const telegram_api::object_ptr<telegram_api::InputPhoto> &input_photo);
static string extract_file_reference(const tl_object_ptr<telegram_api::InputChatPhoto> &input_chat_photo);
static bool extract_was_uploaded(const telegram_api::object_ptr<telegram_api::InputChatPhoto> &input_chat_photo);
static string extract_file_reference(const telegram_api::object_ptr<telegram_api::InputChatPhoto> &input_chat_photo);
template <class StorerT>
void store_file(FileId file_id, StorerT &storer, int32 ttl = 5) const;