Represent photo format as enum PhotoFormat.

GitOrigin-RevId: 9205a8c775111ff394e7c32bc52d343b41e53379
This commit is contained in:
levlam 2020-05-11 22:17:49 +03:00
parent 2def12b70f
commit d647a2a2e8
4 changed files with 51 additions and 29 deletions

View File

@ -135,7 +135,7 @@ Document DocumentsManager::on_get_document(RemoteDocument remote_document, Dialo
FileType file_type = FileType::Document; FileType file_type = FileType::Document;
Slice default_extension; Slice default_extension;
bool supports_streaming = false; bool supports_streaming = false;
bool has_webp_thumbnail = false; PhotoFormat thumbnail_format = PhotoFormat::Jpeg;
if (type_attributes == 1 || default_document_type != Document::Type::General) { // not a general document if (type_attributes == 1 || default_document_type != Document::Type::General) { // not a general document
if (animated != nullptr || default_document_type == Document::Type::Animation) { if (animated != nullptr || default_document_type == Document::Type::Animation) {
document_type = Document::Type::Animation; document_type = Document::Type::Animation;
@ -163,7 +163,9 @@ Document DocumentsManager::on_get_document(RemoteDocument remote_document, Dialo
default_extension = Slice("webp"); default_extension = Slice("webp");
owner_dialog_id = DialogId(); owner_dialog_id = DialogId();
file_name.clear(); file_name.clear();
has_webp_thumbnail = td_->stickers_manager_->has_webp_thumbnail(sticker); if (td_->stickers_manager_->has_webp_thumbnail(sticker) && remote_document.secret_file == nullptr) {
thumbnail_format = PhotoFormat::Webp;
}
} else if (video != nullptr || default_document_type == Document::Type::Video || } else if (video != nullptr || default_document_type == Document::Type::Video ||
default_document_type == Document::Type::VideoNote) { default_document_type == Document::Type::VideoNote) {
bool is_video_note = default_document_type == Document::Type::VideoNote; bool is_video_note = default_document_type == Document::Type::VideoNote;
@ -190,7 +192,6 @@ Document DocumentsManager::on_get_document(RemoteDocument remote_document, Dialo
<< ", has_stickers = " << has_stickers; << ", has_stickers = " << has_stickers;
} }
bool has_png_thumbnail = false;
if (is_background) { if (is_background) {
if (document_type != Document::Type::General) { if (document_type != Document::Type::General) {
LOG(ERROR) << "Receive background of type " << document_type; LOG(ERROR) << "Receive background of type " << document_type;
@ -199,7 +200,7 @@ Document DocumentsManager::on_get_document(RemoteDocument remote_document, Dialo
file_type = FileType::Background; file_type = FileType::Background;
if (is_pattern) { if (is_pattern) {
default_extension = Slice("png"); default_extension = Slice("png");
has_png_thumbnail = true; thumbnail_format = PhotoFormat::Png;
} else { } else {
default_extension = Slice("jpg"); default_extension = Slice("jpg");
} }
@ -258,9 +259,9 @@ Document DocumentsManager::on_get_document(RemoteDocument remote_document, Dialo
if (document_type != Document::Type::VoiceNote) { if (document_type != Document::Type::VoiceNote) {
for (auto &thumb : document->thumbs_) { for (auto &thumb : document->thumbs_) {
auto photo_size = get_photo_size(td_->file_manager_.get(), {FileType::Thumbnail, 0}, id, access_hash, auto photo_size =
file_reference, DcId::create(dc_id), owner_dialog_id, std::move(thumb), get_photo_size(td_->file_manager_.get(), {FileType::Thumbnail, 0}, id, access_hash, file_reference,
has_webp_thumbnail, has_png_thumbnail); DcId::create(dc_id), owner_dialog_id, std::move(thumb), thumbnail_format);
if (photo_size.get_offset() == 0) { if (photo_size.get_offset() == 0) {
thumbnail = std::move(photo_size.get<0>()); thumbnail = std::move(photo_size.get<0>());
} else { } else {

View File

@ -95,18 +95,32 @@ td_api::object_ptr<td_api::minithumbnail> get_minithumbnail_object(const string
return nullptr; return nullptr;
} }
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";
case PhotoFormat::Tgs:
return string_builder << "tgs";
default:
UNREACHABLE();
return string_builder;
}
}
static FileId register_photo(FileManager *file_manager, const PhotoSizeSource &source, int64 id, int64 access_hash, static FileId register_photo(FileManager *file_manager, const PhotoSizeSource &source, int64 id, int64 access_hash,
std::string file_reference, std::string file_reference,
tl_object_ptr<telegram_api::fileLocationToBeDeprecated> &&location, tl_object_ptr<telegram_api::fileLocationToBeDeprecated> &&location,
DialogId owner_dialog_id, int32 file_size, DcId dc_id, bool is_webp = false, DialogId owner_dialog_id, int32 file_size, DcId dc_id, PhotoFormat format) {
bool is_png = false) {
int32 local_id = location->local_id_; int32 local_id = location->local_id_;
int64 volume_id = location->volume_id_; int64 volume_id = location->volume_id_;
LOG(DEBUG) << "Receive " << (is_webp ? "webp" : (is_png ? "png" : "jpeg")) << " photo of type " LOG(DEBUG) << "Receive " << format << " photo of type " << source.get_file_type() << " in [" << dc_id << ","
<< source.get_file_type() << " in [" << dc_id << "," << volume_id << "," << local_id << "]. Id: (" << id << volume_id << "," << local_id << "]. Id: (" << id << ", " << access_hash << ")";
<< ", " << access_hash << ")"; auto suggested_name = PSTRING() << static_cast<uint64>(volume_id) << "_" << static_cast<uint64>(local_id) << '.'
auto suggested_name = PSTRING() << static_cast<uint64>(volume_id) << "_" << static_cast<uint64>(local_id) << format;
<< (is_webp ? ".webp" : (is_png ? ".png" : ".jpg"));
auto file_location_source = owner_dialog_id.get_type() == DialogType::SecretChat ? FileLocationSource::FromUser auto file_location_source = owner_dialog_id.get_type() == DialogType::SecretChat ? FileLocationSource::FromUser
: FileLocationSource::FromServer; : FileLocationSource::FromServer;
return file_manager->register_remote( return file_manager->register_remote(
@ -127,10 +141,12 @@ ProfilePhoto get_profile_photo(FileManager *file_manager, UserId user_id, int64
auto dc_id = DcId::create(profile_photo->dc_id_); auto dc_id = DcId::create(profile_photo->dc_id_);
result.id = profile_photo->photo_id_; result.id = profile_photo->photo_id_;
result.small_file_id = register_photo(file_manager, {DialogId(user_id), user_access_hash, false}, result.id, 0, result.small_file_id =
"", std::move(profile_photo->photo_small_), DialogId(), 0, dc_id); register_photo(file_manager, {DialogId(user_id), user_access_hash, false}, result.id, 0, "",
result.big_file_id = register_photo(file_manager, {DialogId(user_id), user_access_hash, true}, result.id, 0, "", std::move(profile_photo->photo_small_), DialogId(), 0, dc_id, PhotoFormat::Jpeg);
std::move(profile_photo->photo_big_), DialogId(), 0, dc_id); 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);
break; break;
} }
default: default:
@ -190,10 +206,11 @@ DialogPhoto get_dialog_photo(FileManager *file_manager, DialogId dialog_id, int6
auto chat_photo = move_tl_object_as<telegram_api::chatPhoto>(chat_photo_ptr); auto chat_photo = move_tl_object_as<telegram_api::chatPhoto>(chat_photo_ptr);
auto dc_id = DcId::create(chat_photo->dc_id_); auto dc_id = DcId::create(chat_photo->dc_id_);
result.small_file_id = register_photo(file_manager, {dialog_id, dialog_access_hash, false}, 0, 0, "", result.small_file_id =
std::move(chat_photo->photo_small_), DialogId(), 0, dc_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);
result.big_file_id = register_photo(file_manager, {dialog_id, dialog_access_hash, true}, 0, 0, "", result.big_file_id = register_photo(file_manager, {dialog_id, dialog_access_hash, true}, 0, 0, "",
std::move(chat_photo->photo_big_), DialogId(), 0, dc_id); std::move(chat_photo->photo_big_), DialogId(), 0, dc_id, PhotoFormat::Jpeg);
break; break;
} }
@ -283,7 +300,7 @@ PhotoSize get_secret_thumbnail_photo_size(FileManager *file_manager, BufferSlice
Variant<PhotoSize, string> get_photo_size(FileManager *file_manager, PhotoSizeSource source, int64 id, Variant<PhotoSize, string> get_photo_size(FileManager *file_manager, PhotoSizeSource source, int64 id,
int64 access_hash, std::string file_reference, DcId dc_id, int64 access_hash, std::string file_reference, DcId dc_id,
DialogId owner_dialog_id, tl_object_ptr<telegram_api::PhotoSize> &&size_ptr, DialogId owner_dialog_id, tl_object_ptr<telegram_api::PhotoSize> &&size_ptr,
bool is_webp, bool is_png) { PhotoFormat format) {
CHECK(size_ptr != nullptr); CHECK(size_ptr != nullptr);
tl_object_ptr<telegram_api::fileLocationToBeDeprecated> location; tl_object_ptr<telegram_api::fileLocationToBeDeprecated> location;
@ -336,7 +353,7 @@ Variant<PhotoSize, string> get_photo_size(FileManager *file_manager, PhotoSizeSo
} }
res.file_id = register_photo(file_manager, source, id, access_hash, file_reference, std::move(location), res.file_id = register_photo(file_manager, source, id, access_hash, file_reference, std::move(location),
owner_dialog_id, res.size, dc_id, is_webp, is_png); owner_dialog_id, res.size, dc_id, format);
if (!content.empty()) { if (!content.empty()) {
file_manager->set_content(res.file_id, std::move(content)); file_manager->set_content(res.file_id, std::move(content));
@ -535,7 +552,7 @@ Photo get_photo(FileManager *file_manager, tl_object_ptr<telegram_api::photo> &&
for (auto &size_ptr : photo->sizes_) { for (auto &size_ptr : photo->sizes_) {
auto photo_size = get_photo_size(file_manager, {FileType::Photo, 0}, photo->id_, photo->access_hash_, auto photo_size = get_photo_size(file_manager, {FileType::Photo, 0}, photo->id_, photo->access_hash_,
photo->file_reference_.as_slice().str(), DcId::create(photo->dc_id_), photo->file_reference_.as_slice().str(), DcId::create(photo->dc_id_),
owner_dialog_id, std::move(size_ptr), false, false); owner_dialog_id, std::move(size_ptr), PhotoFormat::Jpeg);
if (photo_size.get_offset() == 0) { if (photo_size.get_offset() == 0) {
PhotoSize &size = photo_size.get<0>(); PhotoSize &size = photo_size.get<0>();
if (size.type == 0 || size.type == 't' || size.type == 'i') { if (size.type == 0 || size.type == 't' || size.type == 'i') {

View File

@ -90,12 +90,14 @@ bool operator!=(const DialogPhoto &lhs, const DialogPhoto &rhs);
StringBuilder &operator<<(StringBuilder &string_builder, const DialogPhoto &dialog_photo); StringBuilder &operator<<(StringBuilder &string_builder, const DialogPhoto &dialog_photo);
enum class PhotoFormat : int32 { Jpeg, Png, Webp, Tgs };
PhotoSize get_secret_thumbnail_photo_size(FileManager *file_manager, BufferSlice bytes, DialogId owner_dialog_id, PhotoSize get_secret_thumbnail_photo_size(FileManager *file_manager, BufferSlice bytes, DialogId owner_dialog_id,
int32 width, int32 height); int32 width, int32 height);
Variant<PhotoSize, string> get_photo_size(FileManager *file_manager, PhotoSizeSource source, int64 id, Variant<PhotoSize, string> get_photo_size(FileManager *file_manager, PhotoSizeSource source, int64 id,
int64 access_hash, string file_reference, DcId dc_id, int64 access_hash, string file_reference, DcId dc_id,
DialogId owner_dialog_id, tl_object_ptr<telegram_api::PhotoSize> &&size_ptr, DialogId owner_dialog_id, tl_object_ptr<telegram_api::PhotoSize> &&size_ptr,
bool is_webp, bool is_png); PhotoFormat format);
PhotoSize get_web_document_photo_size(FileManager *file_manager, FileType file_type, DialogId owner_dialog_id, 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); tl_object_ptr<telegram_api::WebDocument> web_document_ptr);
td_api::object_ptr<td_api::photoSize> get_photo_size_object(FileManager *file_manager, const PhotoSize *photo_size); td_api::object_ptr<td_api::photoSize> get_photo_size_object(FileManager *file_manager, const PhotoSize *photo_size);

View File

@ -1584,9 +1584,10 @@ std::pair<int64, FileId> StickersManager::on_get_sticker_document(
PhotoSize thumbnail; PhotoSize thumbnail;
for (auto &thumb : document->thumbs_) { for (auto &thumb : document->thumbs_) {
auto photo_size = get_photo_size(td_->file_manager_.get(), {FileType::Thumbnail, 0}, document_id, auto photo_size =
document->access_hash_, document->file_reference_.as_slice().str(), dc_id, get_photo_size(td_->file_manager_.get(), {FileType::Thumbnail, 0}, document_id, document->access_hash_,
DialogId(), std::move(thumb), has_webp_thumbnail(sticker), false); document->file_reference_.as_slice().str(), dc_id, DialogId(), std::move(thumb),
has_webp_thumbnail(sticker) ? PhotoFormat::Webp : PhotoFormat::Jpeg);
if (photo_size.get_offset() == 0) { if (photo_size.get_offset() == 0) {
thumbnail = std::move(photo_size.get<0>()); thumbnail = std::move(photo_size.get<0>());
break; break;
@ -2084,7 +2085,8 @@ StickerSetId StickersManager::on_get_sticker_set(tl_object_ptr<telegram_api::sti
PhotoSize thumbnail; PhotoSize thumbnail;
if (set->thumb_ != nullptr) { if (set->thumb_ != nullptr) {
auto photo_size = get_photo_size(td_->file_manager_.get(), {set_id.get(), s->access_hash}, 0, 0, "", auto photo_size = get_photo_size(td_->file_manager_.get(), {set_id.get(), s->access_hash}, 0, 0, "",
DcId::create(set->thumb_dc_id_), DialogId(), std::move(set->thumb_), true, false); DcId::create(set->thumb_dc_id_), DialogId(), std::move(set->thumb_),
is_animated ? PhotoFormat::Tgs : PhotoFormat::Webp);
if (photo_size.get_offset() == 0) { if (photo_size.get_offset() == 0) {
thumbnail = std::move(photo_size.get<0>()); thumbnail = std::move(photo_size.get<0>());
} else { } else {