2018-12-31 20:04:05 +01:00
|
|
|
//
|
2021-01-01 13:57:46 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// 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)
|
|
|
|
//
|
|
|
|
#include "td/telegram/Photo.h"
|
|
|
|
|
|
|
|
#include "td/telegram/secret_api.h"
|
|
|
|
#include "td/telegram/telegram_api.h"
|
|
|
|
|
2018-12-27 20:24:44 +01:00
|
|
|
#include "td/telegram/files/FileEncryptionKey.h"
|
2019-01-19 22:54:29 +01:00
|
|
|
#include "td/telegram/files/FileLocation.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/files/FileManager.h"
|
2018-06-26 01:43:11 +02:00
|
|
|
#include "td/telegram/net/DcId.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-01-01 13:59:53 +01:00
|
|
|
#include "td/utils/algorithm.h"
|
2019-03-01 20:51:33 +01:00
|
|
|
#include "td/utils/base64.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/common.h"
|
|
|
|
#include "td/utils/format.h"
|
2018-03-03 22:33:26 +01:00
|
|
|
#include "td/utils/HttpUrl.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/misc.h"
|
|
|
|
#include "td/utils/Random.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
2020-07-07 10:41:01 +02:00
|
|
|
#include <cmath>
|
2018-12-31 20:04:05 +01:00
|
|
|
#include <limits>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
static uint16 get_dimension(int32 size) {
|
|
|
|
if (size < 0 || size > 65535) {
|
|
|
|
LOG(ERROR) << "Wrong image dimension = " << size;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return narrow_cast<uint16>(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
Dimensions get_dimensions(int32 width, int32 height) {
|
|
|
|
Dimensions result;
|
|
|
|
result.width = get_dimension(width);
|
|
|
|
result.height = get_dimension(height);
|
|
|
|
if (result.width == 0 || result.height == 0) {
|
|
|
|
result.width = 0;
|
|
|
|
result.height = 0;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-04-03 09:35:04 +02:00
|
|
|
static uint32 get_pixel_count(const Dimensions &dimensions) {
|
|
|
|
return static_cast<uint32>(dimensions.width) * static_cast<uint32>(dimensions.height);
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
bool operator==(const Dimensions &lhs, const Dimensions &rhs) {
|
|
|
|
return lhs.width == rhs.width && lhs.height == rhs.height;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const Dimensions &lhs, const Dimensions &rhs) {
|
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder &operator<<(StringBuilder &string_builder, const Dimensions &dimensions) {
|
|
|
|
return string_builder << "(" << dimensions.width << ", " << dimensions.height << ")";
|
|
|
|
}
|
|
|
|
|
2019-03-01 20:51:33 +01:00
|
|
|
td_api::object_ptr<td_api::minithumbnail> get_minithumbnail_object(const string &packed) {
|
|
|
|
if (packed.size() < 3) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (packed[0] == '\x01') {
|
|
|
|
static const string header =
|
|
|
|
base64_decode(
|
|
|
|
"/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDACgcHiMeGSgjISMtKygwPGRBPDc3PHtYXUlkkYCZlo+AjIqgtObDoKrarYqMyP/L2u71////"
|
|
|
|
"m8H///"
|
|
|
|
"/6/+b9//j/2wBDASstLTw1PHZBQXb4pYyl+Pj4+Pj4+Pj4+Pj4+Pj4+Pj4+Pj4+Pj4+Pj4+Pj4+Pj4+Pj4+Pj4+Pj4+Pj4+Pj4+Pj/"
|
|
|
|
"wAARCAAAAAADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/"
|
|
|
|
"8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0R"
|
|
|
|
"FRkd"
|
|
|
|
"ISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2"
|
|
|
|
"uHi4"
|
|
|
|
"+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/"
|
|
|
|
"8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkN"
|
|
|
|
"ERUZ"
|
|
|
|
"HSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2"
|
|
|
|
"Nna4"
|
|
|
|
"uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwA=")
|
|
|
|
.move_as_ok();
|
|
|
|
static const string footer = base64_decode("/9k=").move_as_ok();
|
|
|
|
auto result = td_api::make_object<td_api::minithumbnail>();
|
2019-11-13 21:00:49 +01:00
|
|
|
result->height_ = static_cast<unsigned char>(packed[1]);
|
|
|
|
result->width_ = static_cast<unsigned char>(packed[2]);
|
2019-03-01 20:51:33 +01:00
|
|
|
result->data_ = PSTRING() << header.substr(0, 164) << packed[1] << header[165] << packed[2] << header.substr(167)
|
|
|
|
<< packed.substr(3) << footer;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-05-31 21:22:15 +02:00
|
|
|
static td_api::object_ptr<td_api::ThumbnailFormat> get_thumbnail_format_object(PhotoFormat format) {
|
|
|
|
switch (format) {
|
|
|
|
case PhotoFormat::Jpeg:
|
|
|
|
return td_api::make_object<td_api::thumbnailFormatJpeg>();
|
|
|
|
case PhotoFormat::Png:
|
|
|
|
return td_api::make_object<td_api::thumbnailFormatPng>();
|
|
|
|
case PhotoFormat::Webp:
|
|
|
|
return td_api::make_object<td_api::thumbnailFormatWebp>();
|
2020-06-01 19:34:48 +02:00
|
|
|
case PhotoFormat::Gif:
|
|
|
|
return td_api::make_object<td_api::thumbnailFormatGif>();
|
2020-05-31 21:22:15 +02:00
|
|
|
case PhotoFormat::Tgs:
|
|
|
|
return td_api::make_object<td_api::thumbnailFormatTgs>();
|
|
|
|
case PhotoFormat::Mpeg4:
|
|
|
|
return td_api::make_object<td_api::thumbnailFormatMpeg4>();
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-11 21:17:49 +02:00
|
|
|
static StringBuilder &operator<<(StringBuilder &string_builder, PhotoFormat format) {
|
|
|
|
switch (format) {
|
|
|
|
case PhotoFormat::Jpeg:
|
|
|
|
return string_builder << "jpg";
|
|
|
|
case PhotoFormat::Png:
|
|
|
|
return string_builder << "png";
|
|
|
|
case PhotoFormat::Webp:
|
|
|
|
return string_builder << "webp";
|
2020-06-01 19:34:48 +02:00
|
|
|
case PhotoFormat::Gif:
|
|
|
|
return string_builder << "gif";
|
2020-05-11 21:17:49 +02:00
|
|
|
case PhotoFormat::Tgs:
|
|
|
|
return string_builder << "tgs";
|
2020-05-30 00:48:56 +02:00
|
|
|
case PhotoFormat::Mpeg4:
|
|
|
|
return string_builder << "mp4";
|
2020-05-11 21:17:49 +02:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return string_builder;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-08 11:26:35 +02:00
|
|
|
static FileId register_photo(FileManager *file_manager, const PhotoSizeSource &source, int64 id, int64 access_hash,
|
|
|
|
std::string file_reference,
|
|
|
|
tl_object_ptr<telegram_api::fileLocationToBeDeprecated> &&location,
|
2020-05-11 21:17:49 +02:00
|
|
|
DialogId owner_dialog_id, int32 file_size, DcId dc_id, PhotoFormat format) {
|
2019-06-08 11:26:35 +02:00
|
|
|
int32 local_id = location->local_id_;
|
|
|
|
int64 volume_id = location->volume_id_;
|
2020-05-11 21:17:49 +02:00
|
|
|
LOG(DEBUG) << "Receive " << format << " photo of type " << source.get_file_type() << " in [" << dc_id << ","
|
|
|
|
<< volume_id << "," << local_id << "]. Id: (" << id << ", " << access_hash << ")";
|
|
|
|
auto suggested_name = PSTRING() << static_cast<uint64>(volume_id) << "_" << static_cast<uint64>(local_id) << '.'
|
|
|
|
<< format;
|
2019-06-17 15:46:20 +02:00
|
|
|
auto file_location_source = owner_dialog_id.get_type() == DialogType::SecretChat ? FileLocationSource::FromUser
|
|
|
|
: FileLocationSource::FromServer;
|
2019-06-08 11:26:35 +02:00
|
|
|
return file_manager->register_remote(
|
|
|
|
FullRemoteFileLocation(source, id, access_hash, local_id, volume_id, dc_id, std::move(file_reference)),
|
2019-06-17 15:46:20 +02:00
|
|
|
file_location_source, owner_dialog_id, file_size, 0, std::move(suggested_name));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-06-08 11:26:35 +02:00
|
|
|
ProfilePhoto get_profile_photo(FileManager *file_manager, UserId user_id, int64 user_access_hash,
|
2018-12-31 20:04:05 +01:00
|
|
|
tl_object_ptr<telegram_api::UserProfilePhoto> &&profile_photo_ptr) {
|
|
|
|
ProfilePhoto result;
|
|
|
|
int32 profile_photo_id =
|
|
|
|
profile_photo_ptr == nullptr ? telegram_api::userProfilePhotoEmpty::ID : profile_photo_ptr->get_id();
|
|
|
|
switch (profile_photo_id) {
|
|
|
|
case telegram_api::userProfilePhotoEmpty::ID:
|
|
|
|
break;
|
|
|
|
case telegram_api::userProfilePhoto::ID: {
|
|
|
|
auto profile_photo = move_tl_object_as<telegram_api::userProfilePhoto>(profile_photo_ptr);
|
|
|
|
|
2019-06-08 11:26:35 +02:00
|
|
|
auto dc_id = DcId::create(profile_photo->dc_id_);
|
2020-07-08 21:59:31 +02:00
|
|
|
result.has_animation = (profile_photo->flags_ & telegram_api::userProfilePhoto::HAS_VIDEO_MASK) != 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
result.id = profile_photo->photo_id_;
|
2020-05-11 21:17:49 +02:00
|
|
|
result.small_file_id =
|
|
|
|
register_photo(file_manager, {DialogId(user_id), user_access_hash, false}, result.id, 0, "",
|
|
|
|
std::move(profile_photo->photo_small_), DialogId(), 0, dc_id, PhotoFormat::Jpeg);
|
|
|
|
result.big_file_id =
|
|
|
|
register_photo(file_manager, {DialogId(user_id), user_access_hash, true}, result.id, 0, "",
|
|
|
|
std::move(profile_photo->photo_big_), DialogId(), 0, dc_id, PhotoFormat::Jpeg);
|
2018-12-31 20:04:05 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
tl_object_ptr<td_api::profilePhoto> get_profile_photo_object(FileManager *file_manager,
|
2020-07-08 21:59:31 +02:00
|
|
|
const ProfilePhoto &profile_photo) {
|
|
|
|
if (!profile_photo.small_file_id.is_valid()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
2020-07-08 21:59:31 +02:00
|
|
|
return td_api::make_object<td_api::profilePhoto>(
|
|
|
|
profile_photo.id, file_manager->get_file_object(profile_photo.small_file_id),
|
|
|
|
file_manager->get_file_object(profile_photo.big_file_id), profile_photo.has_animation);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const ProfilePhoto &lhs, const ProfilePhoto &rhs) {
|
|
|
|
bool location_differs = lhs.small_file_id != rhs.small_file_id || lhs.big_file_id != rhs.big_file_id;
|
|
|
|
bool id_differs;
|
|
|
|
if (lhs.id == -1 && rhs.id == -1) {
|
|
|
|
// group chat photo
|
|
|
|
id_differs = location_differs;
|
|
|
|
} else {
|
|
|
|
id_differs = lhs.id != rhs.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (location_differs) {
|
2019-02-21 16:58:20 +01:00
|
|
|
LOG_IF(ERROR, !id_differs) << "Photo " << lhs.id << " location has changed. First profilePhoto: " << lhs
|
2018-12-31 20:04:05 +01:00
|
|
|
<< ", second profilePhoto: " << rhs;
|
|
|
|
return false;
|
|
|
|
}
|
2020-07-08 21:59:31 +02:00
|
|
|
return lhs.has_animation == rhs.has_animation && !id_differs;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const ProfilePhoto &lhs, const ProfilePhoto &rhs) {
|
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder &operator<<(StringBuilder &string_builder, const ProfilePhoto &profile_photo) {
|
|
|
|
return string_builder << "<id = " << profile_photo.id << ", small_file_id = " << profile_photo.small_file_id
|
2020-06-24 10:45:36 +02:00
|
|
|
<< ", big_file_id = " << profile_photo.big_file_id
|
2020-07-08 21:59:31 +02:00
|
|
|
<< ", has_animation = " << profile_photo.has_animation << ">";
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-06-08 11:26:35 +02:00
|
|
|
DialogPhoto get_dialog_photo(FileManager *file_manager, DialogId dialog_id, int64 dialog_access_hash,
|
|
|
|
tl_object_ptr<telegram_api::ChatPhoto> &&chat_photo_ptr) {
|
2018-12-31 20:04:05 +01:00
|
|
|
int32 chat_photo_id = chat_photo_ptr == nullptr ? telegram_api::chatPhotoEmpty::ID : chat_photo_ptr->get_id();
|
|
|
|
|
|
|
|
DialogPhoto result;
|
|
|
|
switch (chat_photo_id) {
|
|
|
|
case telegram_api::chatPhotoEmpty::ID:
|
|
|
|
break;
|
|
|
|
case telegram_api::chatPhoto::ID: {
|
|
|
|
auto chat_photo = move_tl_object_as<telegram_api::chatPhoto>(chat_photo_ptr);
|
|
|
|
|
2019-06-08 11:26:35 +02:00
|
|
|
auto dc_id = DcId::create(chat_photo->dc_id_);
|
2020-07-08 21:59:31 +02:00
|
|
|
result.has_animation = (chat_photo->flags_ & telegram_api::chatPhoto::HAS_VIDEO_MASK) != 0;
|
2020-05-11 21:17:49 +02:00
|
|
|
result.small_file_id =
|
|
|
|
register_photo(file_manager, {dialog_id, dialog_access_hash, false}, 0, 0, "",
|
|
|
|
std::move(chat_photo->photo_small_), DialogId(), 0, dc_id, PhotoFormat::Jpeg);
|
2019-06-08 11:26:35 +02:00
|
|
|
result.big_file_id = register_photo(file_manager, {dialog_id, dialog_access_hash, true}, 0, 0, "",
|
2020-05-11 21:17:49 +02:00
|
|
|
std::move(chat_photo->photo_big_), DialogId(), 0, dc_id, PhotoFormat::Jpeg);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-07-07 12:00:56 +02:00
|
|
|
tl_object_ptr<td_api::chatPhotoInfo> get_chat_photo_info_object(FileManager *file_manager,
|
|
|
|
const DialogPhoto *dialog_photo) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (dialog_photo == nullptr || !dialog_photo->small_file_id.is_valid()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2020-07-07 12:00:56 +02:00
|
|
|
return td_api::make_object<td_api::chatPhotoInfo>(file_manager->get_file_object(dialog_photo->small_file_id),
|
2020-07-08 21:59:31 +02:00
|
|
|
file_manager->get_file_object(dialog_photo->big_file_id),
|
|
|
|
dialog_photo->has_animation);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-01-20 04:34:47 +01:00
|
|
|
vector<FileId> dialog_photo_get_file_ids(const DialogPhoto &dialog_photo) {
|
|
|
|
vector<FileId> result;
|
|
|
|
if (dialog_photo.small_file_id.is_valid()) {
|
|
|
|
result.push_back(dialog_photo.small_file_id);
|
|
|
|
}
|
|
|
|
if (dialog_photo.big_file_id.is_valid()) {
|
|
|
|
result.push_back(dialog_photo.big_file_id);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-07-20 16:46:54 +02:00
|
|
|
DialogPhoto as_fake_dialog_photo(const Photo &photo) {
|
2019-06-08 11:26:35 +02:00
|
|
|
DialogPhoto result;
|
2020-06-23 20:04:26 +02:00
|
|
|
if (!photo.is_empty()) {
|
2019-06-08 11:26:35 +02:00
|
|
|
for (auto &size : photo.photos) {
|
|
|
|
if (size.type == 'a') {
|
|
|
|
result.small_file_id = size.file_id;
|
|
|
|
} else if (size.type == 'c') {
|
|
|
|
result.big_file_id = size.file_id;
|
|
|
|
}
|
|
|
|
}
|
2020-07-08 21:59:31 +02:00
|
|
|
result.has_animation = !photo.animations.empty();
|
2019-06-08 11:26:35 +02:00
|
|
|
if (!result.small_file_id.is_valid() || !result.big_file_id.is_valid()) {
|
|
|
|
LOG(ERROR) << "Failed to convert " << photo << " to chat photo";
|
|
|
|
return DialogPhoto();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-07-20 16:46:54 +02:00
|
|
|
ProfilePhoto as_profile_photo(FileManager *file_manager, UserId user_id, int64 user_access_hash, const Photo &photo) {
|
2020-07-20 16:05:49 +02:00
|
|
|
ProfilePhoto result;
|
2020-07-20 16:46:54 +02:00
|
|
|
static_cast<DialogPhoto &>(result) = as_fake_dialog_photo(photo);
|
|
|
|
if (!result.small_file_id.is_valid()) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto reregister_photo = [&](bool is_big, FileId file_id) {
|
|
|
|
auto file_view = file_manager->get_file_view(file_id);
|
|
|
|
CHECK(file_view.has_remote_location());
|
|
|
|
auto remote = file_view.remote_location();
|
|
|
|
CHECK(remote.is_photo());
|
|
|
|
CHECK(!remote.is_web());
|
|
|
|
remote.set_source({DialogId(user_id), user_access_hash, is_big});
|
|
|
|
return file_manager->register_remote(std::move(remote), FileLocationSource::FromServer, DialogId(),
|
|
|
|
file_view.size(), file_view.expected_size(), file_view.remote_name());
|
|
|
|
};
|
|
|
|
|
2020-07-20 16:05:49 +02:00
|
|
|
result.id = photo.id.get();
|
2020-07-20 16:46:54 +02:00
|
|
|
result.small_file_id = reregister_photo(false, result.small_file_id);
|
|
|
|
result.big_file_id = reregister_photo(true, result.big_file_id);
|
|
|
|
|
2020-07-20 16:05:49 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
bool operator==(const DialogPhoto &lhs, const DialogPhoto &rhs) {
|
2020-06-24 10:45:36 +02:00
|
|
|
return lhs.small_file_id == rhs.small_file_id && lhs.big_file_id == rhs.big_file_id &&
|
2020-07-08 21:59:31 +02:00
|
|
|
lhs.has_animation == rhs.has_animation;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const DialogPhoto &lhs, const DialogPhoto &rhs) {
|
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder &operator<<(StringBuilder &string_builder, const DialogPhoto &dialog_photo) {
|
|
|
|
return string_builder << "<small_file_id = " << dialog_photo.small_file_id
|
2020-06-24 10:45:36 +02:00
|
|
|
<< ", big_file_id = " << dialog_photo.big_file_id
|
2020-07-08 21:59:31 +02:00
|
|
|
<< ", has_animation = " << dialog_photo.has_animation << ">";
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-03-01 20:51:33 +01:00
|
|
|
PhotoSize get_secret_thumbnail_photo_size(FileManager *file_manager, BufferSlice bytes, DialogId owner_dialog_id,
|
|
|
|
int32 width, int32 height) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (bytes.empty()) {
|
|
|
|
return PhotoSize();
|
|
|
|
}
|
|
|
|
PhotoSize res;
|
|
|
|
res.type = 't';
|
|
|
|
res.dimensions = get_dimensions(width, height);
|
|
|
|
res.size = narrow_cast<int32>(bytes.size());
|
|
|
|
|
|
|
|
// generate some random remote location to save
|
|
|
|
auto dc_id = DcId::invalid();
|
2020-10-22 01:11:32 +02:00
|
|
|
auto local_id = -(Random::secure_int32() & 0x7FFFFFFF);
|
2018-12-31 20:04:05 +01:00
|
|
|
auto volume_id = Random::secure_int64();
|
2019-06-08 11:26:35 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
res.file_id = file_manager->register_remote(
|
2019-06-08 11:26:35 +02:00
|
|
|
FullRemoteFileLocation(PhotoSizeSource(FileType::EncryptedThumbnail, 't'), 0, 0, local_id, volume_id, dc_id,
|
|
|
|
string()),
|
2018-03-08 21:12:31 +01:00
|
|
|
FileLocationSource::FromServer, owner_dialog_id, res.size, 0,
|
|
|
|
PSTRING() << static_cast<uint64>(volume_id) << "_" << static_cast<uint64>(local_id) << ".jpg");
|
2018-12-31 20:04:05 +01:00
|
|
|
file_manager->set_content(res.file_id, std::move(bytes));
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2019-06-08 11:26:35 +02:00
|
|
|
Variant<PhotoSize, string> get_photo_size(FileManager *file_manager, PhotoSizeSource source, int64 id,
|
|
|
|
int64 access_hash, std::string file_reference, DcId dc_id,
|
|
|
|
DialogId owner_dialog_id, tl_object_ptr<telegram_api::PhotoSize> &&size_ptr,
|
2020-11-09 21:41:23 +01:00
|
|
|
PhotoFormat format, bool expect_jpeg_minithumbnail) {
|
2019-05-17 17:08:17 +02:00
|
|
|
CHECK(size_ptr != nullptr);
|
|
|
|
|
2019-06-08 11:26:35 +02:00
|
|
|
tl_object_ptr<telegram_api::fileLocationToBeDeprecated> location;
|
2018-12-31 20:04:05 +01:00
|
|
|
string type;
|
|
|
|
PhotoSize res;
|
|
|
|
BufferSlice content;
|
2019-01-22 17:08:41 +01:00
|
|
|
switch (size_ptr->get_id()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
case telegram_api::photoSizeEmpty::ID:
|
2019-03-01 20:51:33 +01:00
|
|
|
return std::move(res);
|
2018-12-31 20:04:05 +01:00
|
|
|
case telegram_api::photoSize::ID: {
|
|
|
|
auto size = move_tl_object_as<telegram_api::photoSize>(size_ptr);
|
|
|
|
|
|
|
|
type = std::move(size->type_);
|
2019-06-08 11:26:35 +02:00
|
|
|
location = std::move(size->location_);
|
2018-12-31 20:04:05 +01:00
|
|
|
res.dimensions = get_dimensions(size->w_, size->h_);
|
|
|
|
res.size = size->size_;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case telegram_api::photoCachedSize::ID: {
|
|
|
|
auto size = move_tl_object_as<telegram_api::photoCachedSize>(size_ptr);
|
|
|
|
|
|
|
|
type = std::move(size->type_);
|
2019-06-08 11:26:35 +02:00
|
|
|
location = std::move(size->location_);
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(size->bytes_.size() <= static_cast<size_t>(std::numeric_limits<int32>::max()));
|
|
|
|
res.dimensions = get_dimensions(size->w_, size->h_);
|
|
|
|
res.size = static_cast<int32>(size->bytes_.size());
|
|
|
|
|
|
|
|
content = std::move(size->bytes_);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2019-03-01 20:51:33 +01:00
|
|
|
case telegram_api::photoStrippedSize::ID: {
|
|
|
|
auto size = move_tl_object_as<telegram_api::photoStrippedSize>(size_ptr);
|
2020-11-09 21:41:23 +01:00
|
|
|
if (!expect_jpeg_minithumbnail) {
|
|
|
|
LOG(ERROR) << "Receive unexpected JPEG minithumbnail";
|
|
|
|
return std::move(res);
|
|
|
|
}
|
2019-03-01 20:51:33 +01:00
|
|
|
return size->bytes_.as_slice().str();
|
|
|
|
}
|
2020-07-31 04:50:11 +02:00
|
|
|
case telegram_api::photoSizeProgressive::ID: {
|
|
|
|
auto size = move_tl_object_as<telegram_api::photoSizeProgressive>(size_ptr);
|
|
|
|
|
|
|
|
if (size->sizes_.empty()) {
|
|
|
|
LOG(ERROR) << "Receive " << to_string(size);
|
|
|
|
return std::move(res);
|
|
|
|
}
|
|
|
|
std::sort(size->sizes_.begin(), size->sizes_.end());
|
|
|
|
|
|
|
|
type = std::move(size->type_);
|
|
|
|
location = std::move(size->location_);
|
|
|
|
res.dimensions = get_dimensions(size->w_, size->h_);
|
|
|
|
res.size = size->sizes_.back();
|
2020-08-25 14:09:24 +02:00
|
|
|
size->sizes_.pop_back();
|
|
|
|
res.progressive_sizes = std::move(size->sizes_);
|
2020-07-31 04:50:11 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2020-11-09 21:41:23 +01:00
|
|
|
case telegram_api::photoPathSize::ID: {
|
|
|
|
auto size = move_tl_object_as<telegram_api::photoPathSize>(size_ptr);
|
|
|
|
if (expect_jpeg_minithumbnail) {
|
|
|
|
LOG(ERROR) << "Receive unexpected SVG minithumbnail";
|
|
|
|
return std::move(res);
|
|
|
|
}
|
|
|
|
return size->bytes_.as_slice().str();
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type.size() != 1) {
|
|
|
|
res.type = 0;
|
2019-06-08 11:26:35 +02:00
|
|
|
LOG(ERROR) << "Wrong photoSize \"" << type << "\" " << res;
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2019-06-12 13:42:06 +02:00
|
|
|
res.type = static_cast<uint8>(type[0]);
|
2019-06-08 11:26:35 +02:00
|
|
|
}
|
2019-06-18 18:56:59 +02:00
|
|
|
if (source.get_type() == PhotoSizeSource::Type::Thumbnail) {
|
2019-06-11 12:10:14 +02:00
|
|
|
source.thumbnail().thumbnail_type = res.type;
|
2019-06-08 11:26:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
res.file_id = register_photo(file_manager, source, id, access_hash, file_reference, std::move(location),
|
2020-05-11 21:17:49 +02:00
|
|
|
owner_dialog_id, res.size, dc_id, format);
|
2019-06-08 11:26:35 +02:00
|
|
|
|
|
|
|
if (!content.empty()) {
|
|
|
|
file_manager->set_content(res.file_id, std::move(content));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-03-01 20:51:33 +01:00
|
|
|
return std::move(res);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2020-07-07 10:41:01 +02:00
|
|
|
AnimationSize get_animation_size(FileManager *file_manager, 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) {
|
2020-05-30 00:48:56 +02:00
|
|
|
CHECK(size != nullptr);
|
2020-07-07 10:41:01 +02:00
|
|
|
AnimationSize res;
|
2020-06-24 13:13:33 +02:00
|
|
|
if (size->type_ != "v" && size->type_ != "u") {
|
2020-05-30 00:48:56 +02:00
|
|
|
LOG(ERROR) << "Wrong videoSize \"" << size->type_ << "\" in " << to_string(size);
|
|
|
|
}
|
2020-06-24 13:13:33 +02:00
|
|
|
res.type = static_cast<uint8>(size->type_[0]);
|
2020-05-30 00:48:56 +02:00
|
|
|
res.dimensions = get_dimensions(size->w_, size->h_);
|
|
|
|
res.size = size->size_;
|
2020-07-07 10:41:01 +02:00
|
|
|
if ((size->flags_ & telegram_api::videoSize::VIDEO_START_TS_MASK) != 0) {
|
|
|
|
res.main_frame_timestamp = size->video_start_ts_;
|
|
|
|
}
|
2020-05-30 00:48:56 +02:00
|
|
|
|
|
|
|
if (source.get_type() == PhotoSizeSource::Type::Thumbnail) {
|
|
|
|
source.thumbnail().thumbnail_type = res.type;
|
|
|
|
}
|
|
|
|
|
|
|
|
res.file_id = register_photo(file_manager, source, id, access_hash, file_reference, std::move(size->location_),
|
|
|
|
owner_dialog_id, res.size, dc_id, PhotoFormat::Mpeg4);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-03-03 22:33:26 +01:00
|
|
|
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) {
|
|
|
|
if (web_document_ptr == nullptr) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
FileId file_id;
|
|
|
|
vector<tl_object_ptr<telegram_api::DocumentAttribute>> attributes;
|
|
|
|
int32 size = 0;
|
2020-05-30 00:48:56 +02:00
|
|
|
string mime_type;
|
2018-03-03 22:33:26 +01:00
|
|
|
switch (web_document_ptr->get_id()) {
|
|
|
|
case telegram_api::webDocument::ID: {
|
|
|
|
auto web_document = move_tl_object_as<telegram_api::webDocument>(web_document_ptr);
|
|
|
|
auto r_http_url = parse_url(web_document->url_);
|
|
|
|
if (r_http_url.is_error()) {
|
|
|
|
LOG(ERROR) << "Can't parse URL " << web_document->url_;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
auto http_url = r_http_url.move_as_ok();
|
|
|
|
auto url = http_url.get_url();
|
2018-06-25 23:10:53 +02:00
|
|
|
file_id = file_manager->register_remote(FullRemoteFileLocation(file_type, url, web_document->access_hash_),
|
|
|
|
FileLocationSource::FromServer, owner_dialog_id, 0, web_document->size_,
|
|
|
|
get_url_query_file_name(http_url.query_));
|
2018-03-03 22:33:26 +01:00
|
|
|
size = web_document->size_;
|
2020-05-30 00:48:56 +02:00
|
|
|
mime_type = std::move(web_document->mime_type_);
|
2018-03-03 22:33:26 +01:00
|
|
|
attributes = std::move(web_document->attributes_);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case telegram_api::webDocumentNoProxy::ID: {
|
|
|
|
auto web_document = move_tl_object_as<telegram_api::webDocumentNoProxy>(web_document_ptr);
|
|
|
|
if (web_document->url_.find('.') == string::npos) {
|
|
|
|
LOG(ERROR) << "Receive invalid URL " << web_document->url_;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto r_file_id = file_manager->from_persistent_id(web_document->url_, file_type);
|
|
|
|
if (r_file_id.is_error()) {
|
|
|
|
LOG(ERROR) << "Can't register URL: " << r_file_id.error();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
file_id = r_file_id.move_as_ok();
|
|
|
|
|
|
|
|
size = web_document->size_;
|
2020-05-30 00:48:56 +02:00
|
|
|
mime_type = std::move(web_document->mime_type_);
|
2018-03-03 22:33:26 +01:00
|
|
|
attributes = std::move(web_document->attributes_);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
CHECK(file_id.is_valid());
|
2020-05-30 00:48:56 +02:00
|
|
|
bool is_animation = mime_type == "video/mp4";
|
2020-06-01 19:34:48 +02:00
|
|
|
bool is_gif = mime_type == "image/gif";
|
2018-03-03 22:33:26 +01:00
|
|
|
|
|
|
|
Dimensions dimensions;
|
|
|
|
for (auto &attribute : attributes) {
|
|
|
|
switch (attribute->get_id()) {
|
|
|
|
case telegram_api::documentAttributeImageSize::ID: {
|
|
|
|
auto image_size = move_tl_object_as<telegram_api::documentAttributeImageSize>(attribute);
|
|
|
|
dimensions = get_dimensions(image_size->w_, image_size->h_);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case telegram_api::documentAttributeAnimated::ID:
|
|
|
|
case telegram_api::documentAttributeHasStickers::ID:
|
|
|
|
case telegram_api::documentAttributeSticker::ID:
|
|
|
|
case telegram_api::documentAttributeVideo::ID:
|
|
|
|
case telegram_api::documentAttributeAudio::ID:
|
|
|
|
LOG(ERROR) << "Unexpected web document attribute " << to_string(attribute);
|
|
|
|
break;
|
|
|
|
case telegram_api::documentAttributeFilename::ID:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PhotoSize s;
|
2020-06-24 13:13:33 +02:00
|
|
|
s.type = is_animation ? 'v' : (is_gif ? 'g' : (file_type == FileType::Thumbnail ? 't' : 'n'));
|
2018-03-03 22:33:26 +01:00
|
|
|
s.dimensions = dimensions;
|
|
|
|
s.size = size;
|
|
|
|
s.file_id = file_id;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2020-05-31 21:22:15 +02:00
|
|
|
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()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-06-01 19:34:48 +02:00
|
|
|
if (format == PhotoFormat::Jpeg && photo_size.type == 'g') {
|
|
|
|
format = PhotoFormat::Gif;
|
|
|
|
}
|
|
|
|
|
2020-05-31 21:22:15 +02:00
|
|
|
return td_api::make_object<td_api::thumbnail>(get_thumbnail_format_object(format), photo_size.dimensions.width,
|
|
|
|
photo_size.dimensions.height,
|
|
|
|
file_manager->get_file_object(photo_size.file_id));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2020-05-31 21:22:15 +02:00
|
|
|
static tl_object_ptr<td_api::photoSize> get_photo_size_object(FileManager *file_manager, const PhotoSize *photo_size) {
|
2020-05-30 00:48:56 +02:00
|
|
|
if (photo_size == nullptr || !photo_size->file_id.is_valid()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-05-31 21:22:15 +02:00
|
|
|
return td_api::make_object<td_api::photoSize>(
|
|
|
|
photo_size->type ? std::string(1, static_cast<char>(photo_size->type))
|
|
|
|
: std::string(), // TODO replace string type with integer type
|
2020-08-25 14:09:24 +02:00
|
|
|
file_manager->get_file_object(photo_size->file_id), photo_size->dimensions.width, photo_size->dimensions.height,
|
|
|
|
vector<int32>(photo_size->progressive_sizes));
|
2020-05-30 00:48:56 +02:00
|
|
|
}
|
|
|
|
|
2020-05-31 21:22:15 +02:00
|
|
|
static vector<td_api::object_ptr<td_api::photoSize>> get_photo_sizes_object(FileManager *file_manager,
|
|
|
|
const vector<PhotoSize> &photo_sizes) {
|
2019-01-17 01:27:43 +01:00
|
|
|
auto sizes = transform(photo_sizes, [file_manager](const PhotoSize &photo_size) {
|
|
|
|
return get_photo_size_object(file_manager, &photo_size);
|
|
|
|
});
|
2020-10-12 09:17:02 +02:00
|
|
|
std::stable_sort(sizes.begin(), sizes.end(), [](const auto &lhs, const auto &rhs) {
|
2018-03-09 14:56:42 +01:00
|
|
|
if (lhs->photo_->expected_size_ != rhs->photo_->expected_size_) {
|
|
|
|
return lhs->photo_->expected_size_ < rhs->photo_->expected_size_;
|
|
|
|
}
|
2018-04-03 09:35:04 +02:00
|
|
|
return static_cast<uint32>(lhs->width_) * static_cast<uint32>(lhs->height_) <
|
|
|
|
static_cast<uint32>(rhs->width_) * static_cast<uint32>(rhs->height_);
|
2018-03-09 14:56:42 +01:00
|
|
|
});
|
2020-10-12 09:31:57 +02:00
|
|
|
td::remove_if(sizes, [](const auto &size) {
|
|
|
|
return !size->photo_->local_->can_be_downloaded_ && !size->photo_->local_->is_downloading_completed_;
|
|
|
|
});
|
2019-01-17 01:27:43 +01:00
|
|
|
return sizes;
|
2018-03-09 14:56:42 +01:00
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
bool operator==(const PhotoSize &lhs, const PhotoSize &rhs) {
|
2020-08-25 14:09:24 +02:00
|
|
|
return lhs.type == rhs.type && lhs.dimensions == rhs.dimensions && lhs.size == rhs.size &&
|
|
|
|
lhs.file_id == rhs.file_id && lhs.progressive_sizes == rhs.progressive_sizes;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const PhotoSize &lhs, const PhotoSize &rhs) {
|
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
|
2018-04-03 09:35:04 +02:00
|
|
|
bool operator<(const PhotoSize &lhs, const PhotoSize &rhs) {
|
|
|
|
if (lhs.size != rhs.size) {
|
|
|
|
return lhs.size < rhs.size;
|
|
|
|
}
|
|
|
|
auto lhs_pixels = get_pixel_count(lhs.dimensions);
|
|
|
|
auto rhs_pixels = get_pixel_count(rhs.dimensions);
|
|
|
|
if (lhs_pixels != rhs_pixels) {
|
|
|
|
return lhs_pixels < rhs_pixels;
|
|
|
|
}
|
|
|
|
int32 lhs_type = lhs.type == 't' ? -1 : lhs.type;
|
|
|
|
int32 rhs_type = rhs.type == 't' ? -1 : rhs.type;
|
|
|
|
if (lhs_type != rhs_type) {
|
|
|
|
return lhs_type < rhs_type;
|
|
|
|
}
|
|
|
|
if (lhs.file_id != rhs.file_id) {
|
|
|
|
return lhs.file_id.get() < rhs.file_id.get();
|
|
|
|
}
|
|
|
|
return lhs.dimensions.width < rhs.dimensions.width;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
StringBuilder &operator<<(StringBuilder &string_builder, const PhotoSize &photo_size) {
|
|
|
|
return string_builder << "{type = " << photo_size.type << ", dimensions = " << photo_size.dimensions
|
2020-08-25 14:09:24 +02:00
|
|
|
<< ", size = " << photo_size.size << ", file_id = " << photo_size.file_id
|
|
|
|
<< ", progressive_sizes = " << photo_size.progressive_sizes << "}";
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2020-07-07 10:41:01 +02:00
|
|
|
static tl_object_ptr<td_api::animatedChatPhoto> get_animated_chat_photo_object(FileManager *file_manager,
|
|
|
|
const AnimationSize *animation_size) {
|
|
|
|
if (animation_size == nullptr || !animation_size->file_id.is_valid()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return td_api::make_object<td_api::animatedChatPhoto>(animation_size->dimensions.width,
|
|
|
|
file_manager->get_file_object(animation_size->file_id),
|
|
|
|
animation_size->main_frame_timestamp);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const AnimationSize &lhs, const AnimationSize &rhs) {
|
|
|
|
return static_cast<const PhotoSize &>(lhs) == static_cast<const PhotoSize &>(rhs) &&
|
|
|
|
fabs(lhs.main_frame_timestamp - rhs.main_frame_timestamp) < 1e-3;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const AnimationSize &lhs, const AnimationSize &rhs) {
|
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder &operator<<(StringBuilder &string_builder, const AnimationSize &animation_size) {
|
|
|
|
return string_builder << static_cast<const PhotoSize &>(animation_size) << " from "
|
|
|
|
<< animation_size.main_frame_timestamp;
|
|
|
|
}
|
|
|
|
|
2019-06-17 01:05:56 +02:00
|
|
|
Photo get_encrypted_file_photo(FileManager *file_manager, tl_object_ptr<telegram_api::encryptedFile> &&file,
|
|
|
|
tl_object_ptr<secret_api::decryptedMessageMediaPhoto> &&photo,
|
|
|
|
DialogId owner_dialog_id) {
|
2018-12-31 20:04:05 +01:00
|
|
|
FileId file_id = file_manager->register_remote(
|
2019-06-08 11:26:35 +02:00
|
|
|
FullRemoteFileLocation(FileType::Encrypted, file->id_, file->access_hash_, DcId::create(file->dc_id_), string()),
|
2018-03-08 21:12:31 +01:00
|
|
|
FileLocationSource::FromServer, owner_dialog_id, photo->size_, 0,
|
|
|
|
PSTRING() << static_cast<uint64>(file->id_) << ".jpg");
|
2018-12-31 20:04:05 +01:00
|
|
|
file_manager->set_encryption_key(file_id, FileEncryptionKey{photo->key_.as_slice(), photo->iv_.as_slice()});
|
|
|
|
|
|
|
|
Photo res;
|
2020-06-23 19:50:16 +02:00
|
|
|
res.id = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
res.date = 0;
|
|
|
|
|
|
|
|
if (!photo->thumb_.empty()) {
|
2019-03-01 20:51:33 +01:00
|
|
|
res.photos.push_back(get_secret_thumbnail_photo_size(file_manager, std::move(photo->thumb_), owner_dialog_id,
|
|
|
|
photo->thumb_w_, photo->thumb_h_));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
PhotoSize s;
|
|
|
|
s.type = 'i';
|
|
|
|
s.dimensions = get_dimensions(photo->w_, photo->h_);
|
|
|
|
s.size = photo->size_;
|
|
|
|
s.file_id = file_id;
|
|
|
|
res.photos.push_back(s);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2019-06-08 11:26:35 +02:00
|
|
|
Photo get_photo(FileManager *file_manager, tl_object_ptr<telegram_api::Photo> &&photo, DialogId owner_dialog_id) {
|
|
|
|
if (photo == nullptr || photo->get_id() == telegram_api::photoEmpty::ID) {
|
2020-06-23 19:50:16 +02:00
|
|
|
return Photo();
|
2019-06-08 11:26:35 +02:00
|
|
|
}
|
|
|
|
CHECK(photo->get_id() == telegram_api::photo::ID);
|
|
|
|
return get_photo(file_manager, move_tl_object_as<telegram_api::photo>(photo), owner_dialog_id);
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Photo get_photo(FileManager *file_manager, tl_object_ptr<telegram_api::photo> &&photo, DialogId owner_dialog_id) {
|
2020-06-23 18:40:37 +02:00
|
|
|
CHECK(photo != nullptr);
|
2018-12-31 20:04:05 +01:00
|
|
|
Photo res;
|
|
|
|
|
|
|
|
res.id = photo->id_;
|
|
|
|
res.date = photo->date_;
|
|
|
|
res.has_stickers = (photo->flags_ & telegram_api::photo::HAS_STICKERS_MASK) != 0;
|
|
|
|
|
2020-06-23 20:04:26 +02:00
|
|
|
if (res.is_empty()) {
|
2020-07-06 14:26:29 +02:00
|
|
|
LOG(ERROR) << "Receive photo with id " << res.id.get();
|
2019-06-17 01:41:57 +02:00
|
|
|
res.id = -3;
|
|
|
|
}
|
|
|
|
|
2020-06-24 13:13:33 +02:00
|
|
|
DcId dc_id = DcId::create(photo->dc_id_);
|
2018-12-31 20:04:05 +01:00
|
|
|
for (auto &size_ptr : photo->sizes_) {
|
2019-06-08 11:26:35 +02:00
|
|
|
auto photo_size = get_photo_size(file_manager, {FileType::Photo, 0}, photo->id_, photo->access_hash_,
|
2020-06-24 13:13:33 +02:00
|
|
|
photo->file_reference_.as_slice().str(), dc_id, owner_dialog_id,
|
2020-11-09 21:41:23 +01:00
|
|
|
std::move(size_ptr), PhotoFormat::Jpeg, true);
|
2019-03-01 20:51:33 +01:00
|
|
|
if (photo_size.get_offset() == 0) {
|
2019-07-20 01:33:49 +02:00
|
|
|
PhotoSize &size = photo_size.get<0>();
|
2020-06-24 13:13:33 +02:00
|
|
|
if (size.type == 0 || size.type == 't' || size.type == 'i' || size.type == 'u' || size.type == 'v') {
|
2019-07-20 01:33:49 +02:00
|
|
|
LOG(ERROR) << "Skip unallowed photo size " << size;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
res.photos.push_back(std::move(size));
|
2019-03-01 20:51:33 +01:00
|
|
|
} else {
|
|
|
|
res.minithumbnail = std::move(photo_size.get<1>());
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2020-06-24 13:13:33 +02:00
|
|
|
for (auto &size_ptr : photo->video_sizes_) {
|
2020-07-07 10:41:01 +02:00
|
|
|
auto animation =
|
|
|
|
get_animation_size(file_manager, {FileType::Photo, 0}, photo->id_, photo->access_hash_,
|
|
|
|
photo->file_reference_.as_slice().str(), dc_id, owner_dialog_id, std::move(size_ptr));
|
|
|
|
if (animation.type != 0 && animation.dimensions.width == animation.dimensions.height) {
|
|
|
|
res.animations.push_back(std::move(animation));
|
|
|
|
}
|
2020-06-24 13:13:33 +02:00
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-09-27 21:44:40 +02:00
|
|
|
Photo get_web_document_photo(FileManager *file_manager, tl_object_ptr<telegram_api::WebDocument> web_document,
|
|
|
|
DialogId owner_dialog_id) {
|
|
|
|
PhotoSize s = get_web_document_photo_size(file_manager, FileType::Photo, owner_dialog_id, std::move(web_document));
|
|
|
|
Photo photo;
|
2020-06-23 19:50:16 +02:00
|
|
|
if (s.file_id.is_valid() && s.type != 'v' && s.type != 'g') {
|
2018-09-27 21:44:40 +02:00
|
|
|
photo.id = 0;
|
|
|
|
photo.photos.push_back(s);
|
|
|
|
}
|
|
|
|
return photo;
|
|
|
|
}
|
|
|
|
|
2020-07-06 17:45:43 +02:00
|
|
|
tl_object_ptr<td_api::photo> get_photo_object(FileManager *file_manager, const Photo &photo) {
|
|
|
|
if (photo.is_empty()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-07-06 17:45:43 +02:00
|
|
|
return td_api::make_object<td_api::photo>(photo.has_stickers, get_minithumbnail_object(photo.minithumbnail),
|
|
|
|
get_photo_sizes_object(file_manager, photo.photos));
|
2018-09-12 22:59:10 +02:00
|
|
|
}
|
|
|
|
|
2020-07-07 12:48:56 +02:00
|
|
|
tl_object_ptr<td_api::chatPhoto> get_chat_photo_object(FileManager *file_manager, const Photo &photo) {
|
2020-07-06 17:45:43 +02:00
|
|
|
if (photo.is_empty()) {
|
2020-07-06 14:26:29 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-07-07 10:41:01 +02:00
|
|
|
const AnimationSize *animation = photo.animations.empty() ? nullptr : &photo.animations.back();
|
2020-06-30 11:57:54 +02:00
|
|
|
return td_api::make_object<td_api::chatPhoto>(
|
|
|
|
photo.id.get(), photo.date, get_minithumbnail_object(photo.minithumbnail),
|
2020-07-07 10:41:01 +02:00
|
|
|
get_photo_sizes_object(file_manager, photo.photos), get_animated_chat_photo_object(file_manager, animation));
|
2020-07-06 14:26:29 +02:00
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void photo_delete_thumbnail(Photo &photo) {
|
|
|
|
for (size_t i = 0; i < photo.photos.size(); i++) {
|
|
|
|
if (photo.photos[i].type == 't') {
|
|
|
|
photo.photos.erase(photo.photos.begin() + i);
|
2019-03-16 21:13:18 +01:00
|
|
|
return;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-03 14:44:33 +02:00
|
|
|
bool photo_has_input_media(FileManager *file_manager, const Photo &photo, bool is_secret, bool is_bot) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (photo.photos.empty() || photo.photos.back().type != 'i') {
|
|
|
|
LOG(ERROR) << "Wrong photo: " << photo;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto file_id = photo.photos.back().file_id;
|
|
|
|
auto file_view = file_manager->get_file_view(file_id);
|
|
|
|
if (is_secret) {
|
2018-03-27 15:11:15 +02:00
|
|
|
if (!file_view.is_encrypted_secret() || !file_view.has_remote_location()) {
|
2018-01-31 10:18:40 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto &size : photo.photos) {
|
|
|
|
if (size.type == 't' && size.file_id.is_valid()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
|
|
|
if (file_view.is_encrypted()) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-05-03 14:44:33 +02:00
|
|
|
if (is_bot && file_view.has_remote_location()) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-02-10 00:39:58 +01:00
|
|
|
return /* file_view.has_remote_location() || */ file_view.has_url();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tl_object_ptr<telegram_api::InputMedia> photo_get_input_media(FileManager *file_manager, const Photo &photo,
|
|
|
|
tl_object_ptr<telegram_api::InputFile> input_file,
|
2018-01-30 18:06:54 +01:00
|
|
|
int32 ttl) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!photo.photos.empty()) {
|
|
|
|
auto file_id = photo.photos.back().file_id;
|
|
|
|
auto file_view = file_manager->get_file_view(file_id);
|
|
|
|
if (file_view.is_encrypted()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-11-17 20:41:28 +01:00
|
|
|
if (file_view.has_remote_location() && !file_view.main_remote_location().is_web() && input_file == nullptr) {
|
2018-12-31 20:04:05 +01:00
|
|
|
int32 flags = 0;
|
|
|
|
if (ttl != 0) {
|
|
|
|
flags |= telegram_api::inputMediaPhoto::TTL_SECONDS_MASK;
|
|
|
|
}
|
2019-11-17 20:41:28 +01:00
|
|
|
return make_tl_object<telegram_api::inputMediaPhoto>(flags, file_view.main_remote_location().as_input_photo(),
|
|
|
|
ttl);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (file_view.has_url()) {
|
|
|
|
int32 flags = 0;
|
|
|
|
if (ttl != 0) {
|
|
|
|
flags |= telegram_api::inputMediaPhotoExternal::TTL_SECONDS_MASK;
|
|
|
|
}
|
2018-01-30 18:06:54 +01:00
|
|
|
LOG(INFO) << "Create inputMediaPhotoExternal with a URL " << file_view.url() << " and ttl " << ttl;
|
|
|
|
return make_tl_object<telegram_api::inputMediaPhotoExternal>(flags, file_view.url(), ttl);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2019-02-12 02:50:30 +01:00
|
|
|
if (input_file == nullptr) {
|
|
|
|
CHECK(!file_view.has_remote_location());
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (input_file != nullptr) {
|
|
|
|
int32 flags = 0;
|
|
|
|
vector<tl_object_ptr<telegram_api::InputDocument>> added_stickers;
|
|
|
|
if (photo.has_stickers) {
|
|
|
|
flags |= telegram_api::inputMediaUploadedPhoto::STICKERS_MASK;
|
|
|
|
added_stickers = file_manager->get_input_documents(photo.sticker_file_ids);
|
|
|
|
}
|
|
|
|
if (ttl != 0) {
|
|
|
|
flags |= telegram_api::inputMediaUploadedPhoto::TTL_SECONDS_MASK;
|
|
|
|
}
|
|
|
|
|
2018-01-30 18:06:54 +01:00
|
|
|
return make_tl_object<telegram_api::inputMediaUploadedPhoto>(flags, std::move(input_file),
|
2018-12-31 20:04:05 +01:00
|
|
|
std::move(added_stickers), ttl);
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
SecretInputMedia photo_get_secret_input_media(FileManager *file_manager, const Photo &photo,
|
|
|
|
tl_object_ptr<telegram_api::InputEncryptedFile> input_file,
|
|
|
|
const string &caption, BufferSlice thumbnail) {
|
|
|
|
FileId file_id;
|
|
|
|
int32 width = 0;
|
|
|
|
int32 height = 0;
|
|
|
|
|
|
|
|
FileId thumbnail_file_id;
|
|
|
|
int32 thumbnail_width = 0;
|
|
|
|
int32 thumbnail_height = 0;
|
|
|
|
for (const auto &size : photo.photos) {
|
|
|
|
if (size.type == 'i') {
|
|
|
|
file_id = size.file_id;
|
|
|
|
width = size.dimensions.width;
|
|
|
|
height = size.dimensions.height;
|
|
|
|
}
|
|
|
|
if (size.type == 't') {
|
|
|
|
thumbnail_file_id = size.file_id;
|
|
|
|
thumbnail_width = size.dimensions.width;
|
|
|
|
thumbnail_height = size.dimensions.height;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (file_id.empty()) {
|
|
|
|
LOG(ERROR) << "NO SIZE";
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
auto file_view = file_manager->get_file_view(file_id);
|
|
|
|
auto &encryption_key = file_view.encryption_key();
|
2018-06-08 21:23:49 +02:00
|
|
|
if (!file_view.is_encrypted_secret() || encryption_key.empty()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
if (file_view.has_remote_location()) {
|
2019-11-17 20:41:28 +01:00
|
|
|
LOG(INFO) << "Photo has remote location";
|
|
|
|
input_file = file_view.main_remote_location().as_input_encrypted_file();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (input_file == nullptr) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
if (thumbnail_file_id.is_valid() && thumbnail.empty()) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return SecretInputMedia{
|
|
|
|
std::move(input_file),
|
|
|
|
make_tl_object<secret_api::decryptedMessageMediaPhoto>(
|
|
|
|
std::move(thumbnail), thumbnail_width, thumbnail_height, width, height, narrow_cast<int32>(file_view.size()),
|
|
|
|
BufferSlice(encryption_key.key_slice()), BufferSlice(encryption_key.iv_slice()), caption)};
|
|
|
|
}
|
|
|
|
|
2019-01-06 00:15:07 +01:00
|
|
|
vector<FileId> photo_get_file_ids(const Photo &photo) {
|
2020-06-24 13:13:33 +02:00
|
|
|
auto result = transform(photo.photos, [](auto &size) { return size.file_id; });
|
2020-07-07 10:41:01 +02:00
|
|
|
if (!photo.animations.empty()) {
|
2020-06-28 16:44:56 +02:00
|
|
|
// photo file IDs must be first
|
2020-07-07 10:41:01 +02:00
|
|
|
append(result, transform(photo.animations, [](auto &size) { return size.file_id; }));
|
2020-06-24 13:13:33 +02:00
|
|
|
}
|
|
|
|
return result;
|
2019-01-06 00:15:07 +01:00
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
bool operator==(const Photo &lhs, const Photo &rhs) {
|
2020-07-07 10:41:01 +02:00
|
|
|
return lhs.id.get() == rhs.id.get() && lhs.photos == rhs.photos && lhs.animations == rhs.animations;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const Photo &lhs, const Photo &rhs) {
|
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder &operator<<(StringBuilder &string_builder, const Photo &photo) {
|
2020-06-24 13:13:33 +02:00
|
|
|
string_builder << "[id = " << photo.id.get() << ", photos = " << format::as_array(photo.photos);
|
2020-07-07 10:41:01 +02:00
|
|
|
if (!photo.animations.empty()) {
|
|
|
|
string_builder << ", animations = " << format::as_array(photo.animations);
|
2020-06-24 13:13:33 +02:00
|
|
|
}
|
|
|
|
return string_builder << "]";
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-06-08 11:26:35 +02:00
|
|
|
static tl_object_ptr<telegram_api::fileLocationToBeDeprecated> copy_location(
|
|
|
|
const tl_object_ptr<telegram_api::fileLocationToBeDeprecated> &location) {
|
|
|
|
CHECK(location != nullptr);
|
|
|
|
return make_tl_object<telegram_api::fileLocationToBeDeprecated>(location->volume_id_, location->local_id_);
|
2019-01-22 17:08:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
tl_object_ptr<telegram_api::userProfilePhoto> convert_photo_to_profile_photo(
|
|
|
|
const tl_object_ptr<telegram_api::photo> &photo) {
|
2019-06-08 11:26:35 +02:00
|
|
|
if (photo == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
tl_object_ptr<telegram_api::fileLocationToBeDeprecated> photo_small;
|
|
|
|
tl_object_ptr<telegram_api::fileLocationToBeDeprecated> photo_big;
|
2019-01-22 17:08:41 +01:00
|
|
|
for (auto &size_ptr : photo->sizes_) {
|
|
|
|
switch (size_ptr->get_id()) {
|
|
|
|
case telegram_api::photoSizeEmpty::ID:
|
|
|
|
break;
|
|
|
|
case telegram_api::photoSize::ID: {
|
|
|
|
auto size = static_cast<const telegram_api::photoSize *>(size_ptr.get());
|
|
|
|
if (size->type_ == "a") {
|
|
|
|
photo_small = copy_location(size->location_);
|
|
|
|
} else if (size->type_ == "c") {
|
|
|
|
photo_big = copy_location(size->location_);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case telegram_api::photoCachedSize::ID: {
|
|
|
|
auto size = static_cast<const telegram_api::photoCachedSize *>(size_ptr.get());
|
|
|
|
if (size->type_ == "a") {
|
|
|
|
photo_small = copy_location(size->location_);
|
|
|
|
} else if (size->type_ == "c") {
|
|
|
|
photo_big = copy_location(size->location_);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2019-03-01 20:51:33 +01:00
|
|
|
case telegram_api::photoStrippedSize::ID:
|
|
|
|
break;
|
2020-08-25 14:09:24 +02:00
|
|
|
case telegram_api::photoSizeProgressive::ID: {
|
|
|
|
auto size = static_cast<const telegram_api::photoSizeProgressive *>(size_ptr.get());
|
|
|
|
if (size->type_ == "a") {
|
|
|
|
photo_small = copy_location(size->location_);
|
|
|
|
} else if (size->type_ == "c") {
|
|
|
|
photo_big = copy_location(size->location_);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2019-01-22 17:08:41 +01:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (photo_small == nullptr || photo_big == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2020-06-24 10:45:36 +02:00
|
|
|
int32 flags = 0;
|
2020-06-24 13:13:33 +02:00
|
|
|
if (!photo->video_sizes_.empty()) {
|
|
|
|
flags |= telegram_api::userProfilePhoto::HAS_VIDEO_MASK;
|
|
|
|
}
|
2020-06-24 10:45:36 +02:00
|
|
|
return make_tl_object<telegram_api::userProfilePhoto>(flags, false /*ignored*/, photo->id_, std::move(photo_small),
|
|
|
|
std::move(photo_big), photo->dc_id_);
|
2019-01-22 17:08:41 +01:00
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|