diff options
author | George Hazan <george.hazan@gmail.com> | 2023-06-04 19:24:05 +0300 |
---|---|---|
committer | George Hazan <george.hazan@gmail.com> | 2023-06-04 19:24:05 +0300 |
commit | efc336e60cf1331bf5f3213d296981b87b8b2a6c (patch) | |
tree | ea59ea1a324f45f6e8a06cc0887b376bfba90ca9 /protocols/Telegram/tdlib/td/td/telegram/PhotoSize.cpp | |
parent | 6e83622d2af1cec3c759f4cff6efe4df2fe3328c (diff) |
fixes #3537 (Telegram: 32-разрядная версия падает в 64-разрядной Windows) + update to the fresh TDLIB
Diffstat (limited to 'protocols/Telegram/tdlib/td/td/telegram/PhotoSize.cpp')
-rw-r--r-- | protocols/Telegram/tdlib/td/td/telegram/PhotoSize.cpp | 134 |
1 files changed, 111 insertions, 23 deletions
diff --git a/protocols/Telegram/tdlib/td/td/telegram/PhotoSize.cpp b/protocols/Telegram/tdlib/td/td/telegram/PhotoSize.cpp index d5de4dbbd9..5dc463441f 100644 --- a/protocols/Telegram/tdlib/td/td/telegram/PhotoSize.cpp +++ b/protocols/Telegram/tdlib/td/td/telegram/PhotoSize.cpp @@ -1,5 +1,5 @@ // -// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022 +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023 // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) @@ -8,6 +8,7 @@ #include "td/telegram/files/FileLocation.h" #include "td/telegram/files/FileManager.h" +#include "td/telegram/Td.h" #include "td/utils/base64.h" #include "td/utils/HttpUrl.h" @@ -122,10 +123,10 @@ static StringBuilder &operator<<(StringBuilder &string_builder, PhotoFormat form FileId register_photo_size(FileManager *file_manager, const PhotoSizeSource &source, int64 id, int64 access_hash, string file_reference, DialogId owner_dialog_id, int32 file_size, DcId dc_id, - PhotoFormat format) { - LOG(DEBUG) << "Receive " << format << " photo " << id << " of type " << source.get_file_type("register_photo_size") - << " from " << dc_id; - auto suggested_name = PSTRING() << source.get_unique_name(id) << '.' << format; + PhotoFormat format, const char *call_source) { + LOG(DEBUG) << "Receive " << format << " photo " << id << " of type " << source.get_file_type(call_source) << " from " + << dc_id << " from " << call_source; + auto suggested_name = PSTRING() << source.get_unique_name(id, call_source) << '.' << format; auto file_location_source = owner_dialog_id.get_type() == DialogType::SecretChat ? FileLocationSource::FromUser : FileLocationSource::FromServer; return file_manager->register_remote( @@ -249,7 +250,7 @@ Variant<PhotoSize, string> get_photo_size(FileManager *file_manager, PhotoSizeSo } res.file_id = register_photo_size(file_manager, source, id, access_hash, std::move(file_reference), owner_dialog_id, - res.size, dc_id, format); + res.size, dc_id, format, "get_photo_size"); if (!content.empty()) { file_manager->set_content(res.file_id, std::move(content)); @@ -258,36 +259,63 @@ Variant<PhotoSize, string> get_photo_size(FileManager *file_manager, PhotoSizeSo return std::move(res); } -AnimationSize get_animation_size(FileManager *file_manager, PhotoSizeSource source, int64 id, int64 access_hash, +AnimationSize get_animation_size(Td *td, PhotoSizeSource source, int64 id, int64 access_hash, std::string file_reference, DcId dc_id, DialogId owner_dialog_id, tl_object_ptr<telegram_api::videoSize> &&size) { CHECK(size != nullptr); - AnimationSize res; + AnimationSize result; if (size->type_ != "p" && size->type_ != "u" && size->type_ != "v") { LOG(ERROR) << "Unsupported videoSize \"" << size->type_ << "\" in " << to_string(size); } - res.type = static_cast<uint8>(size->type_[0]); - if (res.type >= 128) { - LOG(ERROR) << "Wrong videoSize \"" << res.type << "\" " << res; - res.type = 0; + result.type = static_cast<uint8>(size->type_[0]); + if (result.type >= 128) { + LOG(ERROR) << "Wrong videoSize \"" << result.type << "\" " << result; + result.type = 0; } - res.dimensions = get_dimensions(size->w_, size->h_, "get_animation_size"); - res.size = size->size_; + result.dimensions = get_dimensions(size->w_, size->h_, "get_animation_size"); + result.size = size->size_; if ((size->flags_ & telegram_api::videoSize::VIDEO_START_TS_MASK) != 0) { - res.main_frame_timestamp = size->video_start_ts_; + result.main_frame_timestamp = size->video_start_ts_; } if (source.get_type("get_animation_size") == PhotoSizeSource::Type::Thumbnail) { - source.thumbnail().thumbnail_type = res.type; + source.thumbnail().thumbnail_type = result.type; } - if (res.size < 0 || res.size > 1000000000) { - LOG(ERROR) << "Receive animation of size " << res.size; - res.size = 0; + if (result.size < 0 || result.size > 1000000000) { + LOG(ERROR) << "Receive animation of size " << result.size; + result.size = 0; } - res.file_id = register_photo_size(file_manager, source, id, access_hash, std::move(file_reference), owner_dialog_id, - res.size, dc_id, PhotoFormat::Mpeg4); - return res; + result.file_id = register_photo_size(td->file_manager_.get(), source, id, access_hash, std::move(file_reference), + owner_dialog_id, result.size, dc_id, PhotoFormat::Mpeg4, "get_animation_size"); + return result; +} + +Variant<AnimationSize, unique_ptr<StickerPhotoSize>> process_video_size( + Td *td, PhotoSizeSource source, int64 id, int64 access_hash, std::string file_reference, DcId dc_id, + DialogId owner_dialog_id, tl_object_ptr<telegram_api::VideoSize> &&size_ptr) { + CHECK(size_ptr != nullptr); + switch (size_ptr->get_id()) { + case telegram_api::videoSize::ID: { + auto animation_size = get_animation_size(td, source, id, access_hash, std::move(file_reference), dc_id, + owner_dialog_id, move_tl_object_as<telegram_api::videoSize>(size_ptr)); + if (animation_size.type == 0) { + return {}; + } + return std::move(animation_size); + } + case telegram_api::videoSizeEmojiMarkup::ID: + case telegram_api::videoSizeStickerMarkup::ID: { + auto sticker_photo_size = StickerPhotoSize::get_sticker_photo_size(td, std::move(size_ptr)); + if (sticker_photo_size == nullptr) { + return {}; + } + return std::move(sticker_photo_size); + } + default: + UNREACHABLE(); + return {}; + } } PhotoSize get_web_document_photo_size(FileManager *file_manager, FileType file_type, DialogId owner_dialog_id, @@ -380,6 +408,65 @@ PhotoSize get_web_document_photo_size(FileManager *file_manager, FileType file_t return s; } +Result<PhotoSize> get_input_photo_size(FileManager *file_manager, FileId file_id, int32 width, int32 height) { + if (width < 0 || width > 10000) { + return Status::Error(400, "Wrong photo width"); + } + if (height < 0 || height > 10000) { + return Status::Error(400, "Wrong photo height"); + } + if (width + height > 10000) { + return Status::Error(400, "Photo dimensions are too big"); + } + + auto file_view = file_manager->get_file_view(file_id); + auto file_size = file_view.size(); + if (file_size < 0 || file_size >= 1000000000) { + return Status::Error(400, "Photo is too big"); + } + + int32 type = 'i'; + if (file_view.has_remote_location() && !file_view.remote_location().is_web()) { + auto photo_size_source = file_view.remote_location().get_source(); + if (photo_size_source.get_type("create_input_message_content") == PhotoSizeSource::Type::Thumbnail) { + auto old_type = photo_size_source.thumbnail().thumbnail_type; + if (old_type != 't') { + type = old_type; + } + } + } + + PhotoSize result; + result.type = type; + result.dimensions = get_dimensions(width, height, nullptr); + result.size = static_cast<int32>(file_size); + result.file_id = file_id; + return std::move(result); +} + +PhotoSize get_input_thumbnail_photo_size(FileManager *file_manager, const td_api::inputThumbnail *input_thumbnail, + DialogId dialog_id, bool is_secret) { + PhotoSize thumbnail; + if (input_thumbnail != nullptr) { + auto r_thumbnail_file_id = + file_manager->get_input_thumbnail_file_id(input_thumbnail->thumbnail_, dialog_id, is_secret); + if (r_thumbnail_file_id.is_error()) { + LOG(WARNING) << "Ignore thumbnail file: " << r_thumbnail_file_id.error().message(); + } else { + thumbnail.type = 't'; + thumbnail.dimensions = get_dimensions(input_thumbnail->width_, input_thumbnail->height_, nullptr); + thumbnail.file_id = r_thumbnail_file_id.ok(); + CHECK(thumbnail.file_id.is_valid()); + + FileView thumbnail_file_view = file_manager->get_file_view(thumbnail.file_id); + if (thumbnail_file_view.has_remote_location()) { + // TODO file_manager->delete_remote_location(thumbnail.file_id); + } + } + } + return thumbnail; +} + td_api::object_ptr<td_api::thumbnail> get_thumbnail_object(FileManager *file_manager, const PhotoSize &photo_size, PhotoFormat format) { if (!photo_size.file_id.is_valid()) { @@ -425,7 +512,8 @@ bool operator<(const PhotoSize &lhs, const PhotoSize &rhs) { } StringBuilder &operator<<(StringBuilder &string_builder, const PhotoSize &photo_size) { - return string_builder << "{type = " << photo_size.type << ", dimensions = " << photo_size.dimensions + char type = 32 <= photo_size.type && photo_size.type <= 127 ? static_cast<char>(photo_size.type) : '?'; + return string_builder << "{type = " << type << ", dimensions = " << photo_size.dimensions << ", size = " << photo_size.size << ", file_id = " << photo_size.file_id << ", progressive_sizes = " << photo_size.progressive_sizes << "}"; } |