Add get_input_photo_size function.

This commit is contained in:
levlam 2023-05-21 16:35:33 +03:00
parent f831357538
commit c18818013a
3 changed files with 43 additions and 27 deletions

View File

@ -2074,14 +2074,11 @@ static Result<InputMessageContent> create_input_message_content(
case td_api::inputMessagePhoto::ID: {
auto input_photo = static_cast<td_api::inputMessagePhoto *>(input_message_content.get());
if (input_photo->width_ < 0 || input_photo->width_ > 10000) {
return Status::Error(400, "Wrong photo width");
}
if (input_photo->height_ < 0 || input_photo->height_ > 10000) {
return Status::Error(400, "Wrong photo height");
}
ttl = input_photo->self_destruct_time_;
TRY_RESULT(input_photo_size,
get_input_photo_size(td->file_manager_.get(), file_id, input_photo->width_, input_photo->height_));
auto message_photo = make_unique<MessagePhoto>();
if (file_view.has_remote_location() && !file_view.remote_location().is_web()) {
@ -2091,32 +2088,12 @@ static Result<InputMessageContent> create_input_message_content(
message_photo->photo.id = 0;
}
message_photo->photo.date = G()->unix_time();
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 s;
s.type = type;
s.dimensions = get_dimensions(input_photo->width_, input_photo->height_, nullptr);
auto size = file_view.size();
if (size < 0 || size >= 1000000000) {
return Status::Error(400, "Wrong photo size");
}
s.size = static_cast<int32>(size);
s.file_id = file_id;
if (thumbnail.file_id.is_valid()) {
message_photo->photo.photos.push_back(std::move(thumbnail));
}
message_photo->photo.photos.push_back(s);
message_photo->photo.photos.push_back(std::move(input_photo_size));
message_photo->photo.has_stickers = !sticker_file_ids.empty();
message_photo->photo.sticker_file_ids = std::move(sticker_file_ids);

View File

@ -408,6 +408,42 @@ 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);
}
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()) {

View File

@ -19,6 +19,7 @@
#include "td/utils/buffer.h"
#include "td/utils/common.h"
#include "td/utils/Status.h"
#include "td/utils/StringBuilder.h"
#include "td/utils/Variant.h"
@ -65,6 +66,8 @@ Variant<AnimationSize, unique_ptr<StickerPhotoSize>> process_video_size(
PhotoSize get_web_document_photo_size(FileManager *file_manager, FileType file_type, DialogId owner_dialog_id,
tl_object_ptr<telegram_api::WebDocument> web_document_ptr);
Result<PhotoSize> get_input_photo_size(FileManager *file_manager, FileId file_id, int32 width, int32 height);
td_api::object_ptr<td_api::thumbnail> get_thumbnail_object(FileManager *file_manager, const PhotoSize &photo_size,
PhotoFormat format);