diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index eada18967..89624e62b 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -26308,8 +26308,14 @@ void MessagesManager::import_messages(DialogId dialog_id, const td_api::object_p vector attached_file_ids; attached_file_ids.reserve(attached_files.size()); for (auto &attached_file : attached_files) { - auto r_attached_file_id = - td_->file_manager_->get_input_file_id(FileType::Document, attached_file, dialog_id, false, false); + auto file_type = td_->file_manager_->guess_file_type(attached_file); + if (file_type != FileType::Animation && file_type != FileType::Audio && file_type != FileType::Document && + file_type != FileType::Photo && file_type != FileType::Sticker && file_type != FileType::Video && + file_type != FileType::VoiceNote) { + LOG(INFO) << "Skip attached file of type " << file_type; + continue; + } + auto r_attached_file_id = td_->file_manager_->get_input_file_id(file_type, attached_file, dialog_id, false, false); if (r_attached_file_id.is_error()) { // TODO TRY_RESULT_PROMISE(promise, ...); return promise.set_error(Status::Error(400, r_attached_file_id.error().message())); diff --git a/td/telegram/files/FileManager.cpp b/td/telegram/files/FileManager.cpp index 2fb439fbb..ae6f01371 100644 --- a/td/telegram/files/FileManager.cpp +++ b/td/telegram/files/FileManager.cpp @@ -3162,6 +3162,70 @@ Result FileManager::get_map_thumbnail_file_id(Location location, int32 z FileLocationSource::FromUser, string(), std::move(conversion), owner_dialog_id, 0); } +FileType FileManager::guess_file_type(const tl_object_ptr &file) { + if (file == nullptr) { + return FileType::Temp; + } + + auto guess_file_type_by_path = [](const string &file_path) { + PathView path_view(file_path); + auto file_name = path_view.file_name(); + auto extension = path_view.extension(); + if (extension == "jpg" || extension == "jpeg") { + return FileType::Photo; + } + if (extension == "ogg" || extension == "oga" || extension == "opus") { + return FileType::VoiceNote; + } + if (extension == "3gp" || extension == "mov") { + return FileType::Video; + } + if (extension == "mp3" || extension == "mpeg3" || extension == "m4a") { + return FileType::Audio; + } + if (extension == "webp" || extension == "tgs" || extension == "opus") { + return FileType::Sticker; + } + if (extension == "gif") { + return FileType::Animation; + } + if (extension == "mp4" || extension == "mpeg4") { + return to_lower(file_name).find("-gif-") != string::npos ? FileType::Animation : FileType::Video; + } + return FileType::Document; + }; + + switch (file->get_id()) { + case td_api::inputFileLocal::ID: + return guess_file_type_by_path(static_cast(file.get())->path_); + case td_api::inputFileId::ID: { + FileId file_id(static_cast(file.get())->id_, 0); + auto file_view = get_file_view(file_id); + if (file_view.empty()) { + return FileType::Temp; + } + return file_view.get_type(); + } + case td_api::inputFileRemote::ID: { + const string &file_persistent_id = static_cast(file.get())->id_; + Result r_file_id = from_persistent_id(file_persistent_id, FileType::Temp); + if (r_file_id.is_error()) { + return FileType::Temp; + } + auto file_view = get_file_view(r_file_id.ok()); + if (file_view.empty()) { + return FileType::Temp; + } + return file_view.get_type(); + } + case td_api::inputFileGenerated::ID: + return guess_file_type_by_path(static_cast(file.get())->original_path_); + default: + UNREACHABLE(); + return FileType::Temp; + } +} + vector> FileManager::get_input_documents(const vector &file_ids) { vector> result; result.reserve(file_ids.size()); diff --git a/td/telegram/files/FileManager.h b/td/telegram/files/FileManager.h index 8fd2b5654..60332ac1c 100644 --- a/td/telegram/files/FileManager.h +++ b/td/telegram/files/FileManager.h @@ -474,6 +474,8 @@ class FileManager : public FileLoadManager::Callback { Result get_map_thumbnail_file_id(Location location, int32 zoom, int32 width, int32 height, int32 scale, DialogId owner_dialog_id) TD_WARN_UNUSED_RESULT; + FileType guess_file_type(const tl_object_ptr &file); + vector> get_input_documents(const vector &file_ids); static bool extract_was_uploaded(const tl_object_ptr &input_media);